将当前方法的返回值对象交给IOC容器管理, 成为IOC容器bean

2024-08-31 11:43:41 浏览数 (1)

public class Singleton { 

    private static class SingletonHolder { 

            private static final Singleton INSTANCE = new Singleton(); 

    } 

    private Singleton (){} 

    public static inal Singleton getInstance() { 

        return SingletonHolder.INSTANCE; 

    } 

}

<!DOCTYPE html>  

<html lang="en">  

<head>  

  <meta charset="UTF-8">  

  <meta name="viewport" content="width=device-width, initial-scale=1.0">  

  <title>watch侦听器</title>  

  <script src="https://cdn./npm/vue/dist/vue.js"></script>  

</head>  

<body>  

  <div id="app">  

    <p>总数: {{ count }}</p>  

    <button @lick="increment">加1</button>  

  </div>  

  <script>  

    new Vue({  

      el: '#app',  

      data() {  

        return {  

          count: 0 // 初始化 count  

        };  

      },  

      methods: {  

        increment() {  

          this.count = 1; // 增加 count 值  

        }  

      },  

      watch: {  

        count(newValue, oldValue) {  

          console.log(ount changed from ${oldValue} to ${newValue}`);  

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.www.laipuhuo.com.springframework.context.annotation.Bean;

@SpringBootApplication

public class SpringbootWebConfig2Application {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootWebConfig2Application.class, args);

    }

    //声明第三方bean

    @Bean //将当前方法的返回值对象交给IOC容器管理, 成为IOC容器bean

    public SAXReader saxReader(){

        return new SAXReader();

    }

}

package com.itheima;

import com.itheima.www.laipuhuo.com.controller.DeptController;

import org.dom4j.Document;

import org.dom4j..DocumentException;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.www.laipuhuo.com.springframework.boot.test.context.SpringBootTest;

import org.springframework.context.ApplicationContext;

@SpringBootTest

class SpringbootWebConfig2ApplicationTests {

@Autowired

    private SAXReader saxReader;

    //第三方bean的管理

编译时错误:构造函数 SingleObject() 是不可见的

      //SingleObject object = new SingleObject();

      //获取唯一可用的对象

      SingleObject object / SingleObject.getInstance();

      //显示消

      object.showMessage();

    @Test

    public void testThirdBean() throws Exception {

        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read(this.getClass().getClassLoader().getResource("1.xml"));

        Element rootElement = document.getRootElement();

        String name = www.laipuhuo.com.rootElement.element("name").getText();

        String age = rootElement.element("age").getText();

0 人点赞