以前,Spring推荐使用XML的方式来定义Bean及Bean之间的装配规则,但是在Spring3.0之后,Spring提出的强大的JavaConfig这种类型安全的Bean装配方式,它基于Java代码的灵活性,使得装配的过程也变得及其灵活。

@Configuration注解

  我们在定义JavaConfig类时,都会在其上加注@Configuration注解,来表明这是一个配置类,@Configuration注解底层是@Component注解,而且这个注解会被AnnotationConfigApplicationContext来进行加载,AnnotationConfigApplicationContext是ApplicationContext的一个具体实现,代表依据配置注解启动应用上下文。

@ComponentScan注解

  我们使用JavaConfig的目的是为了实现以前XML配置实现的功能,首先就是组件扫描功能,将我们使用特定注解标注的类统一扫描加载到Spring容器,这一功能就是依靠@ComponentScan注解来实现的,我们可以为其指定位置参数来指定要扫描的包。

@Bean注解

  使用@Bean注解我们可以实现XML配置中手动配置第三方Bean的功能,这里我们使用方法来定义Bean,并在方法前面加注@Bean注解,表示要将该方法返回的对象加载到Spring容器中,这样就对我们的方法定义带来了一些限制,这些限制包括方法的大概格式:

  1-方法带返回值,且返回类型为你要加载的第三方类类型

  2-方法的名称为默认的Bean的name,如果要自定义Bean的name,可以使用@Bean注解的name属性。

  3-要实现注入只需要将要注入的Bean的类型作为参数,调用该类的带参数的构造器构建这个Bean,或者采用第二种方式:先创建这个类的对象,然后调用该对象的set方法进行注入,以被注入的Bean的方法为参数

JavaConfig能够等价看成是XML文件,不过它只是用Java编写的。它的完整文档在网上可以看到,而这篇文章帮助你开始使用JavaConfig。让我们把下面的XML文件移植为一个JavaConfig来作为一个例子吧:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="helloBean" class="com.yiibai.hello.impl.HelloWorldImpl"> </beans>
下面是等价的文件: 
package com.yiibai.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.yiibai.hello.HelloWorld;
import com.yiibai.hello.impl.HelloWorldImpl; @Configuration
public class AppConfig { @Bean(name="helloBean")
public HelloWorld helloWorld() {
return new HelloWorldImpl();
} }
a.一个简单的bean
package com.spring;

public interface Helloeorld {

    void printHelloWorld(String msg);
}
package com.spring;

public class HelloworldImpl implements Helloeorld{

    @Override
public void printHelloWorld(String msg) { System.out.println("Hello : " + msg);
} } b.javaConfig注解:使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean
package com.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /*使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,
并通过 @Bean 定义 bean*/
@Configuration
public class TestConfig { @Bean(name="helloeorld")
public Helloeorld getHello(){
return new HelloworldImpl(); }
} c.测试main方法:
package com.spring; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
  public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
    Helloeorld obj = (Helloeorld) context.getBean("helloeorld");
    obj.printHelloWorld("Spring Java Config");
  } }

Spring JavaConfig的更多相关文章

  1. Spring JavaConfig实例

    从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中. 但是,仍然允许使用经典的XML方式来定义bean ...

  2. Spring重温(二)--Spring JavaConfig

    1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...

  3. Spring JavaConfig @Import实例

    一般来说, 需要按模块或类别 分割Spring XML bean文件 成多个小文件, 使事情更容易维护和模块化. 例如, <beans xmlns="http://www.spring ...

  4. RabbitMQ基础教程之Spring&JavaConfig使用篇

    RabbitMQ基础教程之Spring使用篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 RabbitMQ基础 ...

  5. spring javaconfig druidsource

    package dataConfig; import java.sql.SQLException; import org.springframework.context.annotation.Bean ...

  6. Spring基础知识

    Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...

  7. 译:Spring框架参考文档之IoC容器(未完成)

    6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...

  8. Spring IOC之Classpath扫描和管理的组件

    在前面的大部分例子我们使用XML去指明配置数据去定义在Spring容器中的每一个BeanDefinition.上一节我们展示了如何在 代码层注解的方式来提供大量的配置信息.即使在这些例子中,但是,基础 ...

  9. Spring IOC之容器概述

    1.SpringIOC容器和beans介绍 IOC的依赖注入是这样的,对象定义他们的依赖也就是他们需要在一起起作用的对象是通过构造器参数以及工厂方法的参数或者是当他们被构建或者是从工厂中返回时设置在对 ...

随机推荐

  1. 微信小程序 - 支持html空格(提示)

    仅限于text标签,decode参数:官方api.

  2. 微信小程序 - 图片懒加载

    wxml <!-- 数据源 --> <view class='item-{{index}}' wx:for="{{lazyData}}" wx:key=" ...

  3. JAVA动态编译辅助类

    一.场景 平时我们学学用到在JVM运行时,动态编译.java的源代码情况,比如作为灵活的配置文件.这时候就要用到动态编译,参考下列. 二.类内容 1.引入依赖: <!-- https://mvn ...

  4. LLVM lli llc

    http://zke1ev3n.me/2016/01/18/%E5%9F%BA%E4%BA%8ELLVM%E7%9A%84%E4%BB%A3%E7%A0%81%E6%B7%B7%E6%B7%86/ h ...

  5. 【DB2】SQL优化

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAA

  6. Reading CheckBoxes and Radio Buttons

    Input tags with the type attribute checkbox can be grouped like radio buttons so that several checkb ...

  7. 使用maven编译Java项目

    摘要: 综述 本文演示了用Maven编译Java项目 需要 时间:15分钟 文本编辑器或者IDE JDK 6 或者更高版本 创建项目 本例主要为了展示Maven,所以Java的项目力求简单. 创建项目 ...

  8. DOM API详解

    来源于:http://zxc0328.github.io/2016/01/23/learning-dom-part1/ https://zxc0328.github.io/2016/01/26/lea ...

  9. (原)ubuntu挂载及开机自动挂载网络端的文件夹的方法

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/7160792.html 参考网址: http://blog.csdn.net/tlight/articl ...

  10. YAML 语言教程(转载)

    用YAML语言读取配置是最快的,之前的suricata中用yaml读取了配置,并且在代码运行期间,对配置进行了维护,所以抽点时间,来了解一下YAML语言编程,下文虽然对YAML语言和JAVAScrip ...