Spring实战读书笔记 第二章 装配Bean
1,依赖注入(Dependency Injection)和控制反转(Inversion of Control)是同一个概念。具体含义是:当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在
传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在Spring里,创建被调用者的工作不再由调用者来完成,因此称为控制反转;创建被调用者 实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入。
2,Spring配置Bean XML解析
如下代码,为对应的配置XML。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="duke" class="chp02.springdol.Juggler">
<constructor-arg value="15" />
</bean>
<bean id="poem" class="chp02.springdol.Poem"></bean>
<bean id="juggle" class="chp02.springdol.PoeticJuggle" >
<constructor-arg ref="poem"></constructor-arg>
<constructor-arg value="15"></constructor-arg>
</bean>
</beans>
Spring核心框架自带10个命名空间配置:
-1,aop:为声明切面以及将@AspectJ注解的类代理为Spring切面提供配置元素。
-2,beans:支持声明Bean和装配Bean,是Spring最核心也是最原始的命名空间。
-3,context:为配置Spring上下文提供了配置元素,包括自动检测和自动装配Bean、注入非Spring直接管理的对象。
-4,jee:提供了与Java EE API的集成,例如JNDI和EJB。
-5,jms:为声明消息驱动的POJO提供了配置元素。
-6,lang:支持配置由Grovy、JRuby或BeanShell等脚本实现的bean。
-7,mvc:启用Spring MVC功能。
-8,oxm:支持Spring的对象到XML映射配置。
-9,tx:提供声明式事务配置。
-10,util:提供工具类配置。
3,注入bean对象,并装配:
<bean id="duke" class="chp02.springdol.Juggler"></bean>
package chp02.springdol;
/**
*
* 类功能说明:接口
*
* @author Jing
*
*/
public interface Perform {
/**
*
* 方法说明:
*
* Author: Jing Create Date: Jan 6, 2015 10:19:03 AM History: Jan 6, 2015
* 10:19:03 AM Jing Created.
*
*/
void perform();
}
/**
*
* @(#) Juggler.java
* @Package chp02.springdol
*
* Copyright © JING Corporation. All rights reserved.
*
*/
package chp02.springdol;
/**
* 类描述:
*
* @author: Jing History: Jan 6, 2015 10:43:45 AM Jing Created.
*
*/
public class Juggler implements Perform {
private int beanBags = 3;
public void setBeanBags(int beanBags) {
this.beanBags = beanBags;
}
public void perform() {
System.out.println("Juggler perform(),beanBags : " + this.beanBags);
}
}
测试类:
public class MainTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("chp02/springdol/spring.xml");
System.out.println(ctx);
Perform pf = (Perform) ctx.getBean("duke");
pf.perform();
}
}
4,通过构造器注入:
对对应的Juggler
增加构造方法,代码如下:
public Juggler() {
}
public Juggler(int beanBags) {
this.beanBags = beanBags;
}
对参数进行注入,代码如下:
<bean id="duke" class="chp02.springdol.Juggler">
<constructor-arg value="15" />
</bean>
通过构造参数注入引用对象:
package chp02.springdol;
/**
* 类描述:引入引用对象
*
* @author: Jing History: Jan 6, 2015 11:28:33 AM Jing Created.
*
*/
public class PoeticJuggle extends Juggler {
private Poem poem;
public PoeticJuggle(Poem poem) {
this.poem = poem;
}
public PoeticJuggle(Poem poem, int beanBags) {
super(beanBags);
this.poem = poem;
}
@Override
public void perform() {
super.perform();
System.out.println("this is PoeticJuggle!");
poem.sayPoem();
}
}
package chp02.springdol;
/**
* 类描述:
*
* @author: Jing
* History: Jan 6, 2015 11:29:35 AM Jing Created.
*
*/
public class Poem {
public void sayPoem(){
System.out.println("This is poem!");
}
}
<bean id="poem" class="chp02.springdol.Poem"></bean>
<bean id="juggle" class="chp02.springdol.PoeticJuggle" >
<constructor-arg ref="poem"></constructor-arg>
<constructor-arg value="15"></constructor-arg>
</bean>
5,通过工厂方法创建Bean
package chp02.springdol;
/**
* 类描述:测试Bean工厂注入
*
* @author: Jing
* History: Jan 6, 2015 2:05:59 PM Jing Created.
*
*/
public class Stage {
private Stage(){
}
private static class ClassHolder {
static Stage stage = new Stage();
}
/*
* 静态方法,返回Stage实例
*/
public static Stage getInstance(){
return ClassHolder.stage;
}
}
<bean id="theStage" class="chp02.springdol.Stage" factory-method="getInstance"></bean>
6,Bean的作用域,通过scope属性指定用户配置Bean的作用域。
-1,singleton:在每一个spring容器中,一个Bean定义只有一个对象实例。
-2,prototype:允许Bean的定义可以被实例化任意次(每次调用创建一个新的实例)。
-3,request:在一次HTTP请求中,每个Bean对应一个实例,该作用域仅在Web的Spring上下文中。
-4,session:在一个HttpSession中,每个Bean对应一个实例。该作用域仅在Web的Spring上下文中。
-5,global-session:在一个全局HttpSession中,每个Bean对应一个实例。该作用域仅在Portlet上下文中才有效。
7,初始化和销毁Bean:通过init-method和 destroy-method属性指定Bean创建和销毁时的方法。
package chp02.springdol;
/**
* 类描述:测试创建和销毁方法
*
* @author: Jing
* History: Jan 6, 2015 2:33:05 PM Jing Created.
*
*/
public class Auditorium {
public void init(){
System.out.println("Auditorium init!");
}
public void destroy(){
System.out.println("Auditorium destroy!");
}
}
<bean id="audit" class="chp02.springdol.Auditorium" init-method="init" destroy-method="destroy"></bean>
public class MainTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("chp02/springdol/spring.xml");
Auditorium audit = (Auditorium) ctx.getBean("audit");
System.out.println(audit);
audit =null;
}
}
8,注入Bean属性。
package chp02.springdol;
/**
* 类描述:测试属性注入
*
* @author: Jing History: Jan 6, 2015 2:43:43 PM Jing Created.
*
*/
public class Instrumentalist implements Perform {
private String song;
private Instrument inst;
@Override
public void perform() {
System.out.println("Instrumentalist perform()!");
inst.play();
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public Instrument getInst() {
return inst;
}
public void setInst(Instrument inst) {
this.inst = inst;
}
}
public interface Instrument {
void play();
}
/**
* 类描述:
*
* @author: Jing
* History: Jan 6, 2015 2:50:10 PM Jing Created.
*
*/
public class Saxophone implements Instrument{
@Override
public void play() {
System.out.println("Saxophone play()");
}
}
<bean id="saxophone" class="chp02.springdol.Saxophone"></bean>
<bean id="instrumentlist" class="chp02.springdol.Instrumentalist">
<property name="song" value="For Elise!"></property>
<property name="inst" ref="saxophone"></property>
</bean>
public class MainTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("chp02/springdol/spring.xml");
Instrumentalist ins = (Instrumentalist) ctx.getBean("instrumentlist");
ins.perform();
}
}
9,装配集合:
-1,list:装配list类型的值,允许重复。
-2,set:装配set类型的值,不允许重复。
-3,map:装配map类型的值,名称和值是任意类型。
-4,props:装配properties类型的值,名称和值必须是String。
package chp02.springdol;
import java.util.Collection;
/**
* 类描述:测试集合
*
* @author: Jing History: Jan 6, 2015 3:07:22 PM Jing Created.
*
*/
public class OneManBand implements Perform {
private Collection<Instrument> instruments;
@Override
public void perform() {
System.out.println("OneManBand perform()");
for(Instrument instrument: instruments){
instrument.play();
}
}
public Collection<Instrument> getInstruments() {
return instruments;
}
public void setInstruments(Collection<Instrument> instruments) {
this.instruments = instruments;
}
}
<bean id="band" class="chp02.springdol.OneManBand">
<property name="instruments">
<list>
<ref bean="saxophone"/>
</list>
</property>
</bean>
public class MainTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("chp02/springdol/spring.xml");
OneManBand ins = (OneManBand) ctx.getBean("band");
ins.perform();
}
}
装配Map集合:
package chp02.springdol;
import java.util.Map;
/**
* 类描述:测试Map注入
*
* @author: Jing
* History: Jan 6, 2015 3:20:15 PM Jing Created.
*
*/
public class MapManBand implements Perform{
private Map<String, Instrument> instruments;
public Map<String, Instrument> getInstruments() {
return instruments;
}
public void setInstruments(Map<String, Instrument> instruments) {
this.instruments = instruments;
}
@Override
public void perform() {
System.out.println("MapManBand perform()");
for(String key: instruments.keySet()){
Instrument ins = instruments.get(key);
ins.play();
}
}
}
<bean id="mapBand" class="chp02.springdol.MapManBand">
<property name="instruments">
<map>
<entry key="saxophone" value-ref="saxophone"></entry>
</map>
</property>
</bean>
装配Properties集合:
package chp02.springdol;
import java.util.Properties;
/**
* 类描述:Properties属性注入测试
*
* @author: Jing
* History: Jan 6, 2015 3:38:59 PM Jing Created.
*
*/
public class PropertiesDI {
private Properties pro;
public Properties getPro() {
return pro;
}
public void setPro(Properties pro) {
this.pro = pro;
}
public void outputPro(){
System.out.println(pro.toString());
}
}
<bean id="pro" class="chp02.springdol.PropertiesDI">
<property name="pro">
<props>
<prop key="A1">ASDASD</prop>
<prop key="A2">SADADADSASD</prop>
<prop key="A3">ASDASDASDDSDSDSDSASD</prop>
</props>
</property>
</bean>
10,使用表达式装配
Spring引入了表达式语言(Spring Expression Language, SpEL),SpEl特性如下:
-1,使用Bean的ID来引用Bean。
-2,调用方法和访问对象的属性。
-3,对值进行算术、关系和逻辑运算。
-4,正则表达式匹配。
-5,集合操作。
欢迎转载,但转载请注明原文链接[博客园: http://www.cnblogs.com/jingLongJun/]
[CSDN博客:http://blog.csdn.net/mergades]。
如相关博文涉及到版权问题,请联系本人。
[CSDN博客:http://blog.csdn.net/mergades]。
如相关博文涉及到版权问题,请联系本人。