“Spring Boot @import 导入组件”的版本间的差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
Jihongchang(讨论 | 贡献)  | 
				Jihongchang(讨论 | 贡献)   | 
				||
| 第11行: | 第11行: | ||
@Repository 代表是一个数据库层组件  | @Repository 代表是一个数据库层组件  | ||
| + | |||
| + | === @import ===  | ||
| + | 在容器中的组件上面使用 @import 注解<syntaxhighlight lang="java">  | ||
| + | package io.github.jihch.boot.config;  | ||
| + | |||
| + | import ch.qos.logback.core.db.DBHelper;  | ||
| + | import io.github.jihch.boot.bean.Pet;  | ||
| + | import io.github.jihch.boot.bean.User;  | ||
| + | import org.springframework.context.annotation.Bean;  | ||
| + | import org.springframework.context.annotation.Configuration;  | ||
| + | import org.springframework.context.annotation.Import;  | ||
| + | |||
| + | /**  | ||
| + |  * 1、配置类里面使用 @Bean 标注在方法上给容器注册组件,默认也是单实例的  | ||
| + |  * 2、配置类本身也是组件  | ||
| + |  * 3、proxyBeanMethods:代理 bean 的方法  | ||
| + |  *     Full(proxyBeanMethods = true)  | ||
| + |  *     Lite(proxyBeanMethods = false)  | ||
| + |  *     组件依赖  | ||
| + |  *  | ||
| + |  *  4、@Import({User.class, DBHelper.class})  | ||
| + |  *     在容器中自动创建出这两个类型的组件  | ||
| + |  */  | ||
| + | @Import({User.class, DBHelper.class})  | ||
| + | @Configuration(proxyBeanMethods = false) //告诉 SpringBoot 这是一个配置类 == 配置文件  | ||
| + | public class MyConfig {  | ||
| + | |||
| + |     /**  | ||
| + |      * 给容器中添加组件。  | ||
| + |      * 以方法名作为组件的id。  | ||
| + |      * 返回类型就是组件类型。  | ||
| + |      * 返回的值,就是组件在容器中的实例  | ||
| + |      *  | ||
| + |      * 外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象  | ||
| + |      * @return  | ||
| + |      */  | ||
| + |     @Bean  | ||
| + |     public User user01() {  | ||
| + |         User zhangsan = new User("zhangsan", 18);  | ||
| + |         zhangsan.setPet(tomcatPet());  | ||
| + |         return zhangsan;  | ||
| + |     }  | ||
| + | |||
| + |     @Bean("tom")  | ||
| + |     public Pet tomcatPet() {  | ||
| + |         return new Pet("tomcat");  | ||
| + |     }  | ||
| + | |||
| + | }  | ||
| + | |||
| + | </syntaxhighlight>修改 MainApplication,验证通过 @import 方式导入组件是否成功<syntaxhighlight lang="java">  | ||
| + | @SpringBootApplication(scanBasePackages = "io.github.jihch")  | ||
| + | public class MainApplication {  | ||
| + | |||
| + |     public static void main(String[] args) {  | ||
| + |         //1、返回 IOC 容器  | ||
| + |         ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);  | ||
| + | |||
| + |         //2、查看容器里面的组件  | ||
| + |         String[] beanDefinitionNames = context.getBeanDefinitionNames();  | ||
| + |         Arrays.sort(beanDefinitionNames);  | ||
| + |         for (String name : beanDefinitionNames) {  | ||
| + |             System.out.println(name);  | ||
| + |         }  | ||
| + | |||
| + |         //3、从容器中获取组件  | ||
| + |         Pet tom01 = context.getBean("tom", Pet.class);  | ||
| + | |||
| + |         Pet tom02 = context.getBean("tom", Pet.class);  | ||
| + | |||
| + |         System.out.println("组件:" + (tom01 == tom02));  | ||
| + | |||
| + |         //4、io.github.jihch.boot.config.MyConfig$$EnhancerBySpringCGLIB$$f0fca2ff@26c89563  | ||
| + |         MyConfig bean = context.getBean(MyConfig.class);  | ||
| + | |||
| + |         System.out.println(bean);  | ||
| + | |||
| + |         //如果 @Configuration(proxyBeanMethods = true) 代理对象调用方法。  | ||
| + |         //SpringBoot 总会检查这个组件是否在容器中有,保持组件单实例  | ||
| + |         User user = bean.user01();  | ||
| + | |||
| + |         User user1 = bean.user01();  | ||
| + | |||
| + |         System.out.println(user == user1);  | ||
| + | |||
| + |         User user01 = context.getBean("user01", User.class);  | ||
| + | |||
| + |         Pet tom = context.getBean("tom", Pet.class);  | ||
| + | |||
| + |         System.out.println("用户的宠物:" + (user01.getPet() == tom));  | ||
| + | |||
| + |         //5、获取组件、验证 @Import 导入组件的功能  | ||
| + |         String[] beanNamesForType = context.getBeanNamesForType(User.class);  | ||
| + |         System.out.println("======");  | ||
| + |         for (String s:beanNamesForType) {  | ||
| + |             System.out.println(s);  | ||
| + |         }  | ||
| + | |||
| + |         DBHelper bean1 = context.getBean(DBHelper.class);  | ||
| + |         System.out.println(bean1);  | ||
| + | |||
| + |     }  | ||
| + | |||
| + | }  | ||
| + | </syntaxhighlight><syntaxhighlight lang="console">  | ||
| + | ……  | ||
| + | ======  | ||
| + | io.github.jihch.boot.bean.User  | ||
| + | user01  | ||
| + | ch.qos.logback.core.db.DBHelper@68ac9ec5  | ||
| + | </syntaxhighlight>  | ||
2023年2月12日 (日) 12:06的最新版本
https://www.bilibili.com/video/BV19K4y1L7MT/?p=9
声明组件的注解
@Bean
@Component 代表是一个组件
@Controller 代表是一个控制器
@Service 代表是一个业务逻辑组件
@Repository 代表是一个数据库层组件
@import
在容器中的组件上面使用 @import 注解
package io.github.jihch.boot.config;
import ch.qos.logback.core.db.DBHelper;
import io.github.jihch.boot.bean.Pet;
import io.github.jihch.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
 * 1、配置类里面使用 @Bean 标注在方法上给容器注册组件,默认也是单实例的
 * 2、配置类本身也是组件
 * 3、proxyBeanMethods:代理 bean 的方法
 *     Full(proxyBeanMethods = true)
 *     Lite(proxyBeanMethods = false)
 *     组件依赖
 *
 *  4、@Import({User.class, DBHelper.class})
 *     在容器中自动创建出这两个类型的组件
 */
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告诉 SpringBoot 这是一个配置类 == 配置文件
public class MyConfig {
    /**
     * 给容器中添加组件。
     * 以方法名作为组件的id。
     * 返回类型就是组件类型。
     * 返回的值,就是组件在容器中的实例
     *
     * 外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
     * @return
     */
    @Bean
    public User user01() {
        User zhangsan = new User("zhangsan", 18);
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }
    @Bean("tom")
    public Pet tomcatPet() {
        return new Pet("tomcat");
    }
}
修改 MainApplication,验证通过 @import 方式导入组件是否成功
@SpringBootApplication(scanBasePackages = "io.github.jihch")
public class MainApplication {
    public static void main(String[] args) {
        //1、返回 IOC 容器
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
        //2、查看容器里面的组件
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        Arrays.sort(beanDefinitionNames);
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
        //3、从容器中获取组件
        Pet tom01 = context.getBean("tom", Pet.class);
        Pet tom02 = context.getBean("tom", Pet.class);
        System.out.println("组件:" + (tom01 == tom02));
        //4、io.github.jihch.boot.config.MyConfig$$EnhancerBySpringCGLIB$$f0fca2ff@26c89563
        MyConfig bean = context.getBean(MyConfig.class);
        System.out.println(bean);
        //如果 @Configuration(proxyBeanMethods = true) 代理对象调用方法。
        //SpringBoot 总会检查这个组件是否在容器中有,保持组件单实例
        User user = bean.user01();
        User user1 = bean.user01();
        System.out.println(user == user1);
        User user01 = context.getBean("user01", User.class);
        Pet tom = context.getBean("tom", Pet.class);
        System.out.println("用户的宠物:" + (user01.getPet() == tom));
        //5、获取组件、验证 @Import 导入组件的功能
        String[] beanNamesForType = context.getBeanNamesForType(User.class);
        System.out.println("======");
        for (String s:beanNamesForType) {
            System.out.println(s);
        }
        DBHelper bean1 = context.getBean(DBHelper.class);
        System.out.println(bean1);
    }
}
……
======
io.github.jihch.boot.bean.User
user01
ch.qos.logback.core.db.DBHelper@68ac9ec5