Java Web實戰05--Spring之bean引用

操作實踐bean直接的引用,具體流程如下。此時省略了pom.xml設置,和之前一樣。主要實現實例依賴外部bean、內部bean的配置。

1、建立兩個類,分別Bag類,Person類,在person類中會引用bag類。

bag類:

package com.yefeng.spring.spring3;
/**
 * @author yefengzhichen
 * 2016年7月3日
 */
public class Bag {
	private String brand;
	private String country;
	private double price;
	private int capacity;
	
	public Bag() {
		super();
	}

	public Bag(String brand, String country, double price) {
		super();
		this.brand = brand;
		this.country = country;
		this.price = price;
	}

	public Bag(String brand, String country, double price, int capacity) {
		super();
		this.brand = brand;
		this.country = country;
		this.price = price;
		this.capacity = capacity;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getCapacity() {
		return capacity;
	}

	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}
	
	@Override
	public String toString() {
		return "Bag [brand=" + brand + ", country=" + country + ", price=" 
				+ price + ", capacity=" + capacity + "]";
	}		
}
person類:
package com.yefeng.spring.spring3;
/**
 * @author yefengzhichen
 * 2016年7月4日
 */
public class Person {
	private String name;
	private int age;
	private Bag bag;
		
	public Person() {
		super();
	}
	public Person(String name, int age, Bag bag) {
		super();
		this.name = name;
		this.age = age;
		this.bag = bag;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Bag getBag() {
		return bag;
	}
	public void setBag(Bag bag) {
		this.bag = bag;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", bag=" + bag + "]";
	}	
}


2、建立bean配置文件,其中設置person依賴與bag實例,其中分別依賴外部bean、內部bean,最後一個是用給依賴對象賦值爲null。

<?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 id="ata" class="com.yefeng.spring.spring3.Bag">
		<constructor-arg value="ata" type="java.lang.String"></constructor-arg>
		<constructor-arg value="China"></constructor-arg>
		<constructor-arg value="120"></constructor-arg>
		<constructor-arg value="5"></constructor-arg>
	</bean>

	<!-- 引用外部bean -->
	<bean id="Jack" class="com.yefeng.spring.spring3.Person">
		<property name="name" value="Jack"></property>
		<property name="age" value="20"></property>
		<property name="bag" ref="ata"></property>
	</bean>

	<bean id="Tom" class="com.yefeng.spring.spring3.Person">
		<property name="name" value="Tom"></property>
		<property name="age" value="15"></property>
		<!-- 內部bean -->
		<property name="bag">
			<bean class="com.yefeng.spring.spring3.Bag">
				<constructor-arg value="nike"></constructor-arg>
				<constructor-arg value="America"></constructor-arg>
				<constructor-arg value="200"></constructor-arg>
				<constructor-arg value="10"></constructor-arg>
			</bean>
		</property>
	</bean>

	<!-- 用null賦值 -->
	<bean id="Lucy" class="com.yefeng.spring.spring3.Person">
		<constructor-arg value="Lucy"></constructor-arg>
		<constructor-arg value="18"></constructor-arg>
		<constructor-arg>
			<null />
		</constructor-arg>
	</bean>

</beans>


3、app運行代碼:

package com.yefeng.spring.spring3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person jack = (Person) context.getBean("Jack");
        System.out.println(jack);    
        
        Person tom = (Person) context.getBean("Tom");
        System.out.println(tom);    
        
        Person lucy = (Person) context.getBean("Lucy");
        System.out.println(lucy);     
    }
}

4、運行結果如下:










發佈了118 篇原創文章 · 獲贊 60 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章