从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. hdu 4726(贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. 不需要打密码的sudo方法

    Linux下频繁输入sudo很麻烦.如果你的账户已经是sudoer了,那么编辑/etc/sudoers,将 %sudo ALL=(ALL:ALL) ALL 修改为: %sudo ALL=(ALL) N ...

  3. JavaWeb知识回顾-servlet简介。

    现在公司主要用jsp+servlet这种原生的开发方式,用的是uap的开发平台,所以趁着这个时候把有关javaweb的知识回顾一下. 首先是从servlet开始. 一.什么是Servlet?(是一些理 ...

  4. 洛谷P1720 月落乌啼算钱 题解

    题目传送门 初看题目,好难.再看一次,探索规律,发现这就是有名的斐波那契数列. F[i]=f[i-1]+f[i-2] SO 代码很简单,貌似要开long long,又貌似不用开. #include&l ...

  5. Eclipse如何定位到某一个类所在硬盘上的位置

    解决方法:安装OpenExplorer_1.5.0.v201108051513.jar插件 将OpenExplorer_1.5.0.v201108051513.jar文件添加到Eclipse所在目录下 ...

  6. AC日记——松江1843路 洛谷七月月赛

    松江1843路 思路: 三分: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #define ...

  7. plan-6.17周末

    喷完了自己,浑身舒爽. 搞个计划,最近要学东西,以提交博客为准,提交了才认为ok. 1.python的新书<<Fluent python>>不错,老的python资料已经满足不 ...

  8. Deepin 2015 火狐 Firefox安装Flash

    1.sudo apt-get install flashplugin-nonfree 2.至Abobe官网下载最新的Linux版本flash安装包,选择.tar.gz类型,下载(https://get ...

  9. lr回放Warning -26601报错的解决方法

    问题现象: Action2.c(30): Error -26601: Decompression function  (wgzMemDecompressBuffer) failed, return c ...

  10. ref:浅谈XXE漏洞攻击与防御

    ref:https://thief.one/2017/06/20/1/ 浅谈XXE漏洞攻击与防御 发表于 2017-06-20   |   分类于 web安全  |   热度 3189 ℃ 你会挽着我 ...