Spring JavaConfig
以前,Spring推荐使用XML的方式来定义Bean及Bean之间的装配规则,但是在Spring3.0之后,Spring提出的强大的JavaConfig这种类型安全的Bean装配方式,它基于Java代码的灵活性,使得装配的过程也变得及其灵活。
@Configuration注解
我们在定义JavaConfig类时,都会在其上加注@Configuration注解,来表明这是一个配置类,@Configuration注解底层是@Component注解,而且这个注解会被AnnotationConfigApplicationContext来进行加载,AnnotationConfigApplicationContext是ApplicationContext的一个具体实现,代表依据配置注解启动应用上下文。
@ComponentScan注解
我们使用JavaConfig的目的是为了实现以前XML配置实现的功能,首先就是组件扫描功能,将我们使用特定注解标注的类统一扫描加载到Spring容器,这一功能就是依靠@ComponentScan注解来实现的,我们可以为其指定位置参数来指定要扫描的包。
@Bean注解
使用@Bean注解我们可以实现XML配置中手动配置第三方Bean的功能,这里我们使用方法来定义Bean,并在方法前面加注@Bean注解,表示要将该方法返回的对象加载到Spring容器中,这样就对我们的方法定义带来了一些限制,这些限制包括方法的大概格式:
1-方法带返回值,且返回类型为你要加载的第三方类类型
2-方法的名称为默认的Bean的name,如果要自定义Bean的name,可以使用@Bean注解的name属性。
3-要实现注入只需要将要注入的Bean的类型作为参数,调用该类的带参数的构造器构建这个Bean,或者采用第二种方式:先创建这个类的对象,然后调用该对象的set方法进行注入,以被注入的Bean的方法为参数
JavaConfig能够等价看成是XML文件,不过它只是用Java编写的。它的完整文档在网上可以看到,而这篇文章帮助你开始使用JavaConfig。让我们把下面的XML文件移植为一个JavaConfig来作为一个例子吧:
<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();
} }
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");
} }
Spring JavaConfig的更多相关文章
- Spring JavaConfig实例
从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中. 但是,仍然允许使用经典的XML方式来定义bean ...
- Spring重温(二)--Spring JavaConfig
1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...
- Spring JavaConfig @Import实例
一般来说, 需要按模块或类别 分割Spring XML bean文件 成多个小文件, 使事情更容易维护和模块化. 例如, <beans xmlns="http://www.spring ...
- RabbitMQ基础教程之Spring&JavaConfig使用篇
RabbitMQ基础教程之Spring使用篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 RabbitMQ基础 ...
- spring javaconfig druidsource
package dataConfig; import java.sql.SQLException; import org.springframework.context.annotation.Bean ...
- Spring基础知识
Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...
- 译:Spring框架参考文档之IoC容器(未完成)
6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...
- Spring IOC之Classpath扫描和管理的组件
在前面的大部分例子我们使用XML去指明配置数据去定义在Spring容器中的每一个BeanDefinition.上一节我们展示了如何在 代码层注解的方式来提供大量的配置信息.即使在这些例子中,但是,基础 ...
- Spring IOC之容器概述
1.SpringIOC容器和beans介绍 IOC的依赖注入是这样的,对象定义他们的依赖也就是他们需要在一起起作用的对象是通过构造器参数以及工厂方法的参数或者是当他们被构建或者是从工厂中返回时设置在对 ...
随机推荐
- jquery 和 FormData 最简单图片异步上传
<script src="/scripts/jquery/jquery-3.1.1.min.js"></script> <script type=&q ...
- LintCode: Valid Parentheses
C++ stack<char|int|string>, push(), pop(), top(), empty(), size() class Solution { public: /** ...
- Web Worker是什么
.Web Worker是什么 Web Worker 是HTML5标准的一部分,这一规范定义了一套 API,它允许一段JavaScript程序运行在主线程之外的另外一个线程中.Web Worker 规范 ...
- 用Android程序打开和关闭输入法
一.打开输入法窗体: InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPU ...
- mule学习笔记
mule学习笔记 1.安装&配置 版本:mule 2.2.1 操作: 1) 下载.解压 2)配置环境变量:MULE_HOME.PATH 3)如果网络环境使用代理,找到%MULE_HOME%/c ...
- sell 项目 类目表 设计 及 创建
1.数据库设计 2.类目表 创建 /** * 类目表 */ create table `product_category` ( `category_id` int not null auto_incr ...
- soapui configure before taking to develop code
1,first go to the settings and configure as below:
- rabbitmq重装依赖的erlang 要注意
今天安装的erlang和rabbitmq版本不匹配导致出现各种问题,在使用正确版本安装后出现问题,在日志中找到报错信息: {"init terminating in do_boot" ...
- C# 7 out variables, tuples & other new features
C# 7 out variables, tuples & other new features C# 7 is available on new Visual Studio 2017 and ...
- [已解决]通过多层nginx,tomcat服务无法获取外网真实IP
问题描述: 使用腾讯的御天验证码,提示IP非法IP,内网的tomcat,经过2层nginx代理,服务获取的IP地址为内网ip地址,由于腾讯云的御天验证码对单一IP的频繁访问有拦截的,认定为非法IP(刷 ...