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 ...
随机推荐
- HTML5 JS 压缩图片,并取得图片的BASE64编码上传
基本过程 1) 调用 FileReader 的 reader.readAsDataURL(img); 方法, 在其onload事件中, 将用户选择的图片读入 Image对象. 2) 在image对象的 ...
- linux基础(5)-用户及权限
用户与组 用户:使用linux时,需要以用户的身份登陆 组 :用来方便组织管理用户 用户种类 -root用户(ID为0的用户为root用户) -系统用户(1-499) -普通用户(500以上 ...
- 执行Oracle存储过程报权限不足的解决方法
当前Oracle用户sofa拥有connect.dba.resource的角色权限,但奇怪的是却没有执行Oracle Procedure的权限.后来通过查找资料发现:如果sofa用户需要执行Proce ...
- UVA 10909 Lucky Number(树状数组+二分+YY)
此题测试时预处理等了很久,结果470ms过了...... 题意:开始不怎么懂,结果发现是这个: 波兰裔美国数学家斯塔尼斯拉夫·乌拉姆(Stanislaw Ulam)在20世纪50年代中期开发出了另一种 ...
- elasticsearch中如何手动控制全文检索结果的精准度
1.为帖子数据增加标题字段 POST /forum/article/_bulk{ "update": { "_id": "1"} }{ &q ...
- 内核编译错误解答(elf_i386错误)
内核编译错误解答(elf_i386错误) 在编译内核过程中遇到的问题及解决方法: 1.root@org:/usr/src/linux# make menuconfig *** Unable to f ...
- js dom操作选择器,dom操作复习
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PyCharm 的升级、设置快捷方式
05. PyCharm 的升级以及其他 PyCharm 提供了对 学生和教师免费使用的版本 教育版下载地址:https://www.jetbrains.com/pycharm-edu/download ...
- 如何创建效率高sql-建立索引
我们做开发的人员,虽说自己不是专业从事数据库方面研究的(如DBA),但很多时候,公司没有专门的DBA,所以拿到具体的项目中,整体的数据库设计都是开发人员自己写的,随着时间的推移,加上开发经验的增长 ...
- js:for循环ul/li,获取当前被点击元素的id,以及给其他li设置属性
js:for循环ul/li,获取当前被点击元素的id,以及给其他li设置属性 <!doctype html> <html> <head> <meta char ...