package junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tiger.service.imple.PeopleService; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PeopleService peopleService = (PeopleService) ctx.getBean("peopleService"); peopleService.save(); } }
package com.tiger.service.imple; public interface PeopleService { public void save(); }
package com.tiger.service.imple; public class PeopleServiceBean implements PeopleService { /* * (non-Javadoc) * * @see com.tiger.service.imple.PeopleService#save() */ public void save() { System.out.println("我是save()方法"); } }
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="peopleService" class=" com.tiger.service.imple.PeopleServiceBean"></bean> </beans>
结果:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. 我是save()方法