1. 程式人生 > >Spring setter注入

Spring setter注入

定義User類

public class User {

	private String name;// 姓名

	private Integer age;

	public Integer getAge() {
		return age;
	}

	public void setMsg(Integer age) {
		this.age= age;
	}

	

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "名字為:" + name+";年齡:"+age;
	}

配置檔案

<?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:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">




	<bean id="user" class="com.javaketang.test.User">
		<property name="name" value="Alan"></property>
		<property name="age" value="20"></property>
	</bean>



</beans>

編寫測試類

	public static void main(String[] args) {

		ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");

		User u=ap.getBean("user",User.class);

		System.out.println(u);
	}

 

輸出結果為   名字為:Alan;年齡:20

 

setter注入的強大之處在於還能引用其他型別的bean物件。

增加一個Dept類

public class Dept{

	private String department;// 部門
	

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department= department;
	}

	@Override
	public String toString() {

		return "部門:" + department;
	}

修改之前的User類

 

public class User {

	private String name;// 姓名

	private Integer age;

    private Dept dept;

    public void setDept(Dept dept){
    this.dept=dept;
    }

    public Dept getDept(){
        return dept;
    }

	public Integer getAge() {
		return age;
	}

	public void setMsg(Integer age) {
		this.age= age;
	}

	

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "名字為:" + name+";年齡:"+age+";"+dept;
	}

 

在配置檔案上新增一個屬性

<?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:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="dept" class="com.javaketang.test.Dept">
        <property name="department" value="營銷部"></property>
     </bean>


	<bean id="user" class="com.javaketang.test.User">
		<property name="name" value="Alan"></property>
		<property name="age" value="20"></property>
        <property name="dept" ref="dept"></property>    
	</bean>

</beans>

執行測試類

將會輸出  名字為:Alan;年齡:20;部門:營銷部

 

注入boolean值

public class User {

	private String name;// 姓名

	private Integer age;

    private Dept dept;

    private boolean close;

    public void set(boolean close){
    this.close=close;
    }

    public boolean isClose(){
    return close;
    }

    public void setDept(Dept dept){
    this.dept=dept;
    }

    public Dept getDept(){
        return dept;
    }

	public Integer getAge() {
		return age;
	}

	public void setMsg(Integer age) {
		this.age= age;
	}

	

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "名字為:" + name+";年齡:"+age+";"+dept+";標記:"+close;
	}

配置檔案

<?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:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="dept" class="com.javaketang.test.Dept">
        <property name="department" value="營銷部"></property>
     </bean>


	<bean id="user" class="com.javaketang.test.User">
		<property name="name" value="Alan"></property>
		<property name="age" value="20"></property>
        <property name="dept" ref="dept"></property>
        <-! 在Spring中boolean值得配置,支援這幾種:true/false、1/0、on/off、yes/no -->
        <property name="close" value="true"></property>    
	</bean>

</beans>

執行測試程式碼,將輸出    名字為:Alan;年齡:20;部門:營銷部;標記:true

 

(中智軟體科技學校)