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

 

(中智软件科技学校)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值