大致的工程内容也来源于上一篇博文!

https://www.cnblogs.com/Mrchengs/p/10108603.html

1.@Lazy懒加载

懒加载:针对单实例的

单实例bean,默认在容器启动的时候创建对象

懒加载就是:容器启动的时候不创建对象,在第一次获取bean的时候创建对象,并且初始化、

Config2.class

@Configuration
public class Config2 {
@Bean("per")
public Person person(){
System.out.println("person对象创建完成");
return new Person("MrChengs",);
}
}

测试

     @Test
public void test2(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanDefinitionNames();
System.out.println("容器创建完成...");
}

此时并没有去获取Person对象!

此时容器创建之后就会实例化对象

person对象创建完成
容器创建完成...

使用懒加载:

@Configuration
public class Config2 {
@Lazy
@Bean("per")
public Person person(){
System.out.println("person对象创建完成");
return new Person("MrChengs",);
}
}

测试:

容器创建完成...

 2..@Conditional

按照一定的条件判断,满足条件给容器注册
可以在类上也可以在方法上
放置在类上,满足 条件,配置的所有的bean都会生效

假设在windows和Linux操作系统获取自动的注册信息

@Configuration
public class Config2 {
@Lazy
@Bean("per")
public Person person(){
System.out.println("person对象创建完成");
return new Person("MrChengs",);
} //按照一定的条件判断,满足条件给容器注册

@Conditional({LinuxCondition.class
})
@Bean("person1")
public Person person1(){
return new Person("Mr",);
} @Conditional({WindowsCondition.class})
@Bean("person2")
public Person person2(){
return new Person("Mx",);
}
}
public class LinuxCondition implements Condition{
//ConditionContext:判条件能使用的上下文
//AnnotatedTypeMetadata:注释信息
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //能获取到IOC容器使用的beanfactory
ConfigurableListableBeanFactory c = context.getBeanFactory();
//获取加载器
ClassLoader classLoader = context.getClassLoader();
//获取当前的环境信息
//Environment environment = (Environment) context.getEnvironment();
//获取bean定义注册的类
BeanDefinitionRegistry resistroy = context.getRegistry();

       //获取当前的操作系统
String name = context.getEnvironment().getProperty("os.name");
if(name.contains("Linux")){
return true;
}
return false;
}
}
public class WindowsCondition implements Condition{
//ConditionContext:判条件能使用的上下文
//AnnotatedTypeMetadata:注释信息
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //能获取到IOC容器使用的beanfactory
ConfigurableListableBeanFactory c = context.getBeanFactory();
//获取加载器
ClassLoader classLoader = context.getClassLoader();
//获取当前的环境信息
//Environment environment = (Environment) context.getEnvironment();
//获取bean定义注册的类
BeanDefinitionRegistry resistroy = context.getRegistry(); String name = context.getEnvironment().getProperty("os.name");
if(name.contains("Win")){
return true;
}
return false;
}
}
     @Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanNamesForType(Person.class);
for(String name : names){
System.out.println(name);
}
System.out.println("容器创建完成...");
}
per
person2
容器创建完成...

3.@import

快速的给容器中导入一个组建
默认创建的是bean是组建的全类名
@Configuration
@Import(Person.class)
public class Config2 { }
     @Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanNamesForType(Person.class);
for(String name : names){
System.out.println(name);
}
System.out.println("容器创建完成...");
}
coom.MrChengs.bean.Person
容器创建完成...

源码:

public @interface Import {
/**
* {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
* or regular component classes to import.
*/
Class<?>
[] value();
}

可以同时传入多个

@Import({Person.class,xxxxx})

直接注入到容器中

ImportSelector

是一个接口

public class MyImportSelector implements ImportSelector{
//返回值就是需要导入到容器中的全类名
//AnnotationMetadata : 就是获取标注@Import()注解类的所有信息

public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{"coom.MrChengs.bean.Person"};
}
}
@Configuration
@Import({MyImportSelector.class})
public class Config2 {
}
@Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
System.out.println("容器创建完成...");
}
config2
coom.MrChengs.bean.Person
容器创建完成...

ImportBeanDefinitionRegistrar接口

public class MyImportBeanDefinitionRegistrar implements  ImportBeanDefinitionRegistrar {
//AnnotationMetadata:当前类的注解信息
//BeanDefinitionRegistry:注册类

public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { //coom.MrChengs.bean.Person
//判断当前类是否有下面的这个bean
boolean a = registry.containsBeanDefinition("coom.MrChengs.bean.Person");
//如果没有我们进行注册
if(!a){
RootBeanDefinition beanDefinition = new RootBeanDefinition(Person.class);
//指定bean的名字
//注册一个bean
registry.registerBeanDefinition("peson", beanDefinition );
}
}
}
@Configuration
@Import({MyImportBeanDefinitionRegistrar.class})
public class Config2 {
}
@Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
System.out.println("容器创建完成...");
}
config2
peson
容器创建完成...

4.FactoryBean接口

public class PersonFactoryBean implements FactoryBean<Person>{

     //返回一个person对象,这个对象会添加到容器中
public Person getObject() throws Exception {
// TODO Auto-generated method stub
return new Person();
} //返回类型
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Person.class;
} //是单例吗?
public boolean isSingleton() {
// TODO Auto-generated method stub
return false;
}
}
@Configuration
public class Config2 { @Bean
public PersonFactoryBean personFactoryBean(){
return new
PersonFactoryBean();
}

}
     @Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
Object name = app.getBean("personFactoryBean").getClass();
System.out.println(name);
System.out.println("容器创建完成...");
}
config2
personFactoryBean
class coom.MrChengs.bean.Person -> Object name = app.getBean("personFactoryBean").getClass();
容器创建完成...

得到的是Person对象

默认得到的是getObject的对象
 
给id前面加一个& 符号得到的是对象本身
Object name = app.getBean("&personFactoryBean").getClass();
class coom.MrChengs.bean.PersonFactoryBean

2spring注解:@Lazy,@Conditional,@import,FactoryBean接口的更多相关文章

  1. 二、Spring注解之@Conditional

    Spring注解之@Conditional [1]@Conditional介绍 ​ @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean. ...

  2. Springboot + redis + 注解 + 拦截器来实现接口幂等性校验

    Springboot + redis + 注解 + 拦截器来实现接口幂等性校验   1. SpringBoot 整合篇 2. 手写一套迷你版HTTP服务器 3. 记住:永远不要在MySQL中使用UTF ...

  3. 使用AOP+自定义注解完成spring boot的接口权限校验

    记使用AOP+自定义注解完成接口的权限校验,代码如下: pom文件添加所需依赖: 1 <dependency> 2 <groupId>org.aspectj</group ...

  4. Spring之BeanFactory和FactoryBean接口的区别

    目录 一.BeanFactory接口 二.FactoryBean接口 1.简单实现 2.增强实现 3.FactoryBean的实际使用案例 三.总结 @   Spring框架中的BeanFactory ...

  5. java反射之获取所有方法及其注解(包括实现的接口上的注解),获取各种标识符备忘

    java反射之获取类或接口上的所有方法及其注解(包括实现的接口上的注解) /** * 获取类或接口上的所有方法及方法上的注解(包括方法实现上的注解以及接口上的注解),最完整的工具类,没有现成的工具类 ...

  6. Spring5源码深度分析(二)之理解@Conditional,@Import注解

    代码地址: 1.源码分析二主要分析的内容 1.使用@Condition多条件注册bean对象2.@Import注解快速注入第三方bean对象3.@EnableXXXX 开启原理4.基于ImportBe ...

  7. java反射注解妙用-获取所有接口说明

    转载请注明出处:https://www.cnblogs.com/wenjunwei/p/10293490.html 前言 最近在做项目权限,使用shiro实现restful接口权限管理,对整个项目都进 ...

  8. WebService之CXF注解之二(Service接口)

    ITeacherService.java: /** * @Title:ITeacherService.java * @Package:com.you.service * @Description:教师 ...

  9. spring注解第05课 FactoryBean

    1.工厂bean调用 @Configuration public class MainConfig2 {/** * 使用Spring提供的 FactoryBean(工厂Bean); * 1).默认获取 ...

随机推荐

  1. [转]象棋AI算法(一)

    本文转自:http://blog.csdn.net/u012723995/article/details/47133693 参考文献:http://www.xqbase.com/computer/se ...

  2. java 线程池(1)

    问题 : 线程池中的 coreSize 和 maxSize 的作用分别是什么? 未执行的线程池存在在哪种数据类型,为什么使用这种类型的数据结构 ThreadPoolExecutor概述 ThreadP ...

  3. Cheatsheet: 2017 03.01 ~ 03.31

    Web New Year, New Blog Day 10 - Using JetBrains Rider with a .NET Core Console Application JavaScrip ...

  4. 在grid结果集中实现全选或全不选某些特定的行

    在script的中的代码如下: function check(){ var id = gridgetselectvalue("require_id"); if(id.length& ...

  5. java 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出

    import java.io.FileInputStream; import java.util.Properties; import java.sql.Connection; import java ...

  6. oracle中,改变表名和字段名的大小写

    1.将表名和字段名改为大写  见--http://www.cnblogs.com/wenboge/articles/4121331.html 2.将表名和字段名改为小写 ①改表名为小写 begin f ...

  7. [翻译]Review——The Inner Workings Of Virtual DOM

    The Inner Workings Of Virtual DOM 虚拟DOM的内部工作机制 原文地址:https://medium.com/@rajaraodv/the-inner-workings ...

  8. JS里的居民们7-对象和数组转换

    编码 学习通用的数据用不同的数据结构进行存储,以及相互的转换 对象转为数组: var scoreObject = { "Tony": { "Math": 95, ...

  9. C语言——顺序表插入、删除、定位运算算法

    说明:将元素x插入到顺序表L的第i个数据元素之前,这个i是从1开始的,但是程序中数组都是从0算起的,不要混淆了. 头文件: header.h // 顺序表的结构定义 #define Maxsize 1 ...

  10. 24_BlockingQueue接口

    [BlockingQueue常见] [ ArrayBlockingQueue ] 基于数组的阻塞队列的实现,在ArrayBlockingQueue内部,维护了一个定长数组,以便缓存队列中的数据对象,其 ...