7 -- Spring的基本用法 -- 6...
7.6 Spring 3.0 提供的Java配置管理
Spring 允许使用Java类进行配置管理,可以不使用XML来管理Bean,以及Bean之间的依赖关系。
Interface :Person
package edu.pri.lime._7_6.bean;
public interface Person {
void userAxe();
}
Interface : Axe
package edu.pri.lime._7_6.bean;
public interface Axe {
String chop();
}
Class : Chinese
package edu.pri.lime._7_6.bean.impl; import edu.pri.lime._7_6.bean.Axe;
import edu.pri.lime._7_6.bean.Person; public class Chinese implements Person { private Axe axe;
private String name;
public Axe getAxe() {
return axe;
}
public void setAxe(Axe axe) {
this.axe = axe;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void userAxe() {
System.out.println("我是:" + name + "," + axe.chop());
}
}
Class : StoneAxe
package edu.pri.lime._7_6.bean.impl;
import edu.pri.lime._7_6.bean.Axe;
public class StoneAxe implements Axe {
public String chop() {
return "石斧砍柴真慢";
}
}
Class : SteelAxe
package edu.pri.lime._7_6.bean.impl;
import edu.pri.lime._7_6.bean.Axe;
public class SteelAxe implements Axe {
public String chop() {
return "钢斧砍柴真快";
}
}
Class : AppConfig
package edu.pri.lime._7_6.bean.configuration; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import edu.pri.lime._7_6.bean.Axe;
import edu.pri.lime._7_6.bean.Person;
import edu.pri.lime._7_6.bean.impl.Chinese;
import edu.pri.lime._7_6.bean.impl.SteelAxe;
import edu.pri.lime._7_6.bean.impl.StoneAxe; //指定类为配置类
@Configuration
public class AppConfig { // 相当于定义一个名为personName的变量,其值为“lime”
@Value("lime")
String personName;
// 配置Bean : stoneAxe
@Bean(name="stoneAxe")
public Axe stoneAxe(){
return new StoneAxe();
}
// 配置Bean : steelAxe
@Bean(name="steelAxe")
public Axe steelAxe(){
return new SteelAxe();
}
// 配置一个Bean:chinese
@Bean(name="chinese")
public Person chinese(){
Chinese person = new Chinese();
person.setName(personName);
person.setAxe(steelAxe());
return person;
}
}
Class :BeanTest
package edu.pri.lime._7_6.main; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import edu.pri.lime._7_6.bean.Person;
import edu.pri.lime._7_6.bean.configuration.AppConfig;
import edu.pri.lime._7_6.bean.impl.Chinese; public class BeanTest { public static void main(String[] args){
//使用Java配置类管理Spring容器中的Bean及其依赖关系时,使用AnnotationConfigApplication创建容器
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Person chinese = ctx.getBean("chinese",Chinese.class);
chinese.userAxe();
}
}
@Configuration : 用于修饰一个Java配置类
@Bean : 用于修饰一个方法,将该方法的返回值定义成容器中的一个Bean。
@Value : 用于修饰以恶搞Field,用于为该Field配置一个值,相当于配置一个变量。
@Import : 修饰一个Java配置类,用于向当前Java配置类中导入其他Java配置类。
@Scope : 用于修饰一个方法,指定该方法对应的Bean的生命域。
@Lazy : 用于修饰一个方法,指定该方法对应的Bean是否需要延迟初始化。
@DependsOn : 用于修饰一个方法,指定在初始化该方法对应的Bean之前初始化指定的Bean。
在实际项目中可能会混合使用XML配置和Java类配置混合使用:
⊙ 如果以XML配置为主,就需要让XMLpehiz能加载Java类配置。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 加载Java配置类 -->
<bean class="edu.pri.lime._7_6.bean.configuration.AppConfig" />
</beans>
由于以XML配置为主,因此应用创建Spring容器时,还是以XML配置文件为参数来创建ApplicationContext对象。那么Spring会先加载XML配置文件,在根据XML配置文件的指示去加载指定的Java配置类。
⊙ 如果以Java类配置为主,就需要让Java配置类能加载XML配置。在配置类上增加@ImportResource注解,修饰Java配置类,用于导入指定的XML配置文件。
package edu.pri.lime._7_6.bean.configuration; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; import edu.pri.lime._7_6.bean.Axe;
import edu.pri.lime._7_6.bean.Person;
import edu.pri.lime._7_6.bean.impl.Chinese;
import edu.pri.lime._7_6.bean.impl.SteelAxe;
import edu.pri.lime._7_6.bean.impl.StoneAxe; //指定类为配置类
@Configuration
@ImportResource(value = { "classpath:app_7_6.xml" })
public class AppConfig { // 相当于定义一个名为personName的变量,其值为“lime”
@Value("lime")
String personName;
// 配置Bean : stoneAxe
@Bean(name="stoneAxe")
public Axe stoneAxe(){
return new StoneAxe();
}
// 配置Bean : steelAxe
@Bean(name="steelAxe")
public Axe steelAxe(){
return new SteelAxe();
}
// 配置一个Bean:chinese
@Bean(name="chinese")
public Person chinese(){
Chinese person = new Chinese();
person.setName(personName);
person.setAxe(steelAxe());
return person;
}
}
由于以Java类配置为主,因此应用创建Spring容器时,应以Java配置类为参数,通过创建AnnotationConfigApplicationContext对象作为Spring容器。那么Spring会先加载Java配置类,在根据Java配置类的指示去加载指定的XML配置文件。
7 -- Spring的基本用法 -- 6...的更多相关文章
- SpringMVC +mybatis+spring 结合easyui用法及常见问题总结
SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...
- Spring中@Async用法详解及简单实例
Spring中@Async用法 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类 ...
- (转)Spring中@Async用法总结
原文:http://blog.csdn.net/blueheart20/article/details/44648667 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的: ...
- Spring中@Async用法总结
引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3. ...
- spring AOP的用法
AOP,面向切面编程,它能把与核心业务逻辑无关的散落在各处并且重复的代码给封装起来,降低了模块之间的耦合度,便于维护.具体的应用场景有:日志,权限和事务管理这些方面.可以通过一张图来理解下: Spri ...
- Spring常用注解用法总结
转自http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Dispat ...
- Spring中@Value用法收集
一.配置方式 @Value需要参数,这里参数可以是两种形式: @Value("#{configProperties['t1.msgname']}") 或者 @Value(" ...
- Spring data redis-StringRedisTemplate 用法
Spring-data-redis为spring-data模块中对redis的支持部分,简称为“SDR”,提供了基于jedis客户端API的高度封装以及与spring容器的整合,事实上jedis客户端 ...
- Spring.Net简单用法
Spring.Net其实就是抽象工厂,只不过更加灵活强大,性能上并没有明显的区别. 它帮我们实现了控制反转. 其有两种依赖注入方式. 第一:属性注入 第二:构造函数注入 首先,我们去 Spring. ...
随机推荐
- hashCode的作用
在一般的应用中你不需要了解hashCode的用法,但当你用到HashMap,HashSet等集合类时要注意下hashCode. 你想通过一个object的key来拿HashMap的value, ...
- java 文件上传
java 上传文件 如果不依赖框架的话 要利用 Apache 中几个jar文件来处理 1. 给表单设置enctype属性,其值为 "multipart/form-data" ...
- C# 自动Ping服务
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; u ...
- jeesz源码下载
大型分布式企业架构 jeesz,百度去搜索jeesz
- 字符转换(C、C++)
标准C和C++库提供了一些转换工具.但是它们在易用性.扩展型和安全型上各有不同. 例如,以atoi为代表的一系列标准C函数就有一些限制: * 只支持单向转换:从文本到内部数据类型.要用C库函数实现另一 ...
- push or get File or Folder using scp wrapped with expect and bash
经常需要把服务器的某些文件传到 Mac,或者获取 Mac 的一些文件到服务器.尽管有很多命令scp, ftp, rsync都可以,霸特每次都有敲好长的命令,好烦,而且还要输入密码.所以想着 wrap ...
- git 常用命令及解析 由浅入深
笔者用的是windows系统,不过并没有什么影响. Git 分布式版本控制系统. 为了让初学git的人明白git是干什么的,有什么意义 笔者觉得先来介绍git作为版本控制器是怎么运作的会让大家对后边 ...
- 动画黄金搭档:CADisplayLink & CAShapeLayer
我们在开发中有时会遇到一些看似非常复杂的动画,不知该如何下手,今天的这篇文章中我会讲到如何利用CADisplayLink和CAShapeLayer来构建一些复杂的动画,希望能在你下次构建动画中,给你一 ...
- Mac > 编写跨平台桌面应用开发工具,基于 Web 技术
Electron: The Electron framework lets you write cross-platform desktop applications using JavaScript ...
- Android卸载程序之后跳转到指定的反馈页面
一个应用被用户卸载肯定是有理由的,而开发者却未必能得知这一重要的理由,毕竟用户很少会主动反馈建议,多半就是用得不爽就卸,如果能在被卸载后获取到用户的一些反馈,那对开发者进一步改进应用是非常有利的.目前 ...