在/cn下有一个文件(但是这个可以从其他方法中抽取出来)PersonService.java
- package cn;
- public interface PersonService {
- public abstract void save();
- }
在/cn/imp文件夹下有一个java文件PersonServiceBean.java
package cn.imp;
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
import cn.PersonService;
public class PersonServiceBean implements PersonService {
/* (non-Javadoc)
* @see cn.imp.PersonService#save()
*/
public void init()
{
System.out.println("我被初始化了");
}
public void save()
{
System.out.println("我是save方法");
}
public PersonServiceBean()
{
System.out.println("我被实例化了");
}
}
在junit.test下面SpringTest.java
- package junit.test;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.imp.PersonServiceBean;
- public class SpringTest
- {
- @Test
- public void instanceSpring()
- {
- ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
- PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService");
- personServiceBean.save();
- }
- }
在src文件夹下面有个配置文件
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.imp.PersonServiceBean" init-method="init"></bean> </beans>