Spring JavaConfig实例
2024-08-25 18:21:33
从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
最新文章
- F#之旅8 - 图片处理应用之动画二维码
- Netbeans 中创建数据连接池和数据源步骤(及解决无法ping通问题)
- struts2 CVE-2013-4316 S2-019 Dynamic method executions Vul
- 使用Settings.settings存储用户的个性化配置
- SQL Server并行死锁案例解析
- Linux永久修改系统时间和时区方法
- js字符串函数之indexOf()
- selenium中处理不带ID的弹出窗口
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-006-用LDAP比较密码(passwordCompare()、passwordAttribute(";passcode";)、passwordEncoder(new Md5PasswordEncoder()))
- 使用APT减少MVP的冗余代码
- 转:推荐!国外程序员整理的 C++ 资源大全
- Android Stduio的使用(七)--Structure窗口
- 一个请求中,ADF、JSF究竟做了哪些工作
- angular4.0微信oAuth第三方认证的正确方式
- 如何在SpriteBuilder中使用BM Font Label
- adduser与useradd的区别
- python学习之路06——字符串
- Redis详解(一)冰叔带你了解Redis
- 爬虫系列1:python简易爬虫分析
- SQLServer&#160;远程链接MySql数据库详解
热门文章
- 《跟老齐学Python Django实战》读后感
- php cache类代码(php数据缓存类)
- webservice使用
- PYTHON学习(三)之利用python进行数据分析(1)---准备工作
- poj 2828(线段树单点更新)
- 用socket发送匿名邮件之python实现
- 【重点】Jmeter----- 将 JDBC Request 查询结果作为下一个接口参数方法(二)
- Weblogic常用监控指标以及监控工具小结
- [MySQL] specified key was too long max key length is 767bytes
- jquery datatable的详细用法