Spring注解@Configuration和Java Config
1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。但是,仍然允许使用经典的XML方式来定义bean和配置,JavaConfig是另一种替代解决方案。所以,在Spring3以后的版本中,支持xml方式和javaConfig两种Spring配置方式。
我们通过XML方式定义:

<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-3.0.xsd"> <bean id="helloBean" class="com.yiibai.hello.impl.HelloWorldImpl"> </beans>

等效于:

package com.yiibai.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.yiibai.hello.HelloWorld;
import com.yiibai.hello.impl.HelloWorldImpl; @Configuration
public class AppConfig { @Bean(name="helloBean")
public HelloWorld helloWorld() {
return new HelloWorldImpl();
} }

2.小例子:
a.一个简单的bean

package com.spring;
public interface Helloeorld {
void printHelloWorld(String msg);
}


package com.spring;
public class HelloworldImpl implements Helloeorld{
@Override
public void printHelloWorld(String msg) {
System.out.println("Hello : " + msg);
}
}

b.javaConfig注解:使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean

package com.spring; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /*使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,
并通过 @Bean 定义 bean*/
@Configuration
public class TestConfig { @Bean(name="helloeorld")
public Helloeorld getHello(){
return new HelloworldImpl(); }
}

c.测试main方法:

package com.spring; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestMain { public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
Helloeorld obj = (Helloeorld) context.getBean("helloeorld"); obj.printHelloWorld("Spring Java Config");
} }

3.@Import
在我们运用传统xml方式进行配置Spring时,可以通过如下方式加载多个配置文件:

在一个大的项目结构,Spring bean配置文件位于不同的文件夹以便于维护和模块化。例如,Spring-Common.xml在common 文件夹中,Spring-Connection.xml 在connection文件夹,Spring-ModuleA.xml在ModuleA 文件夹等等。
你可以加载多个Spring bean的配置文件如下代码中:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml",
"Spring-Connection.xml","Spring-ModuleA.xml"});
把所有的 Spring XML 文件放入在项目类路径中。
project-classpath/Spring-Common.xml
project-classpath/Spring-Connection.xml
project-classpath/Spring-ModuleA.xml
解决方法
以上方法是缺乏组织并且很容易出错,更好的办法应组织所有的Spring bean 配置文件到一个XML文件。例如,创建一个Spring-All-Module.xml文件,并导入整个Spring bean的文件如下:
File : Spring-All-Module.xml
<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-2.5.xsd"> <import resource="common/Spring-Common.xml"/>
<import resource="connection/Spring-Connection.xml"/>
<import resource="moduleA/Spring-ModuleA.xml"/>
</beans>
现在,可以加载一个这样的 XML 文件: ApplicationContext context = new ClassPathXmlApplicationContext(Spring-All-Module.xml);
将这个文件放入项目的类路径。
project-classpath/Spring-All-Module.xml

以上方式等效于javaConfig的如下方式:

1.两个简单的Spring bean。
File One : CustomerBo.java package com.yiibai.core; public class CustomerBo { public void printMsg(String msg) { System.out.println("CustomerBo : " + msg);
} } File Two: SchedulerBo.java package com.yiibai.core; public class SchedulerBo { public void printMsg(String msg) { System.out.println("SchedulerBo : " + msg);
} }
2. @Configuration示例 现在,使用JavaConfig @Configuration声明上述Bean类。
File One: CustomerConfig.java package com.yiibai.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.yiibai.core.CustomerBo; @Configuration
public class CustomerConfig { @Bean(name="customer")
public CustomerBo customerBo(){ return new CustomerBo(); }
} File Two: SchedulerConfig.java package com.yiibai.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.yiibai.core.SchedulerBo; @Configuration
public class SchedulerConfig { @Bean(name="scheduler")
public SchedulerBo suchedulerBo(){ return new SchedulerBo(); } }
3. @Import示例 使用@Import加载多个配置文件。
File : AppConfig.java package com.yiibai.config; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@Import({ CustomerConfig.class, SchedulerConfig.class })
public class AppConfig { }
4. 执行程序 加载主配置文件,并进行测试。
package com.yiibai.core; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.yiibai.config.AppConfig; public class App {
public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(
AppConfig.class); CustomerBo customer = (CustomerBo) context.getBean("customer");
customer.printMsg("Hello 11"); SchedulerBo scheduler = (SchedulerBo) context.getBean("scheduler");
scheduler.printMsg("Hello 22"); }
}
输出结果 CustomerBo : Hello 11
SchedulerBo : Hello 22

总结:Spring有xml和javaConfig两种配置方式,他们方式不同,但效果相同,可以根据自己的需要进行选择性配置。
Spring注解@Configuration和Java Config的更多相关文章
- Spring Security4实例(Java config版)——ajax登录,自定义验证
本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...
- Spring注解 @Configuration
Spring注解 @Configuration 一.@Configuration的作用 二.@Configuration的Spring容器启动方式 三.不加@Configuration的@Bean的解 ...
- Spring Security4实例(Java config 版) —— Remember-Me
本文源码请看这里 相关文章: Spring Security4实例(Java config版)--ajax登录,自定义验证 Spring Security提供了两种remember-me的实现,一种是 ...
- Spring 4 and MyBatis Java Config
TL;DR With the Java Config enhancements in Spring 4, you no longer need xml to configure MyBatis for ...
- [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...
- Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...
- Spring注解@Configuration是如何被处理的?
从SpringApplication开始 一般情况下启动SpringBoot都是新建一个类包含main方法,然后使用SpringApplication.run来启动程序: @SpringBootApp ...
- Spring Web工程web.xml零配置即使用Java Config + Annotation
摘要: 在Spring 3.0之前,我们工程中常用Bean都是通过XML形式的文件注解的,少了还可以,但是数量多,关系复杂到后期就很难维护了,所以在3.x之后Spring官方推荐使用Java Conf ...
- 你真的懂Spring Java Config 吗?Full @Configuration vs lite @Bean mode
Full @Configuration和lite @Bean mode 是 Spring Java Config 中两个非常有意思的概念. 先来看一下官方文档关于这两者的相关内容: The @Bean ...
随机推荐
- c++ cout、cin、endl
cout是标准输出流对象,<<是输出操作符:cin是标准输入流对象,>>是输入操作符:endl是换行符操作符.他们都属于C++标准库,所以都在std的名字空间里.所以要在开头写 ...
- 解决 Intellij IDEA Cannot Resolve Symbol ‘XXX’ 问题
1.java类报错 https://blog.csdn.net/qq_32040767/article/details/77096680 2.类对应的依赖没有加载进来.编译器自身的设置和缓存问题类. ...
- iOS抓包工具Charles
Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Charles:https://www.charlesproxy.com/download/ 2. HTTP ...
- 如何给框架添加API接口日志
前言 用的公司的框架,是MVC框架,看了下里面的日志基类,是操作日志,对增删改进行记录, 夸张的是一张业务的数据表 需要一张专门的日志表进行记录, 就是说你写个更新,添加的方法都必须写一遍操作日志,代 ...
- 在Django中使用ORM创建图书管理系统
一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...
- 舵机&数据处理&stm32内存之堆栈溢出(遇到的问题)
产品名称:TOWER PRO(辉盛)大扭力舵机MG996R (MG995升级产品)6v/11Kg厂家编号:MG996R产品净重: 55g产品尺寸: 40.7*19.7*42.9mm产品拉力: 9.4k ...
- Spring security oauth2 client_credentials认证 最简单示例代码
基于spring-boot-2.0.0 1,在pom.xml中添加: <!-- security --> <!-- https://mvnrepository.com/artifac ...
- zsh fg: no job control in this shell.
图片的上面就是将一个应用按Ctrl+Z,把任务放到后台里面.没法fg将任务回到前台运行. 在.zshrc中添加set -m. 具体原因不明.我切换到root用户里,没有出现这个问题.将我的.zshrc ...
- 来自多校的一个题——数位DP+卡位
n<=1e9就要考虑倍增.矩阵乘法这种了 假设L=0 考虑枚举二进制下,所有X与R的LCP长度,前len高位 对于第len+1位,假设R的这一位是1 如果一个x的这一位是0了,那么后面可以随便填 ...
- 休眠(1):sleep和wait的区别
1.这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类. 2.sleep是Thread的静态类方法,谁调用的谁去睡觉,即使在a线程里调用了b的sleep方法,实际 ...