spring(上一篇文章使用构造器,这一篇使用set方法)

来源:互联网 发布:兰州知豆车体广告 编辑:程序博客网 时间:2024/05/22 06:45

cn.dao

[java] view plaincopy
  1. <pre name="code" class="java">package cn.dao;  
  2.   
  3. public interface PersonDaoInterface {  
  4.   
  5.     public abstract void add();  
  6.   
  7. }  


cn.dao.imp

[java] view plaincopy
  1. package cn.dao.imp;  
  2.   
  3. import cn.dao.PersonDaoInterface;  
  4.   
  5. public class PersonDao implements PersonDaoInterface {  
  6.     public void add()  
  7.     {  
  8.         System.out.println("执行PersonDao.add()");  
  9.     }  
  10.   
  11. }  
cn.service

[java] view plaincopy
  1. package cn.service;  
  2.   
  3. public interface PersonService {  
  4.   
  5.     public abstract void save();  
  6.   
  7. }  
cn.service.imp

package cn.service.imp;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;import cn.dao.imp.PersonDao;import cn.service.PersonService;public class PersonServiceBean implements PersonService {private String name;private PersonDao personDao;public PersonServiceBean(){}public PersonServiceBean(String name,PersonDao personDao) {this.name = name;this.personDao=personDao;}public String getName() {return name;}public void setName(String name) {this.name = name;}public PersonDao getPersonDao() {return personDao;}@Resourcepublic void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public void save(){personDao.add();//System.out.println(name);}}

也可以这样写

package cn.service.imp;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;import org.springframework.beans.factory.annotation.Qualifier;import cn.dao.imp.PersonDao;import cn.service.PersonService;public class PersonServiceBean implements PersonService {private String name;@Autowired @Qualifier("personDao") private PersonDao personDao;public PersonServiceBean(){}public PersonServiceBean(String name,PersonDao personDao) {this.name = name;this.personDao=personDao;}public String getName() {return name;}public void setName(String name) {this.name = name;}public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public void save(){personDao.add();//System.out.println(name);}}



juintest

[java] view plaincopy
  1. package junit.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.AbstractApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import cn.service.imp.PersonServiceBean;  
  9.   
  10.   
  11. public class SpringTest   
  12. {  
  13.     @Test   
  14.     public void instanceSpring()  
  15.     {  
  16.         AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});  
  17.         PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService");  
  18.         personServiceBean.save();  
  19.         ctx.close();  
  20.     }  
  21. }  

bean.xml


[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.         <context:annotation-config></context:annotation-config>  
  10.         <bean id="personDao" class="cn.dao.imp.PersonDao"></bean>  
  11.         <bean id="personService" class="cn.service.imp.PersonServiceBean">  
  12.         </bean>  
  13.   
  14. </beans>  


原创粉丝点击