當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring 注解AOP 入门
生活随笔
收集整理的這篇文章主要介紹了
Spring 注解AOP 入门
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?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/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><aop:aspectj-autoproxy /><bean id="myInterceptor" class="cn.itcast.service.MyInterceptor" /><bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" /> </beans>
業(yè)務(wù)Bean,接口就不貼了
package cn.itcast.service.impl;import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
public String getPersonName(Integer id) {
System.out.println("我是getPersonName()方法");
return "XXX";
}
public void save(String name) {
System.out.println("我是save()方法");
}
public void update(String name, Integer id) {
System.out.println("我是update()方法");
}
}
實(shí)現(xiàn)AOP的類
package cn.itcast.service;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyInterceptor {
@Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
private void anyMethod() {}
@Before("anyMethod()")
public void doAccessCheck(String userName) {
System.out.println("前置通知");
}
}
測試方法
package junit.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.service.PersonService;
public class SpringAOPTest {
@Test
public void interceptorTest(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService)ctx.getBean("personService");
personService.save("xxx");
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/live365wang/archive/2011/08/06/2129415.html
總結(jié)
以上是生活随笔為你收集整理的Spring 注解AOP 入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CF449 C. Jzzhu and A
- 下一篇: openssl ca文档翻译