spring学习笔记--bean的配置

构造器方式注入

Performer.java文件:

package com.springinaction.springidol;


public interface Performer {
	void perform() throws PerformanceException;
}

 PerformanceException.java:

package com.springinaction.springidol;

public class PerformanceException extends Exception {

	
	public PerformanceException() {
		
	}
	
	public PerformanceException(String str) {
		super(str);
	}
}

一:简单值的注入

Juggler.java:这个是等会要进行注入配置的java文件,他有beanBags,name两个属性,并都有自己的默认值。

package com.springinaction.springidol;

import com.springinaction.springidol.PerformanceException;
import com.springinaction.springidol.Performer;

public class Juggler implements Performer {

	private int beanBags = 3;
	private String name = "default";
	
	public Juggler(int beanBags,String name) {
		this.beanBags = beanBags;
		this.name = name;
	}
	
	public Juggler() {
	}
	public void perform() throws PerformanceException {
		System.out.println(name+"--Juggling--" + beanBags +"...beanBags");
		
	}

}

 接下来才是比较激动人心的时刻了,bean文件的配置,名称为spring-idol.xml文件,位置跟前面的java文件在同一个包中。

construtor-arg中的属性:name用来配置对应javabean中属性的名称,value用来配置javabean中简单属性类型的值,ref用来配置javabean中引用类型的值,index用来制定将某个值配置到javabean中构造方法对应顺序位置的值,type用来指定即将转换到javabean中的类型。

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd" >
    <bean name="duke" class="com.springinaction.springidol.Juggler">
    	<!-- name表示属性的名称,value为属性的值 -->
    	<constructor-arg name="beanBags" value="13"/>
    	<constructor-arg name="name" value="duke"/>
    </bean> 
</beans>

 

二:注入对象

假设Juggler类中存在一个Instructment对象

Instructment.java

package com.springinaction.springidol;

public class Instructment {

	private String name;

	public Instructment(String name) {
		this.name = name;
	}
	public void sayName() {
		System.out.println("Instructment has name=" +name );
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.name;
	}
}

 Juggler.java

 

package com.springinaction.springidol;

import com.springinaction.springidol.PerformanceException;
import com.springinaction.springidol.Performer;

public class Juggler implements Performer {

	private String name = "default";
	private int beanBags = 3;
	private Instructment instructment;
	
	public Juggler(String name, int beanBags, Instructment instructment) {
		super();
		this.name = name;
		this.beanBags = beanBags;
		this.instructment = instructment;
	}
	public void perform() throws PerformanceException {
		System.out.println(name+"--Juggling--" + beanBags +"...beanBags is show-----" + instructment);
		
	}

}

 

spring-idol.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
        
    <bean name="instructment" class="com.springinaction.springidol.Instructment">
    	<constructor-arg name="name" value="instruct"/>
    </bean>   
        
     
    <bean name="duke" class="com.springinaction.springidol.Juggler">
    	<constructor-arg name="beanBags" value="15"/>
    	<constructor-arg name="name" value="duke"/>
    	<constructor-arg name="instructment" ref="instructment"/>
    </bean> 
</beans>

 三:注入集合

Juggler.java:

package com.springinaction.springidol;

import java.util.Collection;



public class Juggler implements Performer {

	private String name = "default";
	private Collection<Integer> beanBags;
	public Juggler(String name, Collection<Integer> beanBags) {
		super();
		this.name = name;
		this.beanBags = beanBags;
	}

	public void perform() throws PerformanceException {
		System.out.println(name+"--Juggling--" + beanBags +"...beanBags is show-----");
		
	}

}

 spring-idol.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
     
    <bean name="duke" class="com.springinaction.springidol.Juggler">
    	<constructor-arg name="name" value="duke"/>
    	<constructor-arg name="beanBags">
    		<list>
    			<value>1</value>
    			<value>2</value>
    			<value>3</value>
    		</list>
    	</constructor-arg>
    </bean> 
</beans>

注:这里只是介绍了集合一种比较简单的使用方式,对于map使用方式跟list差不多,只是对应的子元素为entry,entry元素中使用key与value两个属性进行赋值;对于集合中是集合或者对象的情况可以根据xml文件的提示使用对应的方式进行赋值即可,这里不再记录了。

 

 通过setter方式注入

Performer.java与 PerformanceException.java文件同上

一:简单值的注入

Juggler.java:

package com.springinaction.springidol;

import com.springinaction.springidol.PerformanceException;
import com.springinaction.springidol.Performer;

public class Juggler implements Performer {

	private String name = "default";
	private int beanBags = 3;
	
	public void perform() throws PerformanceException {
		System.out.println(name+"--Juggling--" + beanBags +"...beanBags");
		
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setBeanBags(int beanBags) {
		this.beanBags = beanBags;
	}

}

spring-idol.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
    <bean name="duke" class="com.springinaction.springidol.Juggler">
    	<property name="beanBags" value="13" />
    	<property name="name" value="duke"/>
    </bean> 
</beans>

:对象的注入

Instructment.java:

package com.springinaction.springidol;

public class Instructment {

	private String name;

	
	public void setName(String name) {
		this.name = name;
	}

	public void sayName() {
		System.out.println("Instructment has name=" +name );
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.name;
	}
}

 

 

Juggler.java:

package com.springinaction.springidol;

import com.springinaction.springidol.PerformanceException;
import com.springinaction.springidol.Performer;

public class Juggler implements Performer {

	private String name = "default";
	private int beanBags = 3;
	private Instructment instructment;
	
	public void perform() throws PerformanceException {
		System.out.println(name+"--Juggling--" + beanBags +"...beanBags is show-----" + instructment);
		
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setBeanBags(int beanBags) {
		this.beanBags = beanBags;
	}

	public void setInstructment(Instructment instructment) {
		this.instructment = instructment;
	}
	
	

}

 spring-idol.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
    <bean name="instructment" class="com.springinaction.springidol.Instructment">
    	<property name="name" value="instruct"></property>
    </bean> 
    <bean name="duke" class="com.springinaction.springidol.Juggler">
    	<property name="beanBags" value="15" />
    	<property name="name" value="duke" />
    	<property name="instructment" ref="instructment"></property>
    </bean> 
</beans>

 :集合的注入

Juggler.java:

package com.springinaction.springidol;

import java.util.Collection;

import com.springinaction.springidol.PerformanceException;
import com.springinaction.springidol.Performer;

public class Juggler implements Performer {

	private String name = "default";
	private Collection<Integer> beanBags;
	
	public void perform() throws PerformanceException {
		System.out.println(name+"--Juggling--" + beanBags +"...beanBags is show-----");
		
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setBeanBags(Collection<Integer> beanBags) {
		this.beanBags = beanBags;
	}
}

 spring-idol.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" >
    <bean name="duke" class="com.springinaction.springidol.Juggler">
    	<property name="name" value="duke" />
    	<property name="beanBags">
    		<list>
    			<value>1</value>
    			<value>2</value>
    			<value>3</value>
    		</list>
    	</property>
    </bean> 
</beans>

 到这里spring bean的xml文件注入方式基本上就记录完毕了,下面将记录bean的作用域问题。

bean的作用域

1.spring默认所有的bean都是单例的,当容器分配一个bean(不论通过装配还是调用容器的getBean方法),他总是返回bean的一个实例。那如何配置Spring让其覆盖默认的单例配置呢?

spring在bean元素中为我们提供了scope属性,通过设置这个属性的值为prototype就可以在每次请求的时候都会产生一个实例了。具体实现:

<bean name="duke" class="com.springinaction.springidol.Juggler" scope="prototype">

 scope属性还有以下几个值:

singleton 在每一个Spring容器中,一个bean定义只有一个对象实例(默认)

prototype 允许bean的定义可以被实例化任意次(每次调用都会产生一个实例)

request  在一次http请求中,每个bean定义对应一个实例,该作用域仅在基于web的spring上下文(SpringMvc)中才有效

session  在一个http session中,每个bean定义对应一个实例,该作用域仅在基于web的spring上下文(SpringMvc)中才有效

global-session  在一个全局http session中,每个bean定义对应一个实例,该作用域仅在PortLet上下文中才有效

 

 2.bean的初始化与销毁

在bean元素中可以设置bean的初始化与销毁的方法,具体的做法如下:

 

package com.springinaction.springidol;

import java.util.Collection;

import com.springinaction.springidol.PerformanceException;
import com.springinaction.springidol.Performer;

public class Juggler implements Performer {

	private String name = "default";
	private Collection<Integer> beanBags;
	public void myInit() {
		System.out.println("dosomething here to init bean");
	}
	public void myDestory() {
		System.out.println("dosomething here to destory bean");
	}
	public void perform() throws PerformanceException {
		System.out.println(name+"--Juggling--" + beanBags +"...beanBags is show-----");
		myDestory();//执行销毁当前bean
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setBeanBags(Collection<Integer> beanBags) {
		this.beanBags = beanBags;
	}
}

 

 

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd" >
    <bean name="duke" class="com.springinaction.springidol.Juggler" 
    	  scope="prototype"
    	  init-method="myInit"
    	  destroy-method="myDestory">
    	<property name="name" value="duke" />
    	<property name="beanBags">
    		<list>
    			<value>1</value>
    			<value>2</value>
    			<value>3</value>
    		</list>
    	</property>
    </bean> 
</beans>

 

 当然还可以配置全局的初始化以及销毁bean的方法:

 

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd" 
        default-init-method="myInit"
        default-destroy-method="myDestory"
        >
    <bean name="duke" class="com.springinaction.springidol.Juggler" 
    	  scope="prototype">
    	<property name="name" value="duke" />
    	<property name="beanBags">
    		<list>
    			<value>1</value>
    			<value>2</value>
    			<value>3</value>
    		</list>
    	</property>
    </bean> 
</beans>

 

 测试类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springinaction.springidol.PerformanceException;
import com.springinaction.springidol.Performer;


public class Test {
	public static void main(String[] args) throws PerformanceException {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
		Performer performer = (Performer)ctx.getBean("duke");
		performer.perform();
	}
}

 

 

猜你喜欢

转载自forsave.iteye.com/blog/2291568