首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

第三章 set流入

2012-10-07 
第三章 set注入Setter注入Dao层:Dao层接口:public interface FruitDao {public void create()}实现类:pub

第三章 set注入
Setter注入
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" 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" value="class001" />
<property name="name" value="java0801" />
</bean>
<bean id="student" 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是配置复杂类型值.

set注入也是日后使用最多的一种注入方式.

热点排行