一、
UserDAO.java:

package com.cy.dao; import com.cy.model.User; public interface UserDAO { public void save(User user); }
实现UserDAOImpl.java:

package com.cy.dao.impl; import org.springframework.stereotype.Component; import com.cy.dao.UserDAO; import com.cy.model.User; @Component public class UserDAOImpl implements UserDAO { public void save(User user) { //Hibernate //JDBC //XML //NetWork System.out.println("user saved!"); } }
切面类LogInterceptor.java:

package com.cy.aop; public class LogInterceptor { public void before() { System.out.println("method before"); } }
UserService.java:

package com.cy.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.cy.dao.UserDAO; import com.cy.model.User; @Component("userService") public class UserService { @Resource private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
配置文件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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.cy"/> <!-- 把切面类在容器中初始化 这是一个切面逻辑类的对象 --> <bean id="logInteceptor" class="com.cy.aop.LogInterceptor"></bean> <!-- aop:config aop配置 aop:pointcut: 声明一个切面,在service的add方法上面加入我们切进来的各种逻辑;给这个切面起名为servicePointcut 在aop:config下面定义了一个全局的pointCut,可以在所有的aop:aspect上面使用; (可以添加这种全局的pointCut,也可以在aop:aspect下面添加pointCut,后者只能在这个aspect下面使用) aop:aspect: 声明切面对象;它所参考的切面类的对象是logInteceptor aop:before: 在切面servicePointcut之前执行logInteceptor的before方法 --> <aop:config> <aop:pointcut id="servicePointcut" expression="execution(public * com.cy.service..*.add(..))"/> <aop:aspect id="logAspect" ref="logInteceptor"> <aop:before method="before" pointcut-ref="servicePointcut"/> </aop:aspect> </aop:config> </beans>
测试代码:
UserServiceTest.java:

package com.cy.service; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cy.model.User; public class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); System.out.println(service.getClass()); service.add(new User()); ctx.destroy(); } }
console:
二、第2个例子:
pointcut可以直接定义在aop:before/aop:after等这些advice里面:
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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.cy"/> <bean id="logInteceptor" class="com.cy.aop.LogInterceptor"></bean>
<aop:config> <aop:aspect id="logAspect" ref="logInteceptor"> <aop:before method="before" pointcut="execution(public * com.cy.service..*.add(..))"/> </aop:aspect> </aop:config> </beans>
console同样的运行效果;
三、aop:advisor这个放在声明式事务管理中写;
总结: