J2EE系列之Spring4学习笔记(三)--IOC详解(依赖注入)

现在讲一下如何通过Spring装配一个对象实例,并给对象中的属性赋值。

1.新建一个工程:Spring402-02

2.新建一个类People:

package com.test.entity;

public class People {

	private int id;
	private String name;
	private int age;
	
	
	public People() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	public People(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}


	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
	
}
这里定义了几个属性,并生成了People类的两个构造函数。

3.装配一个bean,Spring配置文件为:

<?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="people" class="com.test.entity.People"></bean>
	
	
</beans>

4.写测试方法:

package com.test.test;

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

import com.test.entity.People;

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		People people = (People) ac.getBean("people");
		System.out.println(people);
		
	}
}

运行这个测试方法:


这里我们在配置文件中只是生成了一个简单的People类对象,并没有给对象中的属性赋值。


下面通过几种不同的方法给People对象的属性注入值。

1.属性注入方法

修改Spring配置文件,里面生成一个新的People对象:

<bean id="people2" class="com.test.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
	</bean>
这里使用了属性注入方法来生成一个新的People对象实例,使用<property>标签给各个属性赋值。

修改测试方法:

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		People people = (People) ac.getBean("people");
		System.out.println(people);
		
		People people2 = (People) ac.getBean("people2");
		System.out.println(people2);
		
		
	}
}

运行测试方法:


可以看到这里的第二个对象people2中各个属性已经有值了。

2.构造函数注入(通过类型)

People类中我们生成了有参的构造函数,这里通过构造函数中各个形参的类型类给对象注入值。修改Spring配置文件,添加一个新的bean:

<bean id="people3" class="com.test.entity.People">
		<constructor-arg type="int" value="2"></constructor-arg>
		<constructor-arg type="String" value="李四"></constructor-arg>
		<constructor-arg type="int" value="22"></constructor-arg>
	</bean>
这里我们通过有参的构造函数来给各个属性赋值。

修改测试方法为:

public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		People people = (People) ac.getBean("people");
		System.out.println(people);
		
		People people2 = (People) ac.getBean("people2");
		System.out.println(people2);
		
		People people3 = (People) ac.getBean("people3");
		System.out.println(people3);
		
	}


运行测试方法:


可以看到这里生成的新的People对象people3中的各个属性也是有值的。

3.构造方法注入(通过索引)

上面的注入方法是通过构造函数中各个形参的类型完成的,现在来通过各个形参的索引完成注入。修改Spring配置文件,生成一个新的bean:

<bean id="people4" class="com.test.entity.People">
		<constructor-arg index="0" value="3"></constructor-arg>
		<constructor-arg index="1" value="王五"></constructor-arg>
		<constructor-arg index="2" value="33"></constructor-arg>
	</bean>

这里通过构造函数中各个形参的索引值来注入值。

修改测试方法:

public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		People people = (People) ac.getBean("people");
		System.out.println(people);
		
		People people2 = (People) ac.getBean("people2");
		System.out.println(people2);
		
		People people3 = (People) ac.getBean("people3");
		System.out.println(people3);
		
		People people4 = (People) ac.getBean("people4");
		System.out.println(people4);
		
	}

运行测试方法:


这里通过索引注入了值。

4.构造函数注入(联合使用)

所谓的联合使用就是把类型和索引两者联合在一起。修改Spring配置文件,生成一个新的bean:

<bean id="people5" class="com.test.entity.People">
		<constructor-arg index="0" type="int" value="4"></constructor-arg>
		<constructor-arg index="1" type="String" value="赵六"></constructor-arg>
		<constructor-arg index="2" type="int" value="44"></constructor-arg>
	</bean>

修改测试方法为:

public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		People people = (People) ac.getBean("people");
		System.out.println(people);
		
		People people2 = (People) ac.getBean("people2");
		System.out.println(people2);
		
		People people3 = (People) ac.getBean("people3");
		System.out.println(people3);
		
		People people4 = (People) ac.getBean("people4");
		System.out.println(people4);
		
		People people5 = (People) ac.getBean("people5");
		System.out.println(people5);
		
		
	}

运行测试方法:



5.使用非静态工厂注入

这种方式首先要建一个工厂类负责生成People类的对象。新建一个工厂类PeopleFactory:

package com.test.factory;

import com.test.entity.People;

public class PeopleFactory {

	public People createPeople(){
		People p = new People();
		p.setId(5);
		p.setName("七七");
		p.setAge(55);
		return p;
	}
}

这个工厂类中有个非静态的方法createPeople来生成一个people对象,因为生成方法是非静态的,所以这个注入方法叫非静态工厂注入。

修改Spring配置文件,生成一个新的bean:

<bean id="peopleFactory" class="com.test.factory.PeopleFactory">
  	</bean>
  	
  	<bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>

这里第一个<bean>生成了一个造人工厂类的实例对象,第二个<bean>表示使用造人工厂中的createPeople方法返回的结果。

修改测试方法:

public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		People people = (People) ac.getBean("people");
		System.out.println(people);
		
		People people2 = (People) ac.getBean("people2");
		System.out.println(people2);
		
		People people3 = (People) ac.getBean("people3");
		System.out.println(people3);
		
		People people4 = (People) ac.getBean("people4");
		System.out.println(people4);
		
		People people5 = (People) ac.getBean("people5");
		System.out.println(people5);
		
		People people7 = (People) ac.getBean("people7");
		System.out.println(people7);
		
	}
运行测试方法:

这里通过非静态工厂方法给新的People对象注入了值。


6.使用静态工厂方法注入

这里所谓的静态工厂就是指生成People对象的方法为静态方法。

新建一个造人工厂类:

package com.test.factory;

import com.test.entity.People;

public class PeopleFactory2 {

	public static People createPeople(){
		People p = new People();
		p.setId(6);
		p.setName("八");
		p.setAge(66);
		return p;
	}
}

这里的createPeople方法为静态方法。

修改Spring配置文件,使用静态工厂方法给一个新的bean赋值:

<bean id="people8" class="com.test.factory.PeopleFactory2" factory-method="createPeople"></bean>

这里的方法为静态方法,直接使用类名.方法名的方式调用该静态方法。这里使用people8代表静态方法createPeople返回的结果。

修改测试方法:

public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		People people = (People) ac.getBean("people");
		System.out.println(people);
		
		People people2 = (People) ac.getBean("people2");
		System.out.println(people2);
		
		People people3 = (People) ac.getBean("people3");
		System.out.println(people3);
		
		People people4 = (People) ac.getBean("people4");
		System.out.println(people4);
		
		People people5 = (People) ac.getBean("people5");
		System.out.println(people5);
		
		People people7 = (People) ac.getBean("people7");
		System.out.println(people7);
		
		People people8 = (People) ac.getBean("people8");
		System.out.println(people8);
	}

运行测试方法:

这里通过静态工厂方法给People对象的属性进行了赋值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值