您的位置:首页 > 其它

helloworld set方法注入

2013-02-25 16:52 375 查看
首先写class

dao

public class StudentDao {
public void save(User u){
System.out.println("save "+u.getName()+" success!");
}
}


 

service

public class StudentService {
private StudentDao dao;

public StudentDao getDao() {
return dao;
}

public void setDao(StudentDao dao) {
this.dao = dao;
}

public void add(User u){
dao.save(u);
}

}


再写beans.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="S_dao" class="com.sl.dao.StudentDao"></bean>
<bean id="S_service" class="com.sl.service.StudentService">
<property name="dao" ref="S_dao"></property>
</bean>
</beans>

用property就是是代表着用set方法依赖注入

 

 

测试类

test

public class test {
@Test
public void testOne(){
ApplicationContext appl=new ClassPathXmlApplicationContext("beans.xml");
StudentService service=(StudentService)appl.getBean("S_service");

User u=new User("hong");
service.add(u);
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐