作用:引用时,容器自动创建出这填写的类型的组件,默认组件的名字就是全类名。
往容器中添加两个组件
@Import({User.class, DBHelper.class})
测试:
System.out.println("=======测试import========"); String[] beanNamesForType = run.getBeanNamesForType(User.class); System.out.println("=============="); for(String s: beanNamesForType){ System.out.println(s); } DBHelper bean1 = run.getBean(DBHelper.class); System.out.println(bean1);
结果:
com.xbhog.pojo.User
user
ch.qos.logback.core.db.DBHelper@68565bc7
可以看出,使用import的组件返回名字时全类名,我们自己写的组件,就是原来的。
条件装配:满足Conditional指定的条件,则进行组件注入.
组件注解:
实例采用以下两个为例:
- @ConditionalOnMissingBean(name="tom11")
- @ConditionalOnBean(name="tom11")
ConditionalOnMissingBean表示当容器中没有这个tom11组件时,执行下面的组件
@Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) public class MyConfig { @ConditionalOnMissingBean(name="tom11") @Bean public User user(){ User user = new User("xbhog", 18); return user; } //@Bean("tom11") public Pet tomcat(){ return new Pet("tomcat"); } }
主启动程序中的代码片段:
ConfigurableApplicationContext run = SpringApplication.run(Myapp.class, args); //返回IOC容器 boolean tom11 = run.containsBean("tom11"); //容器中tom11组件:false //容器中user组件:true System.out.println("容器中tom11组件:"+tom11); boolean user = run.containsBean("user"); System.out.println("容器中user组件:"+user);
结果:
容器中tom11组件:false 容器中user组件:true
ConditionalOnBean表示当容器中存在该组件时,才执行下面的内容
@Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) public class MyConfig { @ConditionalOnBean(name="tom11") @Bean public User user(){ User user = new User("xbhog", 18); return user; } //@Bean("tom11") public Pet tomcat(){ return new Pet("tomcat"); } }
主程序启动代码:
ConfigurableApplicationContext run = SpringApplication.run(Myapp.class, args); //返回IOC容器 boolean tom11 = run.containsBean("tom11"); //容器中tom11组件:false //容器中user组件:true System.out.println("容器中tom11组件:"+tom11); boolean user = run.containsBean("user"); System.out.println("容器中user组件:"+user);
结果:
容器中tom11组件:false 容器中user组件:false
解决的问题:
将properties文件中的内容绑定到类的属性中,并把它们封装到JavaBean中,以供随时使用
有两种方法实现:
采用component+configurationProperties来绑定properties中的数据
properties数据:(brand:别摸我)
mycar.brand = BMW mycar.price = 200000
编写Car实体类:
package com.xbhog.popj; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @AllArgsConstructor //有参构造 @NoArgsConstructor //无参构造 @Component //将该类加入到容器中,来获得Spring Boot提供的强大的功能 @ConfigurationProperties(prefix="mycar") //实现读取绑定到properties中的以mycar前缀的数据 public class Car { private String brand; private String price; }
编写Controller:
package com.xbhog.controller; import com.xbhog.popj.Car; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @Autowired //自动匹配到容器中的car Car car; @RequestMapping("/car") public Car car(){ return car; } }
实现效果:
{"brand":"BMM","price":"200000"}
EnableConfigurationProperties+ConfigurationProperties
@EnableConfigurationProperties(类.class)
功能:
- 开启类配置绑定功能
- 把这个类这个组件自动注册到容器中
需要把EnableConfigurationProperties注解加到配置类中,因为配置类本身就是组件。
MyConfig:
@Configuration @EnableConfigurationProperties(Car.class) //开启Car配置绑定功能n并把Car自动注册到容器中 public class MyConfig { }
@ConfigurationProperties(prefix = “mycar”)
找到配置文件中application.properties中的前置为mycar的内容
实现效果:
{"brand":"BMM","price":"200000"}
使用@ImportResouce注解将bean.xml中的组件加入到容器中
bean.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="haha" class="com.xbhog.pojo.User"> <property name="name" value="zhangsan"></property> <property name="age" value="18"></property> </bean> <bean id="hehe" class="com.xbhog.pojo.Pet"> <property name="name" value="tomcat"></property> </bean> </beans>
package com.xbhog.springboot1times; import ch.qos.logback.core.db.DBHelper; import com.xbhog.config.MyConfig; import com.xbhog.pojo.Pet; import com.xbhog.pojo.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportResource; //@SpringBootApplication @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan("com.xbhog") @ImportResource("classpath:bean.xml") public class Myapp { public static void main(String[] args) { /*1. 返回我们IOC容器*/ ConfigurableApplicationContext run = SpringApplication.run(Myapp.class, args); boolean haha = run.containsBean("haha"); boolean hehe = run.containsBean("hehe"); System.out.println("是否存在:"+haha+"==="+hehe); } }
结果:是否存在:true===true
尚硅谷B站视频
如果你看到这里或者正好对你有所帮助,希望能点个关注或者推荐,感谢;
有错误的地方,欢迎在评论指出,作者看到会进行修改。