class="java"> <?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd "> <bean id="greetingService" class="com.jesse.spring.simple.service.impl.GreetingServiceImpl"> <property name="string" value="Hello, Jesse!" /> </bean> <bean id="point" class="com.jesse.spring.simple.service.Point"> </bean> <aop:config> <aop:aspect ref="point"> <!-- 创建切面 --> <aop:pointcut id="greetPointcut" expression="execution(* *.greet(..))"/> <!-- 创建切入点并绑定greet方法 --> <aop:before method="greetBefore" pointcut-ref="greetPointcut"/> <aop:after method="greetAfter" pointcut-ref="greetPointcut"/> </aop:aspect> </aop:config> </beans>
package com.jesse.spring.simple.service.impl; import org.apache.log4j.Logger; import com.jesse.spring.simple.service.GreetingService; public class GreetingServiceImpl implements GreetingService { private static final Logger logger = Logger .getLogger(GreetingServiceImpl.class); private String string; @Override public void greet() { logger.info(this); logger.info(string); } public String getString() { return string; } public void setString(String string) { this.string = string; } }
package com.jesse.spring.simple.service; import org.apache.log4j.Logger; public class Point { private static final Logger logger = Logger.getLogger(Point.class); public void greetBefore() { logger.info("this is before method....."); } public void greetAfter() { logger.info("this is after method....."); } }
[2013-10-26 03:32:05] INFO (Point.java:10) -this is before method..... [2013-10-26 03:32:05] INFO (GreetingServiceImpl.java:15) -com.jesse.spring.simple.service.impl.GreetingServiceImpl@170eb6d [2013-10-26 03:32:05] INFO (GreetingServiceImpl.java:16) -Hello, Jesse! [2013-10-26 03:32:05] INFO (Point.java:14) -this is after method..... [2013-10-26 03:32:05] INFO (Point.java:10) -this is before method..... [2013-10-26 03:32:05] INFO (GreetingServiceImpl.java:15) -com.jesse.spring.simple.service.impl.GreetingServiceImpl@170eb6d [2013-10-26 03:32:05] INFO (GreetingServiceImpl.java:16) -Hello, Jesse! [2013-10-26 03:32:05] INFO (Point.java:14) -this is after method.....