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、运行结果如下:










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值