在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.

  1. @Configuration
  2. @Bean
  3. @DependsOn
  4. @Primary
  5. @Lazy
  6. @Import
  7. @ImportResource
  8. @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.

最新文章

  1. ApacheCommons的Java公共类库(实现如Log这些功能)
  2. Markdown 完全指南
  3. C# 版本的冒泡排序,包括该死的控制台读取
  4. 转:ADO.NET连接字符串
  5. php获取数组第一个值 current()
  6. ASP.NET MVC 的URL路由介绍
  7. CSS 宝库
  8. Vue.js学习 Item8 -- 方法与事件处理器
  9. 2014 ACM/ICPC Asia Regional Guangzhou Online
  10. codeforces #313 div1 A
  11. 按钮制作技巧(css精灵效果)-高级版
  12. codeforces 665E Beautiful Subarrays
  13. js随机模块颜色
  14. Web地图导图总结
  15. springboot在eclipse中运行使用开发配置,打包后运行使用生产环境默认配置
  16. C# 记录日志
  17. qrcode.php
  18. Selenium webdriver操作日历控件
  19. EF性能优化-有人说EF性能低,我想说:EF确实不如ADO.NET
  20. wx工具栏,菜单栏,状态栏

热门文章

  1. IntelliJ IDEA 中 右键新建时,选项没有Java class的解决方法和具体解释
  2. javaMail发送邮件实例
  3. QT paintevent 事件, update()槽函数
  4. host capability
  5. keystone DB in devstack
  6. phalcon:整合官方多模块功能,方便多表查询
  7. java:file文件类
  8. ES6-Set和Map数据结构学习笔记
  9. css3——transition属性和opacity属性
  10. socket和多线程编程资料汇集-基础篇