Spring动态代理,aop 注解实现aop
2015-10-23 20:50
766 查看
<?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">
<aop:aspectj-autoproxy/>
</beans>
上面红色的事关于spring动态代理的aopxml配置选项,必须要填写
配置完毕后,导入jar包
就可以使用注解实现aop
代码结构
代码样例
dao层
package com.leige.dao;
import org.aspectj.lang.annotation.Aspect;
public interface PersonDao {
public void add();
}
dao层实现
package com.leige.daoimpl;
import org.springframework.stereotype.Repository;
import com.leige.dao.PersonDao;
@Repository("personDao")
public class PersonDaoImpl1 implements PersonDao{
public void add() {
// TODO Auto-generated method stub
System.out.println("leige我是add方法");
}
}
service层
package com.leige.service;
import javax.annotation.Resource;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
import com.leige.dao.PersonDao;
@Service("personService")
public class PersonService {
private PersonDao dao=null;
public PersonDao getDao() {
return dao;
}
@Resource(name="personDao")
public void setDao(PersonDao dao) {
this.dao = dao;
}
}
动态代理拦截
package com.leige.interceptor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Repository;
@Aspect
@Repository("myInterceptor")
public class MyInterceptor {
@Pointcut("execution (* com.leige.daoimpl.PersonDaoImpl1.*(..)) ")
public void fun(){}//定义切入点
@Before("fun()")
public void before(){
System.out.println("磊哥无敌");
}
@After("fun()")
public void after(){
System.out.println("leigezuishuai");
}
}
测试类
package com.leige.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.leige.service.PersonService;
import sun.awt.AppContext;
public class PersonTest{
@Test
public void fun(){
ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");//读取配置文件
PersonService per=(PersonService) app.getBean("personService");
per.getDao().add();
}
}
测试结果
所需jar包
<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">
<aop:aspectj-autoproxy/>
</beans>
上面红色的事关于spring动态代理的aopxml配置选项,必须要填写
配置完毕后,导入jar包
就可以使用注解实现aop
代码结构
代码样例
dao层
package com.leige.dao;
import org.aspectj.lang.annotation.Aspect;
public interface PersonDao {
public void add();
}
dao层实现
package com.leige.daoimpl;
import org.springframework.stereotype.Repository;
import com.leige.dao.PersonDao;
@Repository("personDao")
public class PersonDaoImpl1 implements PersonDao{
public void add() {
// TODO Auto-generated method stub
System.out.println("leige我是add方法");
}
}
service层
package com.leige.service;
import javax.annotation.Resource;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
import com.leige.dao.PersonDao;
@Service("personService")
public class PersonService {
private PersonDao dao=null;
public PersonDao getDao() {
return dao;
}
@Resource(name="personDao")
public void setDao(PersonDao dao) {
this.dao = dao;
}
}
动态代理拦截
package com.leige.interceptor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Repository;
@Aspect
@Repository("myInterceptor")
public class MyInterceptor {
@Pointcut("execution (* com.leige.daoimpl.PersonDaoImpl1.*(..)) ")
public void fun(){}//定义切入点
@Before("fun()")
public void before(){
System.out.println("磊哥无敌");
}
@After("fun()")
public void after(){
System.out.println("leigezuishuai");
}
}
测试类
package com.leige.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.leige.service.PersonService;
import sun.awt.AppContext;
public class PersonTest{
@Test
public void fun(){
ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");//读取配置文件
PersonService per=(PersonService) app.getBean("personService");
per.getDao().add();
}
}
测试结果
所需jar包
相关文章推荐
- springmvc 请求响应时报异常
- JAVA除去制定字符的方法
- 解决方法参数为arg0,arg1的问题
- java中的内存和数组中注意的问题
- eclipse
- java开发常用工具类
- JDK7目录监视服务
- Java设计模式3:工厂方法模式
- URL与URI到底哪里不一样?
- HiveServer2 的jdbc方式创建udf的修改(add jar 最好不要使用),否则会造成异常: java.sql.SQLException: Error while processing statement: null
- java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
- Eclipse debug ‘Source not found’解决方法
- MyEclipse软件中快捷键
- java的笔试选择题
- 如何使用eclipse自动调整代码格式
- myeclipse中java文件中文乱码怎么解决
- Java-ServletContextAttributeListener
- Java-ServletContextAttributeListener
- Java-ServletContextListener
- Java-ServletContextListener