从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。
但是,仍然允许使用经典的XML方式来定义bean和配置,JavaConfig是另一种替代解决方案。
看来看经典的XML定义和JavaConfig的不同,如下定义在Spring容器中的bean。

Spring XML file - applicationContext.xml :

<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>
等效于以下JavaConfig的配置:
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();
} }

Spring JavaConfig Hello World

现在,看到一个完整的Spring JavaConfig例子。

1. 工程目录结构

这个例子的目录结构如下。

3. Spring Bean

一个简单的Bean

package com.yiibai.hello;

public interface HelloWorld {

	void printHelloWorld(String msg);

}
package com.yiibai.hello.impl;

import com.yiibai.hello.HelloWorld;

public class HelloWorldImpl implements HelloWorld {

	@Override
public void printHelloWorld(String msg) { System.out.println("Hello : " + msg);
} }

4. JavaConfig 注解

使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean。
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();
} }

5. 执行结果

使用 AnnotationConfigApplicationContext 加载您的JavaConfig类

package com.yiibai.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.yiibai.config.AppConfig;
import com.yiibai.hello.HelloWorld; public class App {
public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
HelloWorld obj = (HelloWorld) context.getBean("helloBean"); obj.printHelloWorld("Spring Java Config"); }
}

输出结果

Hello : Spring Java Config
 
http://www.yiibai.com/spring/spring-3-javaconfig-example.html

Spring JavaConfig实例的更多相关文章

  1. spring得到实例和new一个实例,哪个快?

    spring配置的bean是默认单例,那么在程序中,得到一个实例一定比创建一个实例的速度快,也更加省资源.今天实际测试的时候发现,new 一个对象比spring得到一个对象快多了.后面自己又加了个单例 ...

  2. Spring Security4实例(Java config版)——ajax登录,自定义验证

    本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...

  3. Spring Security4实例(Java config 版) —— Remember-Me

    本文源码请看这里 相关文章: Spring Security4实例(Java config版)--ajax登录,自定义验证 Spring Security提供了两种remember-me的实现,一种是 ...

  4. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  5. SSH---整合Struts2&Spring&Hibernate(实例)

    一.SSH回顾 Struts2:核心为过滤器+拦截器.过程:Filter--->FilterDispatcher-->ActionMapper-->ActionProxy--> ...

  6. Spring事物实例

    Spring事务实例: entity实体类: public class Accounts { private int accountid; private String accountname; pr ...

  7. Spring登录实例

    Spring登录实例 项目结构 首先看一下整个项目的目录结构,如下: 导入Jar包 工欲善必先利其器,导入一下Jar包 配置文件 web.xml 配置 web.xml配置文件,如下: xmlns:xs ...

  8. Spring JavaConfig @Import实例

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

  9. spring mvc: 注解和JavaConfig实例

    通过javaConfig来配置config,并能正常访问url. 先看图 访问地址:http://localhost:8080/gugua5/ http://localhost:8080/gugua5 ...

随机推荐

  1. 自动ftp 上传

    #!/bin/sh ftp -n<<END_FTP open 192.168.1.4 user codfei duibuqi //用户名codfei 密码duibuqi binary pr ...

  2. php 学习try_catch测试抛出异常

    /** * Class show * 一个catch接收抛出异常 */ class show { // 错误的演示 //try { //require ('test_try_catch.php'); ...

  3. Gradient-Based Learning Applied to Document Recognition 部分阅读

    卷积网络        卷积网络用三种结构来确保移位.尺度和旋转不变:局部感知野.权值共享和时间或空间降采样.典型的leNet-5如下图所示: C1中每个特征图的每个单元和输入的25个点相连,这个5* ...

  4. 爬虫基础库之beautifulsoup的简单使用

    beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...

  5. 对于JAVA多线程卖票小程序的理解

    昨天把多线程重新看了一遍,发现自己还是有许多需要理解的地方,现在写一篇总结. 一:利用继承Thread类会出现的问题: public class SellTicketsThread extends T ...

  6. 转载:C++ typename的起源与用法

    转载:http://feihu.me/blog/2014/the-origin-and-usage-of-typename/#typename 侯捷在Effective C++的中文版译序中提到: C ...

  7. 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  8. Yii2使用驼峰命名的形式访问控制器

    yii2在使用的时候,访问控制器的时候,如果控制器的名称是驼峰命名法,那访问的url中要改成横线的形式.例如: public function actionRoomUpdate() { // }//访 ...

  9. 检测浏览器对HTML5新input类型的支持

    HTML5新增加了很多input元素类型,比如color,date,datetime,datetime-local,email,month,number,range,search,tel,time,u ...

  10. 【WPF】自定义控件之远程图片浏览

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...