Set注入

Dao层:

 

Dao层接口:
public interface FruitDao {
	public void create();
}
实现类:
public class FruitDaoImpl implements FruitDao {
	public void create(){
		System.out.println("Dao层方法create被调用");
	}
}

服务层接口:
public interface FruitService {
	public void create();
}

服务层实现类:
public class FruitServiceImpl implements FruitService {
	private FruitDao fruitDao;
	
	@Override
	public void create() {
		fruitDao.create();
	}

	public void setFruitDao(FruitDao fruitDao) {
		this.fruitDao = fruitDao;
	}
}

Test方法:
public class Test {

	public static void main(String[] args) {
		ApplicationContext acx = new ClassPathXmlApplicationContext(
				"chapter2.xml");
		FruitService fruitService =(FruitService)acx.getBean("fruitService");
		fruitService.create();
	}
}

XML配置:
<?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-2.5.xsd">
	<bean id="fruitDao" class="dao.impl.FruitDaoImpl"/>
	<bean id="fruitService" class="service.impl.FruitServiceImpl">
		<property name="fruitDao" ref="fruitDao" />
	</bean>
</beans>

 

 

 

 

 

需要注意的地方:

<property name="fruitDao" ref="fruitDao" />

 

这里的name="fruitDao"并不是私有属性的名字,实际上Spring会根据这个名字找对应的setFruitDao这个方法来达到注入的一个目的. fruitDao是set方法的简单名称

 

 

 

 

使用set注入基本与复杂类型:

MyClass:
public class MyClass {
	private String id;
	private String name;
	private Set<Student> students = new HashSet<Student>();
	
	public MyClass() {
		super();
	}

	public MyClass(String name) {
		super();
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}
}

Student:
public class Student {
	private String id;
	private String name;
	private Integer age;
	private MyClass myClass;

	public Student() {
		super();
	}

	public Student(String name) {
		super();
		this.name = name;
	}

	public Student(String name, MyClass myClass) {
		super();
		this.name = name;
		this.myClass = myClass;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public MyClass getMyClass() {
		return myClass;
	}

	public void setMyClass(MyClass myClass) {
		this.myClass = myClass;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<bean id="myclass" class="model.MyClass">
		<property name="id" value="class001" />
		<property name="name" value="java0801" />
	</bean>
	<bean id="student" class="model.Student">
		<property name="id" value="001" />
		<property name="name" value="wdpc" />
		<property name="age" value="22" />
		<property name="myClass" ref="myclass" />
	</bean>
</beans>

 

 

 

 

Test测试:

 

 

 

public class Test {
	public static void main(String[] args) {
		ApplicationContext acx = new ClassPathXmlApplicationContext(
				"chapter2.xml");
		Student student = (Student) acx.getBean("student");
		System.out.println(student.getAge() + ":"
				+ student.getMyClass().getName());
	}
}

 

 记住,使用set注入,一定要给类提供一个无参的构造函数,否则Spring不能实例化类的.
value是配置基本类型值
ref是配置复杂类型值.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值