Spring v3.0.2 Learning Note 13 - AOP Example
2010-10-25 11:15
246 查看
命名空间的支持
<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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<context:component-scan base-package="com.spring.test" />
<aop:aspectj-autoproxy />
// AOP中这2个Bean的定义不可少!不明为什么,我定义了类路径的自动扫描了
<bean id="personAopImpl" class="com.spring.test.aop.PersonAopImpl" />
<bean id="personAopProxy" class="com.spring.test.aop.PersonAopProxy" />
</beans>
Aspect 注解
<aop:aspectj-autoproxy />
原代码
接口:
package com.spring.test.aop;
public interface IPersonAop {
public void sayHello(String name);
}
实现类:
package com.spring.test.aop;
import org.springframework.stereotype.Service;
@Service
public class PersonAopImpl implements IPersonAop {
@Override
public void sayHello(String name) {
System.out.println("Hello," + name);
}
}
代理类:
package com.spring.test.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class PersonAopProxy {
@Before("execution (* com.spring.test.aop..*.*(..))")
public void before() {
System.out.println("before...");
}
@After("execution (* com.spring.test.aop..*.*(..))")
public void after() {
System.out.println("after...");
}
}
测试类:
package com.spring.test.junit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.aop.IPersonAop;
public class AopTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
IPersonAop aop = (IPersonAop)ctx.getBean("personAopImpl");
aop.sayHello("xxxxx");
}
}
<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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<context:component-scan base-package="com.spring.test" />
<aop:aspectj-autoproxy />
// AOP中这2个Bean的定义不可少!不明为什么,我定义了类路径的自动扫描了
<bean id="personAopImpl" class="com.spring.test.aop.PersonAopImpl" />
<bean id="personAopProxy" class="com.spring.test.aop.PersonAopProxy" />
</beans>
Aspect 注解
<aop:aspectj-autoproxy />
原代码
接口:
package com.spring.test.aop;
public interface IPersonAop {
public void sayHello(String name);
}
实现类:
package com.spring.test.aop;
import org.springframework.stereotype.Service;
@Service
public class PersonAopImpl implements IPersonAop {
@Override
public void sayHello(String name) {
System.out.println("Hello," + name);
}
}
代理类:
package com.spring.test.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class PersonAopProxy {
@Before("execution (* com.spring.test.aop..*.*(..))")
public void before() {
System.out.println("before...");
}
@After("execution (* com.spring.test.aop..*.*(..))")
public void after() {
System.out.println("after...");
}
}
测试类:
package com.spring.test.junit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.aop.IPersonAop;
public class AopTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
IPersonAop aop = (IPersonAop)ctx.getBean("personAopImpl");
aop.sayHello("xxxxx");
}
}
相关文章推荐
- Spring v3.0.2 Learning Note 10 - Annotation-based Transaction Management
- Spring v3.0.2 Learning Note 12 - Integrate with Hiberate
- Spring v3.0.2 Learning Note 15 - Use Velocity Email Template with Spring
- Spring v3.0.2 Learning Note 2 - A Sample Example
- Spring v3.0.2 Learning Note 3 - Initialisation of Beans
- Spring v3.0.2 Learning Note 6 - Inject Components
- Spring v3.0.2 Learning Note 7 - Constructor-based dependency injection
- Spring v3.0.2 Learning Note 8 - Annotation-based dependency injection
- CalTech machine learning, video 13 note(validation)
- The NOTE of learning ASP.NET [13] 关于.NET中的Constants,Enumerations,Structs
- Linear Regression_Note of Machine Learning_Andrew NG
- C#Note13:如何在C#中调用python
- [Paper note] Joint Learning of Single-image and Cross-image Representations for Person Re-id.
- Struts Learning Note1 --- Overview
- c++ learning note (1): std 命名规范 vector容器
- CalTech machine learning video 8 note(Bias-Variance Tradeoff)
- Stanford Machine Learning Note1 - Linear Regression
- Note for video Machine Learning and Data Mining——Theory of Generalization
- Learning note(4) for windows powershell Tips
- note of kafka learning (first part, before replication)