cn.dao
- package cn.dao;
- public interface PersonDao {
- public abstract void add();
- }
cn.dao.imp
- package cn.dao;
- public interface PersonDao {
- public abstract void add();
- }
cn.service
- package cn.service;
- public interface PersonService {
- public abstract void save();
- }
cn.service.imp
package cn.service.imp;
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
import cn.dao.PersonDao;
import cn.service.PersonService;
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name;
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()
{
System.out.println("执行save()方法");
personDao.add();
}
}
junit.test
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();
System.out.println(personServiceBean.getName());
ctx.close();
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="personService" class="cn.service.imp.PersonServiceBean">
<property name="personDao">
<bean class="cn.dao.imp.PersonDaoBean"></bean>
</property>
<property name="name" value="sunxiaojie"></property>
</bean>
</beans>