参考:https://www.jianshu.com/p/8e3de55d4373
(1)@Configration标签
表示这个类可被Spring识别的配置对象的类,只有有这个标记的标签的类才能使用@Bean标签作用于对应的方法上面。
(2)@Bean标签
@Bean(destroyMethod = "destory", initMethod = "init")也可以通过这样的写法来配置bean的初始化方法和销毁方法。
使用示例:
package edu.xatu.cn;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// 作为Spring的主配置文件
// 这个标签表示这个类可被Spring识别的配置对象的类,只有有这个标记的标签的类才能使用
// @Bean标签作用于对应的方法上面
@Configuration
public class AppConfig {
//告诉Spring这个方法是Spring帮我们管理的bean通过@Bean标签
@Bean
public BeanTest getBeanTest(){
return new BeanTest();
}
@Bean
public BeanTest2 getBeanTest2(){
return new BeanTest2();
}
}
原来创建bean对象的方法:
1.配置文件application.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="BeanTest" class="edu.xatu.cn.BeanTest"></bean>
</beans>
2.读取配置文件:
//基于Spring容器生成对象
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
BeanTest bean = context.getBean(BeanTest.class);
System.out.println(bean);
}
现在使用Configration注解方法:AppConfig.java上面已经给出
//基于AnnotationConfigApplicationContext来配置
@Test
public void test2(){
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
BeanTest bean1 = context.getBean(BeanTest.class);
BeanTest2 bean2 = context.getBean(BeanTest2.class);
System.out.println(bean1);
System.out.println(bean2);
}
(3)@Component,@ComponentScan标签
@ComponentScan 开启组件自动扫描:默认情况下,它会扫描当前类所在的包及其子包中的所有标签对象加载到spring容器
(4)对象的关系引用:
BeanTest1中依赖一个BeanTest2
测试类MyTest.java:
//对象的关系引用
@Autowired
@Qualifier("bean1")
private BeanTest bean1;
@Test
public void test3(){
System.out.println(bean1.getBeanTest2());
}
配置文件类AppConfig.java
@Bean(destroyMethod = "destory", initMethod = "init")
public BeanTest beanTest(BeanTest2 bean2){
BeanTest bean1 = new BeanTest();
bean1.setOtherBean(bean2);
return bean1;
}
使用try来关闭容器(MyTest.java)
// 使用try来关闭容器
@Test
public void test4(){
try(AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class)){
BeanTest bean1 = context.getBean(BeanTest.class,"bean1");
System.out.println(bean1);
};
}
(5)@import标签
用来导入配置类,主配置文件AppConfig导入其他配置文件类
其他两个配置文件:
@Configuration
public class ConfigRedis {
@Bean
public MyRedisTemplate myRedisTemplate(){
return new MyRedisTemplate();
}
}
@Configuration
public class ConfigDataSource {
@Bean
public MyDataSource myDataSource(){
return new MyDataSource();
}
}
测试:
(6)@importresource标签在javaConfig中混用xml,导入资源
配置文件配置了BeanTest2对象
BeanTest1中有BeanTest2的引用
在主配置文件AppConfig.java中使用@importresource标签导入资源文件
测试MyTest.java:
(7)引入外部资源.properties资源文件
属性文件以及实体类
主配置文件使用propertysource标签
测试:
第二种方式:
AppConfig.java
属性文件以及实体类
配置类
主配置导入其他两个配置文件
测试: