Spring @Configuration详解
@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
注意:@Configuration注解的配置类有如下要求:
- @Configuration不可以是final类型;
- @Configuration不可以是匿名类;
- 嵌套的configuration必须是静态类。
一、@Configuation加载Spring方法
1.1、@Configuration配置spring并启动spring容器
@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>
,作用为:配置spring容器(应用上下文)
package com.ahies.ija.management.util;/*** @Description TODO* @Date 2019/3/8 9:59* @Author zsj*/
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.awt.*;
import java.io.File;@Configuration
public class TestConfiguration {public TestConfiguration() {System.out.println("TestConfiguration容器启动初始化。。。");}}
测试方法
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestMain {public static void main(String[] args) {// @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContextApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);// 如果加载spring-context.xml文件:// ApplicationContext context = new// ClassPathXmlApplicationContext("spring-context.xml");}
}
1.2、@Configuration启动容器+@Bean注册Bean,@Bean下管理bean的生命周期
@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的<bean>
,作用为:注册bean对象
bean类:
package com.ahies.ija.management.util;/*** @Description TODO* @Date 2019/3/8 10:00* @Author zsj*/public class TestBean {private String username;private String url;private String password;public void sayHello() {System.out.println("TestBean sayHello...");}public String toString() {return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;}public void start() {System.out.println("TestBean 初始化。。。");}public void cleanUp() {System.out.println("TestBean 销毁。。。");}
}
配置类:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;@Configuration
public class TestConfiguration {public TestConfiguration() {System.out.println("TestConfiguration容器启动初始化。。。");}// @Bean注解注册bean,同时可以指定初始化和销毁方法// @Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")@Bean@Scope("prototype")public TestBean testBean() {return new TestBean();}
}
主方法测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestMain {public static void main(String[] args) {// @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContextApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);// 如果加载spring-context.xml文件:// ApplicationContext context = new// ClassPathXmlApplicationContext("spring-context.xml");//获取beanTestBean tb = (TestBean) context.getBean("testBean");tb.sayHello();}
}
1.3、@Configuration启动容器+@Component注册Bean
bean类:
import org.springframework.stereotype.Component;//添加注册bean的注解
@Component
public class TestBean {private String username;private String url;private String password;public void sayHello() {System.out.println("TestBean sayHello...");}public String toString() {return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;}public void start() {System.out.println("TestBean 初始化。。。");}public void cleanUp() {System.out.println("TestBean 销毁。。。");}
}
配置类:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;@Configuration
//添加自动扫描注解,basePackages为TestBean包路径
@ComponentScan(basePackages = "com.ahies.ija.management")
public class TestConfiguration {public TestConfiguration() {System.out.println("TestConfiguration容器启动初始化。。。");}}
主方法测试获取bean对象:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestMain {public static void main(String[] args) {// @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContextApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);// 如果加载spring-context.xml文件:// ApplicationContext context = new// ClassPathXmlApplicationContext("spring-context.xml");//获取beanTestBean tb = (TestBean) context.getBean("testBean");tb.sayHello();}
}
1.6、@Configuation总结
@Configuation等价于<Beans></Beans>
@Bean等价于<Bean></Bean>
@ComponentScan等价于<context:component-scan base-package="com.ahies.ija.management"/>
二、组合多个配置类
2.1、在@configuration中引入spring的xml配置文件
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;@Configuration
@ImportResource("classpath:applicationContext-configuration.xml")
public class WebConfig {
}
bean类:
public class TestBean2 {private String username;private String url;private String password;public void sayHello() {System.out.println("TestBean2 sayHello...");}public String toString() {return "TestBean2 username:" + this.username + ",url:" + this.url + ",password:" + this.password;}public void start() {System.out.println("TestBean2 初始化。。。");}public void cleanUp() {System.out.println("TestBean2 销毁。。。");}
}
测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestMain2 {public static void main(String[] args) {// @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContextApplicationContext context = new AnnotationConfigApplicationContext(WebConfig.class);// 如果加载spring-context.xml文件:// ApplicationContext context = new// ClassPathXmlApplicationContext("spring-context.xml");// 获取beanTestBean2 tb = (TestBean2) context.getBean("testBean2");tb.sayHello();}
}
2.2、在@configuration中引入其它注解配置
package com.ahies.ija.management.util;/*** @Description TODO* @Date 2019/3/8 13:04* @Author zsj*/
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;@Configuration
//@ImportResource("classpath:applicationContext-configuration.xml")
@Import(TestConfiguration.class)
public class WebConfig {
}
测试类:
package com.ahies.ija.management.util;/*** @Description TODO* @Date 2019/3/8 13:05* @Author zsj*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestMain2 {public static void main(String[] args) {// @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContextApplicationContext context = new AnnotationConfigApplicationContext(WebConfig.class);// 如果加载spring-context.xml文件:// ApplicationContext context = new// ClassPathXmlApplicationContext("spring-context.xml");// 获取beanTestBean tb = (TestBean) context.getBean("testBean");tb.sayHello();}
}
2.3、@configuration嵌套(嵌套的Configuration必须是静态类)
通过配置类嵌套的配置类,达到组合多个配置类的目的。但注意内部类必须是静态类。
上代码:
package com.ahies.ija.management.util;/*** @Description TODO* @Date 2019/3/8 9:59* @Author zsj*/
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.awt.*;
import java.io.File;@Configuration
public class TestConfiguration {public TestConfiguration() {System.out.println("TestConfiguration容器启动初始化。。。");}// @Bean注解注册bean,同时可以指定初始化和销毁方法@Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
// @Beanpublic TestBean testBean() {return new TestBean();}@Configurationstatic class DatabaseConfig {@BeanFile file() {return new File("/");}}
}
启动类
package com.ahies.ija.management.util;/*** @Description TODO* @Date 2019/3/8 9:59* @Author zsj*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.io.File;public class TestMain {public static void main(String[] args) {// @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContextApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);// 如果加载spring-context.xml文件:// ApplicationContext context = new// ClassPathXmlApplicationContext("spring-context.xml");//获取beanTestBean tb = (TestBean) context.getBean("testBean");tb.sayHello();File ds = (File) context.getBean("file");System.out.println(ds);}
}
Spring @Configuration详解相关推荐
- Spring JDBC详解
<Spring JDBC详解> 本文旨在讲述Spring JDBC模块的用法.Spring JDBC模块是Spring框架的基础模块之一. 一.概述 在Spring JDBC模块中,所有的 ...
- struts2+hibernate+spring配置详解
#struts2+hibernate+spring配置详解 struts2+hibernate+spring配置详解 哎 ,当初一个人做好难,现在终于弄好了,希望自学这个的能少走些弯路. 以下是自己配 ...
- spring注解详解与用法(总览)
这篇文章收集了我写的所有的spring注解的详细说明与用法,点击可以跳转到对应文章,此文章会不断更新 spring注解详解与用法(1)最基础也是最常见的如下所示,详情点击这里 @Controller/ ...
- Spring入门详解
typora-copy-images-to: upload Spring入门详解 Spring框架是Java开发中最常用的框架,功能非常强大 源码下载:Spring Framework jar包.文档 ...
- Spring IoC详解
Spring IoC详解 原文地址:Spring IoC详解 写在最前 本文将主要写Spring最核心的部分,为什么写这篇的原因也是因为在刚开始学习Spring的时候,学得太粗糙了.感觉学了个皮毛,从 ...
- @Configuration详解
@Configuration详解,功能:将想要的组件添加到容器中 首先,需要准备三个文件: ps:完整代码片在文章最后,建议看完教程. 1.主程序类: 2.宠物类 3.用户类 对比以前原生spring ...
- Spring AOP详解(转载)所需要的包
上一篇文章中,<Spring Aop详解(转载)>里的代码都可以运行,只是包比较多,中间缺少了几个相应的包,根据报错,几经百度搜索,终于补全了所有包. 截图如下: 在主测试类里面,有人怀疑 ...
- Spring 体系结构详解
Spring 体系结构详解 核心容器(Core Container) Core和Beans模块提供了Spring最基础的功能,提供IOC和依赖注入特性.这里的基础概念是BeanFactory,它提供对 ...
- [转载]Spring配置文件详解一:
2019独角兽企业重金招聘Python工程师标准>>> 原文地址:Spring配置文件详解一:<context:annotation-config/>与<conte ...
最新文章
- React React-Redux的安装,使用
- 2G---5G与未来天线技术
- Spring MVC报异常:org.springframework.web.util.NestedServletException: Request processing failed
- JavaScript——易班优课YOOC课群在线测试自动答题解决方案(四)答案显示
- Leetcode 53 最大子串和
- 扬尘监测系统_工地扬尘监测_工地扬尘监测解决方案
- 在你们看来,信息网络的定级如何才能更好的发展?或者说有什么影响因素使得定级工作不好开展?
- 动态调整linux分区大小,GParted 动态调整Linux分区大小
- 邹欣对话图灵奖得主Jeffrey Ullman:数据库不会进入周期性的坏循环|《新程序员》...
- JAVA 使用Dom4j 解析XML
- tar打包命令(linux)
- 微服务项目之电商4.0技术架构图
- onenote无法打开链接出现错误您的组织策略阻止我们为您完成此操作
- 微信支付:商户订单号重复
- matlab提取wind底层数据库操作
- 分析早期关节炎队列发现冬春季发病者的短期放射学进展更重
- Python Iterator 学习小记
- 使用 * 打印等腰三角形
- k8s利用deployment部署pod,以及应用更新和回滚操作
- 让机器人组装Ikea家具是怎样的体验?