1.下载spring并添加到项目中
commons-loggin-1.2.jar
log4j-1.2.17.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.ja
2.编写Spring配置文件
<!--扫描包中注解标注的类-->
<context:component-scan base-package="cn.zhiyi.service,cn.zhiyi.dao,cn.zhiyi.aop"/>
<!--<bean class="aop.UserServiceLogger"/>-->
<bean class="cn.zhiyi.aop.UserServiceLogger"/>
<aop:aspectj-autoproxy/>
<!--Dao-->
<!--<bean id="dao" class="cn.zhiyi.dao.UserDaoImpl"></bean>-->
<!--Service-->
<!--<bean id="service" class="cn.zhiyi.service.UserServiceImpl" p:dao-ref="dao">-->
<!--<!–<property name="dao" ref="dao"/>–>-->
<!--<!–通过定义的单参数构造方法为userService的dao属性赋值–>-->
<!--<constructor-arg>-->
<!--<!–引用id为userDao的对象为userService的dao属性赋值–>-->
<!--<ref bean="dao"/>-->
<!--</constructor-arg>-->
<!--</bean>-->
<!--Logger-->
<!--<bean id="userLogger" class="cn.zhiyi.aop.UserServiceLogger"/>-->
<!--<bean id="theLogger" class="cn.zhiyi.aop.ErrorLogger"/>-->
<!--<bean id="afterLogger" class="cn.zhiyi.aop.AfterLogger"/>-->
<!--<bean id="aroundLogger" class="cn.zhiyi.aop.AroundLogger"/>-->
<!--<aop:config>-->
<!--定义一个切入点表达式,并命名为“pointcut”-->
<!--<aop:pointcut id="pointcut" expression="execution(public * addNewUser(cn.zhiyi.entity.User))"/>-->
<!--引用包含增强方法的bean-->
<!--<aop:aspect ref="userLogger">-->
<!--将before()方法定义前置增强并引用pointcut切入点-->
<!--<aop:before method="before" pointcut-ref="pointcut"></aop:before>-->
<!--将afterReturning()方法定义后为后置增强并引用pointcut切入点-->
<!--通过returning属性指定为名为result的参数注入返回值-->
<!--<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>-->
<!--</aop:aspect>-->
<!--</aop:config>-->
<!--<aop:config>-->
<!--<aop:pointcut id="pointcut" expression="execution(* cn.zhiyi.service.UserService.*())"/>-->
<!--将afterThrowing()方法定义为异常抛出增强并引用pointcut切入点-->
<!--<aop:aspect ref="theLogger">-->
<!--<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>-->
<!--</aop:aspect>-->
<!--</aop:config>-->