[SpringBoot2]容器功能_底层注解&配置绑定_@Configuration&@Import&@Conditional&@ImportResource&
Posted 唐火
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[SpringBoot2]容器功能_底层注解&配置绑定_@Configuration&@Import&@Conditional&@ImportResource&相关的知识,希望对你有一定的参考价值。
@Configuration&@Bean
- 告诉SpringBoot这是一个配置类==配置文件
#############################Configuration使用示例######################################################
/**
* 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
* 2、配置类本身也是组件
* 3、proxyBeanMethods:代理bean的方法
* Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
* Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
* 组件依赖必须使用Full模式默认。其他默认是否Lite模式
*
*
*
*/
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig
/**
* Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
* @return
*/
@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01()
User zhangsan = new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(tomcatPet());
return zhangsan;
@Bean("tom")
public Pet tomcatPet()
return new Pet("tomcat");
################################@Configuration测试代码如下########################################
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
public class MainApplication
public static void main(String[] args)
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names)
System.out.println(name);
//3、从容器中获取组件
Pet tom01 = run.getBean("tom", Pet.class);
Pet tom02 = run.getBean("tom", Pet.class);
System.out.println("组件:"+(tom01 == tom02));
//true 组件默认是单实例的
//4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892
MyConfig bean = run.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 = run.getBean("user01", User.class);
Pet tom = run.getBean("tom", Pet.class);
System.out.println("用户的宠物:"+(user01.getPet() == tom));
User user01 = run.getBean("user01", User.class);
Pet tom = run.getBean("tom", Pet.class);
System.out.println("用户的宠物:"+(user01.getPet() == tom));
当proxyBeanMethods = true时,用户中的宠物就是容器中的宠物,当
proxyBeanMethods = false时,用户中的宠物不是容器中的宠物
@Import
* 4、@Import(User.class, DBHelper.class)
* 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
*
*
*
*/
@Import(User.class, DBHelper.class)
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig
测试:
@SpringBootApplication
public class MainApplication
public static void main(String[] args)
//1.返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2.查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name :names)
System.out.println(name);
//获取组件
System.out.println("=============================================");
String[] beanNamesForType = run.getBeanNamesForType(User.class);
for (String s : beanNamesForType)
System.out.println(s);
DBHelper bean1 = run.getBean(DBHelper.class);
System.out.println(bean1);
@Conditional
条件装配:满足Conditional指定的条件,则进行组件注入
=====================测试条件装配==========================
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
//@ConditionalOnBean:在容器中有某个组件
//@ConditionalOnBean(name = "tom")
@ConditionalOnMissingBean(name = "tom")
public class MyConfig
/**
* Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
* @return
*/
@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01()
User zhangsan = new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(tomcatPet());
return zhangsan;
@Bean("tom22")
public Pet tomcatPet()
return new Pet("tomcat");
public static void main(String[] args)
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names)
System.out.println(name);
boolean tom = run.containsBean("tom");
System.out.println("容器中Tom组件:"+tom);
boolean user01 = run.containsBean("user01");
System.out.println("容器中user01组件:"+user01);
boolean tom22 = run.containsBean("tom22");
System.out.println("容器中tom22组件:"+tom22);
举例:
@ConditionalOnBean(name = "tom")
@Bean("tom22")
public Pet tomcatPet()
return new Pet("tomcat");
表示当容器中有组件tom的时候,才会创建组件tom22到容器中
@ConditionalOnMissingBean(name = "tom")
public class MyConfig
//........
表示当容器中没有组件tom的时候,才会创建Myconfig中的那些组件到容器中
@ImportResource
- 原生配置文件引入
======================beans.xml=========================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="haha" class="com.atguigu.boot.bean.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
<bean id="hehe" class="com.atguigu.boot.bean.Pet">
<property name="name" value="tomcat"></property>
</bean>
</beans>
@ImportResource("classpath:beans.xml")
public class MyConfig
======================测试=================
boolean haha = run.containsBean("haha");
boolean hehe = run.containsBean("hehe");
System.out.println("haha:"+haha);//true
System.out.println("hehe:"+hehe);//true
配置绑定
如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用;
public class getProperties
public static void main(String[] args) throws FileNotFoundException, IOException
Properties pps = new Properties();
pps.load(new FileInputStream("a.properties"));
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements())
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
//封装到JavaBean。
@Component + @ConfigurationProperties
/**
* 只有在容器中的组件,才会拥有SpringBoot提供的强大功能
*/
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car
private String brand;
private Integer price;
public String getBrand()
return brand;
public void setBrand(String brand)
this.brand = brand;
public Integer getPrice()
return price;
public void setPrice(Integer price)
this.price = price;
@Override
public String toString()
return "Car" +
"brand='" + brand + '\\'' +
", price=" + price +
'';
application.properties:
mycar.brand=BYD
mycar.price=10000000
测试:
@RestController
public class HelloController
@Autowired
Car car;
@RequestMapping("/car")
public Car car()
return car;
@EnableConfigurationProperties + @ConfigurationProperties
@Configuration//告诉SpringBoot这是一个配置类==配置文件
@EnableConfigurationProperties(Car.class)
//1.开启Car配置绑定功能
//2.把这个Car这个组件自动注册到容器中
public class MyConfig
//.......
@ConfigurationProperties(prefix = "mycar")
public class Car
private String brand;
private Integer price;
public String getBrand()
return brand;
public void setBrand(String brand)
this.brand = brand;
public Integer getPrice()
return price;
public void setPrice(Integer price)
this.price = price;
@Override
public String toString()
return "Car" +
"brand='" + brand + '\\'' +
", price=" + price +
'';
- 只用一个@ConfigurationProperties是不行的!
以上是关于[SpringBoot2]容器功能_底层注解&配置绑定_@Configuration&@Import&@Conditional&@ImportResource&的主要内容,如果未能解决你的问题,请参考以下文章
[SpringBoot2]原生组件注入_原生注解与Spring方式注入