Spring JavaConfig实例
2016-05-27 09:50
465 查看
从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。
但是,仍然允许使用经典的XML方式来定义bean和配置,JavaConfig是另一种替代解决方案。
看来看经典的XML定义和JavaConfig的不同,如下定义在Spring容器中的bean。
Spring XML file - applicationContext.xml :
等效于以下JavaConfig的配置:
现在,看到一个完整的Spring JavaConfig例子。
这个例子的目录结构如下。
一个简单的Bean
使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean。
使用 AnnotationConfigApplicationContext 加载您的JavaConfig类
输出结果
但是,仍然允许使用经典的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
一个简单的Beanpackage 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
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 浅析Java中的set集合类型及其接口的用法