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

spring中JavaConfig相关的注解的更多相关文章

  1. Spring中AOP相关的API及源码解析

    Spring中AOP相关的API及源码解析 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring ...

  2. 使用Spring的JavaConfig 和 @Autowired注解与自动装配

    1 JavaConfig  配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...

  3. Spring中AOP相关源码解析

    前言 在Spring中AOP是我们使用的非常频繁的一个特性.通过AOP我们可以补足一些面向对象编程中不足或难以实现的部分. AOP 前置理论 首先在学习源码之前我们需要了解关于AOP的相关概念如切点切 ...

  4. Spring中Aware相关接口原理

    Spring中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.ServletContextA ...

  5. Spring中@Resource、@controller注解的含义

    @Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象. Spring确实支 ...

  6. spring中少用的注解@primary解析

    这次看下spring中少见的注解@primary注解,例子 @Component public class MetalSinger implements Singer{ @Override publi ...

  7. Spring中如何使用自定义注解搭配@Import引入内外部配置并完成某一功能的启用

    文章背景 有一个封装 RocketMq 的 client 的需求,用来提供给各项目收.发消息,但是项目当中常常只使用收或者发消息的单一功能,而且不同的项目 group 等并不相同而且不会变化,可以在项 ...

  8. Spring中的属性注入注解

    @Inject使用 JSR330规范实现的 默认按照类型注入 如果需要按照名称注入,@Inject需要和@Name一起使用 @Resource JSR250规范实现的,需要导入不同的包 @Resour ...

  9. spring中整合ssm框架注解版

    和xml版差不多,只不过创建对象的方式是由spring自动扫描包名,然后命名空间多一行context代码在application.xml中,然后将每个对象通过注解创建和注入: 直接上代码: 1.use ...

随机推荐

  1. POI实现数据的导入

    1.POI技术的概述? POI技术:apache POI是可以对微软office文档进行读和写的工具. l HSSF:操作97格式的excel,扩展名:.xls 纯二进制,最大行数65535. l X ...

  2. 【bzoj1258】三角形tri[CQOI2007](乱搞)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1258 这是道很意思的大水题,虽然看上去一脸懵逼,但画张图出来看看也可以窥见一丝端倪. ...

  3. [Codechef November Challenge 2012] Arithmetic Progressions

    题意:给定一个序列,求多少个三元组满足ai+ak=2*aj(i<j<k). 题解:原来叉姐的讲义上有啊..完全忘掉了.. 首先这个式子很明显是一个卷积.我们有了FFT的思路.但是肯定不能每 ...

  4. Ansi 与 Unicode 字符串类型的互相转换

    WideCharToMultiByte 实现宽字节转换到窄字节MultiByteToWideChar 实现窄字节转换到宽字节 WideCharToMultiByte 的代码页用来标记与新转换的字符串相 ...

  5. java中的向下转型

    1.父类对象可以强制转换为子类对象,但是前提是此父类对象为子类对象实例化的结果. e.g. Fruit fruit=new Apple(); Apple a=(Apple)fruit;//ok e.g ...

  6. boot小知识

    lg 大, md 中等, sm 小, xs 极小. 可以单独用,也可以混合用,不同的屏幕用不同的比例. push ,pull 推拉.这个不实用. row里面可以嵌套实用row. 挤不下的时候,就会自动 ...

  7. 根据图片名字在drawable中得到图片

    int imageId = context.getResources().getIdentifier("图片的名字","drawable", "包名& ...

  8. RtlWerpReportException failed with status code :-1073741823

    在release下程序运行总是崩溃:debugView输出了这个崩溃信息, 1. 一开始是release看崩溃,各种二分法找崩溃点,太玄没找到: 2. 终于想到可以调试,我草,调试一下瞬间发现某个cl ...

  9. Respond.js的作用

    在html页面中我们经常看到 <!--[if lt IE 9]>    //判断当前浏览器的版本是否小于IE 9          <script src="https:/ ...

  10. 《Advanced Bash-scripting Guide》学习(八):从一个目录移动整个目录树到另一个目录

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 ABS书上的例子: 从一个目录移动整个目录树到另一个目录 #!/bin/bash ...