在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. Ubuntu登陆不进去(已解决)【转】

    本文转载自:https://blog.csdn.net/u010925447/article/details/72236387 Ubuntu一直在登陆界面循环:登陆密码是正确的,但是登陆后自动退回登陆 ...

  2. 阿里云环境搭建CDN内容分发

    1.创建CDN CNAME 指向CDN云厂商地址 2.使用域名转向到CDN云厂商地址 请求过来 通过cnd 分发到不同的服务器  如果有缓存的话 直接走了 CDN也可以实现安全功能,比如CDN实现防止 ...

  3. 用简单的反射优化代码(动态web项目)

    在动态web项目中,没有使用框架时,只是简单的jsp访问servlet实现增删改查, 无论是哪个方法都需要经过Servlet中的doGet()方法或doPost()方法,我们可以在链接中附带参数进行区 ...

  4. ViewPagerAdapter

    ) { ; } } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } ...

  5. DATEDIFF 的用法

    DECLARE @date DATETIME = '2017-12-26 00:00:00';DECLARE @date2 DATETIME = DATEADD(DAY, 1, @date);DECL ...

  6. linux vim vi编辑时撤销输入操作

    linux vim vi编辑时撤销输入操作 1,esc退出输入状态 2,u 撤销上次操作 3,ctrl+r 恢复撤销

  7. OpenStack企业私有云新需求(1):Nova 虚机支持 GPU

    作者:Sammy Liu 刘世民 本系列会介绍OpenStack 企业私有云的几个需求: GPU 支持 自动扩展(Auto-scaling)支持 混合云(Hybrid cloud)支持 物理机(Bar ...

  8. iOS审核拒绝苹果官方原因详解

    1.1不当内容应用程序不应该包括攻击性,敏感,令人不悦,侮辱或者品味低下的内容.例如: 1.1.1 诽谤或者人格侮辱的内容,包括引用或者评论宗教.种族.性取向.性别或者其他目标群体的内容,特别是该应用 ...

  9. android问题 This version of android studio is incompatible with the gradle version used.

    http://blog.csdn.net/sinat_15417921/article/details/51907728 问题背景 因为项目app分包需要,将工程的graddle版本升至了2.2.0 ...

  10. 分布式事务_03_2PC框架raincat源码解析-事务提交过程

    一.前言 前面两节,我们已经将raincat的demo工程启动,并简单分析了下事务协调者与事务参与者的启动过程. 这一节,我们来看下raincat的事务提交过程. 二.事务提交过程概览 1.二阶段对应 ...