spring中JavaConfig相关的注解
在spring3.0中增加配置spring beans的新方式JavaConfig,可以替换spring的applicataion.xml配置。也即@Configuration对等<beans/>,@Bean对等<bean/>,关于@Configuration见《spring4.0之二:@Configuration的使用》。
The following are the list of annotations introduced as part of the JavaConfig module.
- @Configuration
- @Bean
- @DependsOn
- @Primary
- @Lazy
- @Import
- @ImportResource
- @Value
In this example I have used only the first two annotations to demonstrate the basic use of JavaConfig features.
AnnotationConfigApplicationContext is the class used for loading the configuration from Java class file. Look at the below example. If you have any questions, please write it in the comments section.
1. Create JavaConfig
The following are the twp steps required for creating the Java configuration classes in the spring framework.
- Annotate the class with @Configuration annotation
- Annotate the method with @Bean to indicating that it is bean definition
package javabeat.net; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class JavaConfig {
@Bean(name="userDetails")
public UserDetails userDetails(){
return new UserDetails();
}
}
2. Create Bean Class
package javabeat.net; public class UserDetails {
private String name;
private String phone;
private String city;
private String country; // Other methods }
3. Load Application Context and Instantiate Bean
Create AnnotationConfigApplicationContext and get the bean instance. This is very simple example to show you how simple to configure the beans using Java class.
package javabeat.net; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class JavaConfigDemo {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
UserDetails userDetails = (UserDetails) context.getBean("userDetails");
}
}
I hope this article have helped you to understand the use of @Configuration annotation and how to use them. This has been taken as the recommended way for writing the configurations for spring applications.
Related posts:
- @Required Annotation in Spring
- Annotation Based Bean Wiring @Autowired in Spring framework
- @Qualifier Annotation in Spring
- @Autowired Annotation in Spring
- @ModelAttribute in Spring MVC
拷贝:http://javabeat.net/javaconfig-spring-3-0/
spring中JavaConfig相关的注解的更多相关文章
- Spring中AOP相关的API及源码解析
Spring中AOP相关的API及源码解析 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring ...
- 使用Spring的JavaConfig 和 @Autowired注解与自动装配
1 JavaConfig 配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...
- Spring中AOP相关源码解析
前言 在Spring中AOP是我们使用的非常频繁的一个特性.通过AOP我们可以补足一些面向对象编程中不足或难以实现的部分. AOP 前置理论 首先在学习源码之前我们需要了解关于AOP的相关概念如切点切 ...
- Spring中Aware相关接口原理
Spring中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.ServletContextA ...
- Spring中@Resource、@controller注解的含义
@Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象. Spring确实支 ...
- spring中少用的注解@primary解析
这次看下spring中少见的注解@primary注解,例子 @Component public class MetalSinger implements Singer{ @Override publi ...
- Spring中如何使用自定义注解搭配@Import引入内外部配置并完成某一功能的启用
文章背景 有一个封装 RocketMq 的 client 的需求,用来提供给各项目收.发消息,但是项目当中常常只使用收或者发消息的单一功能,而且不同的项目 group 等并不相同而且不会变化,可以在项 ...
- Spring中的属性注入注解
@Inject使用 JSR330规范实现的 默认按照类型注入 如果需要按照名称注入,@Inject需要和@Name一起使用 @Resource JSR250规范实现的,需要导入不同的包 @Resour ...
- spring中整合ssm框架注解版
和xml版差不多,只不过创建对象的方式是由spring自动扫描包名,然后命名空间多一行context代码在application.xml中,然后将每个对象通过注解创建和注入: 直接上代码: 1.use ...
随机推荐
- js学习笔记2(5章操作方法)
5.2.5操作 concat();将slice(); 5.2.7位置操作的方法 其他的不赘述 5.5 函数 1.函数其实是对象,每一个函数都是function对象的实例,与其他引用类型一样,都具有属性 ...
- Myeclipse中Tomcat的两种部署方式
一.在Myeclipse软件中部署 1. 在Myeclipse中,创建好工程后,在Myeclipse菜单栏中选择 Windows -> Preferences -> Myeclipse - ...
- oracle 计算时间差
1.计算时间差(相隔星期,天数,小时,分钟,秒) SELECT TO_CHAR(date1,'MMDDYYYY:HH24:MI:SS') date1, TO_CHAR(date2,'MMDDYYYY: ...
- Linux操作系统的安装以及基本的操作命令详解
背景:使用的虚拟机安装Linux 虚拟机使用的是VMware Linux版本:CentOS-6.7-X86 自行下载:CentOS-6.7-x86_64-bin-DVD1.iso 打开VMw ...
- [转载]spring security 的 logout 功能
原文地址:security 的 logout 功能">spring security 的 logout 功能作者:sumnny 转载自:http://lengyun3566.iteye ...
- 导出android真机上应用的apk文件
1. 首先你的手机要开启调试模式 2. 终端输入命令行 (这个时候需要在手机端打开此应用.它的思路是抓取出当前窗口的包名.以下命令操作自己未亲自验证.) adb shell dumpsys windo ...
- 虚拟机CentOS6.5网络配置
不得不说 6.5比7.0麻烦了许多.. 编辑ifcfg配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0 内容如下 DEVICE=eth0 HWADDR ...
- 将datagridview数据保为xml或txt文件
using System.IOpublic void SaveFile() { //实例化一个保存文件对话框 SaveFileDialog s ...
- asp.net IE 页面刷新固定位置
MaintainScrollPositionOnPostback="true" 可能我们会经常遇到这种情况,当页面内容比较多的时候,当用户执行操作执行一次页面回送后,页面又重新从顶 ...
- java中如何将string 转化成long
1.Java中如何将string 转化成long long l = Long.parseLong([String]); 或 long l = Long.parseLong([String],[int ...