使用@Configuration注解来代替Spring的bean配置
下面是一个典型的Spring配置文件(application-config.xml):
<beans>
<bean id="orderService" class="com.acme.OrderService"/>
<constructor-arg ref="orderRepository"/>
</bean>
<bean id="orderRepository" class="com.acme.OrderRepository"/>
<constructor-arg ref="dataSource"/>
</bean>
</beans>
然后你就可以像这样来使用是bean了:
ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml");
OrderService orderService = (OrderService) ctx.getBean("orderService");
现在Spring Java Configuration这个项目提供了一种通过java代码来装配bean的方案:
@Configuration
public class ApplicationConfig { public @Bean OrderService orderService() {
return new OrderService(orderRepository());
} public @Bean OrderRepository orderRepository() {
return new OrderRepository(dataSource());
} public @Bean DataSource dataSource() {
// instantiate and return an new DataSource …
}
}
然后你就可以像这样来使用是bean了:
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class);
OrderService orderService = ctx.getBean(OrderService.class);
这么做有什么好处呢?
1.使用纯java代码,不在需要xml
2.在配置中也可享受OO带来的好处
3.类型安全对重构也能提供良好的支持
4.依旧能享受到所有springIoC容器提供的功能
使用@Configuration注解来代替Spring的bean配置的更多相关文章
- 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
[原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...
- spring中bean配置和bean注入
1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...
- spring中bean配置和注入场景分析
bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean,并 ...
- Spring 梳理-bean配置与装配
1 bean配置与装配 1.1 bean在XML文件中进行显示配置并装配 1.2 bean在JavaConfig中显示配置并装配 1.2.1 优点:类型是安全的,编译 ...
- 关于spring中bean配置的几件小事
一.IOC和DI 1.IOC(Inversion of Control) 其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源:而应用了IOC之后 ...
- Spring的Bean配置
IOC和DI 网上概念很多,感兴趣可以去搜一搜,在这里我就给个比喻: IOC:以前我们买东西都要去商店买,用了IOC之后,我们只要在门口放个箱子, Spring就会给我相应商品,ಠᴗಠ 举个例子 cl ...
- Spring的Java配置方式—@Configuration和@Bean实现Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...
- SpringBoot学习之@Configuration注解和@Bean注解
@Configuration 1.@Configuration注解底层是含有@Component ,所以@Configuration 具有和 @Component 的作用. 2.@Configurat ...
- Spring源码之@Configuration注解解析
1.前言 Spring注解开发中,我们只需求要类上加上@Configuration注解,然后在类中的方法上面加上@Bean注解即可完成Spring Bean组件的注册.相较于之前的xml配置文件定 ...
随机推荐
- Excel中COUNTIFS函数统计词频个数出现次数
Excel中COUNTIFS函数统计词频个数出现次数 在Excel中经常需要实现如下需求:在某一列单元格中有不同的词语,有些词语相同,有的不同(如图1所示).需要统计Excel表格中每个词语出现的 ...
- Linux 下安装mysql 链接库
1.mysql 客户端 开发 链接库 1.1)CentOS yum install mysql-devel
- POJ3903:Stock Exchange(LIS)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/E 题目: Description The world ...
- 字典的快速赋值 setValuesForKeysWithDictionary
字典的快速赋值 setValuesForKeysWithDictionary 前言 在学习解析数据的时候,我们经常是这么写的:PersonModel.h文件中 @property (nona ...
- [LintCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- django1.9 创建数据表
1.在setting.py 中注册app: 2.编写models.py 文件创建表结构: (生成的表默认是: app名称_定义的表面 ) 3.执行命令: python manage.py check ...
- struts2中用xml配置文件去验证填写信息
xml名字是这样的 actionName-validation.xml 每个action 对应一个xml文件 xml文件和action放在同一个包下 后台验证用户输入是否符合格式要求,不符合,提交后返 ...
- JAVA中JDBC连接数据库
这里列举了JDBC连接Oracle . SQLServer .MySQL 三种 数据库 1.Oracle连接(导入classes12.jar 包) public static Connection g ...
- thinkphp3.2设置session的过期时间
thinkPHP3.2中session的过期时间配置是不能使用的,我们需要修改一下它的配置文件thinkPHP>common>functions.php,找到这一行: if(isset($ ...
- 关于c++风格 code style
Header files should be self-contained. All header files should have #define guards to prevent multip ...