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;
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();
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="personDao" class="cn.dao.imp.PersonDaoBean"></bean>
<bean id="personService" class="cn.service.imp.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>
</beans>