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 ...
随机推荐
- docker link
什么是docker的link机制 同一个宿主机上的多个docker容器之间如果想进行通信,可以通过使用容器的ip地址来通信,也可以通过宿主机的ip加上容器暴露出的端口号来通信,前者会导致ip地址的硬编 ...
- 自己动手写OpenStack的QoS功能(4)
本文地址:http://blog.csdn.net/spch2008/article/details/9283561 数据库相应操作已完成,对OVS-Plugin进行修改. 在quantum\plug ...
- /var/adm/wtmp文件太大该怎么办?
/var/admin/wtmp文件记录所有用户的登陆信息,随着时间会增长到很大,/var/adm/wtmp文件太大该怎么办呢?先来看看/var/adm/wtmp文件的属性:testterm1:/#ls ...
- 使用document.domain和iframe实现站内AJAX跨域
站内AJAX跨域可以通过document.domain和iframe实现,比如www.css88.com.js.css88.com.css88.com这3个域名其实是3个不同的域,很多时候www.cs ...
- Java_脚本引擎_03_nashorn支持es6
一.前言 jdk1.8的nashorn 支持部分es6语法. 二.支持es6 jdk默认是关闭了对es6的支持的,想要开启对es6的支持,需要设置一下jvm参数: -Dnashorn.args=--l ...
- Java_数据交换_Jackson_用法入门
一.用法 二.参考资料 1.官方 1.Java下利用Jackson进行JSON解析和序列化 2.
- Mac 系统安装redis服务
1.首先去http://www.redis.io/下载最新的redis文件,现在最新的是redis-2.8.19 2.进行解压缩 tar -zxvf redis-2.8.19.tar.gz 3.移动重 ...
- Redis-简单动态字符串
这是读redis设计与实现的一系列读书笔记 1.SDS定义 C语言字符串:用一个 \0 结尾的 char 数组来表示 SDS:redis自己定义的简单动态字符串(simple dyanmic stri ...
- java入门学习(1)一简介及其基础特点
分类: java基础 1.为什么java如此流行,为什么历史选择了它? 因为它拥有全新的编程思想,更接近人们的语言习惯,由于其编译器把代码编译成字节码,然后再不同的平台上运行分别用不同的虚拟机去解释字 ...
- THUPC2017 小 L 的计算题
求 $k=1,2,\cdots,n \space \space \sum\limits_{i=1}^n a_i^k$ $n \leq 2 \times 10^5$ sol: 时隔多年终于卡过去了 之前 ...