maven+Spring环境搭建
一,项目结构图
二,applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="personDao" class="com.deppon.test04.dao.impl.PersonDao"></bean> <bean id="personService" class="com.deppon.test04.service.impl.PersonService"> <property name="personDao" ref="personDao"></property> </bean> </beans>
三,service
package com.deppon.test04.service; public interface IPersonService { public void processSave(); }
package com.deppon.test04.service.impl; import com.deppon.test04.dao.IPersonDao; import com.deppon.test04.service.IPersonService; public class PersonService implements IPersonService { private IPersonDao personDao; public void setPersonDao(IPersonDao personDao) { this.personDao = personDao; } public void processSave() { System.out.println("-------------from PersonService.processSave()"); personDao.save(); } }
四,dao
package com.deppon.test04.dao; public interface IPersonDao { public void save(); }
package com.deppon.test04.dao.impl; import com.deppon.test04.dao.IPersonDao; public class PersonDao implements IPersonDao { public void save() { System.out.println("------------from PersonDao.save()"); } }
五,测试类
package com.deppon.test04.service; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class PersonServiceTest { private BeanFactory factory = null; @Before public void before() { factory = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testSpring() { IPersonService personService = (IPersonService) factory.getBean("personService"); personService.processSave(); } }
转自https://blog.csdn.net/yuguiyang1990/article/details/8799307
愿你遍历山河
仍觉人间值得