一、@configuration注解
1、基本使用
自定义配置类
/**
* 1、@configuration 告诉springboot这是一个配置类,相当于一个xml配置文件
*
* 2、配置类里面使用 @bean 标注在方法上 来给容器注册组件,默认是单实例的
*
* 3、配置类本身也是一个组件
*/
@configuration(proxybeanmethods = true)
public class myconfig {
@bean
public user user01(){
return new user("zhangsan",23);
}
@bean
public pet pet01(){
return new pet("cat");
}
}
主程序类
/**
* 主程序类
* @springbootapplication:这是一个springboot应用
*/
@springbootapplication
public class mainapplication {
public static void main(string[] args) {
//返回ioc容器
configurableapplicationcontext context = springapplication.run(mainapplication.class, args);
//从容器中获取bean
user user = context.getbean(user.class);
system.out.println(user);
pet pet = context.getbean("pet01", pet.class);
system.out.println(pet);
myconfig myconfig = context.getbean(myconfig.class);
system.out.println(myconfig);
/*如果配置@configuration(proxybeanmethods = true),代理对象调用方法从容器中拿组件,springboot总会检查容器中是否有这个组件
* 保持组件的单实例*/
user user1 = myconfig.user01();
user user2 = myconfig.user01();
system.out.println(user1 == user2);
}
}
打印结果:

2、full模式与lite模式
full模式是指proxybeanmethods = true,开启代理bean的方法。此时调用配置类中每一个给而容器注册组件方法,都会从容器中找组件,保持单例模式。
/*如果配置@configuration(proxybeanmethods = true),代理对象调用方法从容器中拿组件,springboot总会检查容器中是否有这个组件
* 保持组件的单实例*/
user user1 = myconfig.user01();
user user2 = myconfig.user01();
system.out.println(user1 == user2); //true
lite模式是指proxybeanmethods = false,关闭代理bean的方法。容器中不会保存代理对象,每一次调用配置类里面的方法,·都会产生一个新的对象。这可以解决组件依赖的问题。
user组件里面有pet组件
public class user {
private string name;
private integer age;
private pet pet;
public pet getpet() {
return pet;
}
}
自定义的配置类中proxybeanmethods设置为false
@configuration(proxybeanmethods = false)
public class myconfig {
@bean
public user user01(){
user user = new user("zhangsan", 23);
//user组件依赖了pet组件
user.setpet(pet01());
return user;
}
@bean
public pet pet01(){
return new pet("cat");
}
}
主程序类
@springbootapplication
public class mainapplication {
public static void main(string[] args) {
user user01 = context.getbean("user01", user.class);
pet pet01 = context.getbean("pet01", pet.class);
system.out.println(user01.getpet() == pet01); //true
}
}
打印结果:

- 配置类组件之间无依赖关系用
lite模式加速容器启动过程,减少判断 - 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,这时要用
full模式
二、@import注解导入组件
@import注解可以给容器中自动创建出指定类型的组件,默认组件的名字就是全类名
@import(dbhelper.class)
@configuration(proxybeanmethods = false)
public class myconfig {
}
在容器中或者这个导入的组件

三、@conditional注解条件装配
满足conditional指定的条件的方法,则进行组件注入


也可以标注在类上,当容器中存在指定的组件的时候,配置类中的方法才会生效

四、@importresource注解导入spring配置文件
外部配置文件

导入外部配置文件

获取导入的外部组件

五、@configurationproperties注解配置绑定
javabean与配置文件中属性进行绑定
需要使用@configurationproperties 和 @component两个注解,@component注解将组件注册到容器中,因为只有在容器中的组件,才能使用springboot提供的一些强大的注解功能

访问请求获取绑定的javabean

还可以在配置类中使用@enableconfigurationproperties开启组件属性配置功能,并把这个组件自动注册到容器中,这样就不需要再使用 @component注解
@enableconfigurationproperties(pet.class)
public class myconfig {
}
//@component
@configurationproperties(prefix = "mydog")
public class pet {
}
依然可以访问请求获取绑定的javabean

到此这篇关于详解如何实现springboot的底层注解的文章就介绍到这了,更多相关springboot底层注解内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
进退两男