从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. TreeSet之定制排序和自然排序

    TreeSet的几大特点: 1.TreeSet中存储的类型必须是一致的,不能一下存int,一下又存string 2.TreeSet在遍历集合元素时,是有顺序的[从小到大](我的理解,如果存的字母,按字 ...

  2. 斐讯路由器L(联)B(壁)K-码兑换包安全下车通道(图文教程)

    大家好,最近大家比较关心的斐讯路由器如何下车问题,楼主亲自试提取了一遍,记录下过程,欢迎大家一起讨论. 言归正传,上图,上图! No.1 打开斐讯提供的良心k码退换通道: https://tech-s ...

  3. 处理tomcat内存溢出问题

    TOMCAT起步内存溢出问题Exception in thread ""http-bio-8080"-exec-java.lang.OutOfMemoryError: P ...

  4. docker部署Asp.net core应用

    1 容器概念 使用Docker前我们首先要简单了解一下容器的概念.MSDN上有一张虚拟机和容器的对比图,很好的展示了虚拟机和容器的区别,如下所示,虚拟机包括应用程序.必需的库或二进制文件以及完整的来宾 ...

  5. jquery 验证

    引入JS <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src=&qu ...

  6. 【C#】缓存数据

    namespace WpfCopy.Controls { public class CacheFileEventArgs : EventArgs { public bool IsFaulted { g ...

  7. 面试的65个回答技巧-适用于BAT公司

    互联网职业群分享的资料,里面大多是BAT公司的人,很多是猎头.这些技巧对于职场人来说,是非常宝贵的. 1.请你自我介绍一下你自己? 回答提示:一般人回答这个问题过于平常,只说姓名.年龄.爱好.工作经验 ...

  8. 爱奇艺全国高校算法大赛初赛C

    区间$dp$. 倒着考虑这件事件,肯定有最后一个取走的数字,假设是$a[k]$,那么最后一次取走的价值肯定是$a[0]*a[k]*a[n+1]$,之前取走的价值和为$[1,k-1]$的价值加上$[k+ ...

  9. 1. MNIST读取数据

    %pylab inline Populating the interactive namespace from numpy and matplotlib 在Yann LeCun教授的网站中(http: ...

  10. jquery实用的一些方法

    做个购物车功能,需要修改下前端页面 有些实用的方法总结一下 当你想实现最基本的加减法的时候,对于转换number实用Number(str)即可 首先明确下页面的每一行是动态的,这个时候绑定事件的时候不 ...