项目(包)列表:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--spring 配置文件位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!--spring 监听器--> <listener> <description>spring监听器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?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-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <bean id="AopMethod" class="yingjun.aop.AopMethod"> </bean> <bean id="u" class="yingjun.dao.impl.UserDaoImpl"> </bean> <bean id="userService" class="yingjun.service.impl.UserServiceImpl"> <property name="userdao" ref="u"></property> </bean> <aop:config> <aop:pointcut expression="execution( * yingjun.service..*.*(..))" id="servicepointcut"/> <aop:aspect id="myaspect" ref="AopMethod"> <!--对应expression执行前运行 --> <aop:before method="beforeMethod" pointcut-ref="servicepointcut"/> <!--对应expression执行后返回正常运行 --> <aop:after-returning method="AfterReturningMethod" pointcut-ref="servicepointcut"/> <!--对应expression抛出异常运行 --> <aop:after-throwing method="AfterThrowing" pointcut-ref="servicepointcut"/> <!--对应expression执行后运行(不管是否抛出异常都会执行) --> <aop:after method="AfterMethod" pointcut-ref="servicepointcut"/> </aop:aspect> </aop:config> </beans>
package yingjun.aop; public class AopMethod { public void beforeMethod(){ System.out.println("before method...AOP!!!!"); } public void AfterReturningMethod(){ System.out.println("After Returning normal...AOP!!!!"); } public void AfterMethod(){ System.out.println("After method...AOP!!!!"); } public void AfterThrowing(){ System.out.println("After Throwing...AOP!!!!"); } }
package yingjun.service; import yingjun.model.User; public interface UserServiceI { /*用户操作*/ public void DoUser(User use); }
package yingjun.service.impl; import yingjun.dao.UserDaoI; import yingjun.model.User; import yingjun.service.UserServiceI; public class UserServiceImpl implements UserServiceI { private UserDaoI userdao; public void DoUser(User user) { userdao.SaveUser(user); } public UserDaoI getUserdao() { return userdao; } public void setUserdao(UserDaoI userdao) { this.userdao = userdao; } }
package yingjun.dao; import yingjun.model.User; public interface UserDaoI { public void AddUser(User user ); public void DeleteUser(User user ); public void SaveUser(User user ); }
package yingjun.dao.impl; import yingjun.dao.UserDaoI; import yingjun.model.User; public class UserDaoImpl implements UserDaoI{ public void AddUser(User user ) { System.out.println("增加用户成功"); } public void DeleteUser(User user ) { System.out.println("删除用户成功"); } public void SaveUser(User user ) { System.out.println("保存用户成功"); } }
package yingjun.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import yingjun.model.User; import yingjun.service.UserServiceI; public class SpringTest { @Test public void springtest(){ ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml"); UserServiceI us=(UserServiceI)ac.getBean("userService"); User user=new User(); us.DoUser(user); } }
运行结果: