spring(上一篇文章使用构造器,这一篇使用set方法)
2012-05-25 08:43
761 查看
cn.dao
[java] view
plaincopy
<pre name="code" class="java">package cn.dao;
public interface PersonDaoInterface {
public abstract void add();
}
cn.dao.imp
[java] view
plaincopy
package cn.dao.imp;
import cn.dao.PersonDaoInterface;
public class PersonDao implements PersonDaoInterface {
public void add()
{
System.out.println("执行PersonDao.add()");
}
}
cn.service
[java] view
plaincopy
package cn.service;
public interface PersonService {
public abstract void save();
}
cn.service.imp
也可以这样写
juintest
[java] view
plaincopy
package junit.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.service.imp.PersonServiceBean;
public class SpringTest
{
@Test
public void instanceSpring()
{
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService");
personServiceBean.save();
ctx.close();
}
}
bean.xml
[html] view
plaincopy
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
<bean id="personDao" class="cn.dao.imp.PersonDao"></bean>
<bean id="personService" class="cn.service.imp.PersonServiceBean">
</bean>
</beans>
[java] view
plaincopy
<pre name="code" class="java">package cn.dao;
public interface PersonDaoInterface {
public abstract void add();
}
cn.dao.imp
[java] view
plaincopy
package cn.dao.imp;
import cn.dao.PersonDaoInterface;
public class PersonDao implements PersonDaoInterface {
public void add()
{
System.out.println("执行PersonDao.add()");
}
}
cn.service
[java] view
plaincopy
package cn.service;
public interface PersonService {
public abstract void save();
}
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; } @Resource public 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
package junit.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.service.imp.PersonServiceBean;
public class SpringTest
{
@Test
public void instanceSpring()
{
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService");
personServiceBean.save();
ctx.close();
}
}
bean.xml
[html] view
plaincopy
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
<bean id="personDao" class="cn.dao.imp.PersonDao"></bean>
<bean id="personService" class="cn.service.imp.PersonServiceBean">
</bean>
</beans>
相关文章推荐
- spring使用set方法注入的常见类型写法
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- 转一篇文章,以便在后续文章中讲解——开源gis项目Mitab在C#中的使用方法
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 一篇关于STL标准模板库使用方法的经典文章!
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- Spring容器框架、spring ioc、两种注入方法set注入与构造器注入
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 一篇总结的很好的Spring data jpa 文章,里面包含多种查询方式,可以结合api使用
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 从spring容器中得到bean_使用类构造器实例化_使用静态工厂方法实例化_使用实例工厂方法实例化_例子
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 转载一篇nm命令使用的文章,虽然没用用这个方法解决但是文章很好
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入
- 通过 @Autowired的使用来消除 set ,get方法 ---- Spring · JPA ·hibernate 的使用方法