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 ...
随机推荐
- Android LCD(二):常用接口原理篇【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/9125799 关键词:Android LCD TFT TTL(RGB) LVDS E ...
- Java 访问修饰符总结
Java中的访问修饰符 Java面向对象的基本思想之一是封装细节并且公开接口. Java语言采用访问控制修饰符来封装类及类的方法和属性的访问权限,从而向使用者暴露接口.隐藏细节. Java访问控制分为 ...
- jar 冲突解决方案
val urlOfClass = classOf[Nothing].getClassLoader.getResource("org/slf4j/spi/LocationAwareLogger ...
- maven 中pom.xml文件依赖包从本地加载如何配置?
比如我现在有一个需求是:项目中要加载ueditor的jar架构包,并且用maven构建的项目 那么在pom.xml文件中如配置: 说明:${project.basedir} 是maven 自带(内置) ...
- HBase-协处理器详解及实现
协处理器(coprocessor) 把一部分计算移动到数据的存放端. 实例:HBase添加solr二级索引详细代码 简介 协处理器允许用户在region服务器上运行自己的代码,允许用户执行region ...
- 字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)?
字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)? 根据我的了解,编码中有三个核心概念:1. 字符集(Character Set),可以说是一个抽象概念,字符的合集2. ...
- Codeforces Round #280 (Div. 2) A , B , C
A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #371 (Div. 2) A ,B , C 水,水,trie树
A. Meeting of Old Friends time limit per test 1 second memory limit per test 256 megabytes input sta ...
- cos用在什么场景
随着应用的不断发展,应用过的图片,文件会不断增加, 存储量的提升带来的是IO用量的提升和带宽占用增加 大部分文件属于低频文件,但是占用较大 硬盘的变更需要重启机器 这个循环会造成支出不断增加 为了节省 ...
- js建造者(生成器)模式
建造者模式将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成. 建造者模式类图: ...