Spring-----
注解方法使用
AOP
先建立项目
,
在
spring
的
xml
文件中加入
spring AOP
的支持
.
主要是下面红色字部分
.
<?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></aop:aspectj-autoproxy>
<bean id="myInterceptor" class="com.liyu.service.MyInterceptor"></bean>
<bean id="personServiceBean" class="com.liyu.service.impl.PersonServiceBean"></bean>
</beans>
再定一个一接口
,
一个这个接口的实现
.
再就是写这个
AOP
类了
.
package com.liyu.service;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 切面
*
*/
@Aspect
public class MyInterceptor {
@Pointcut("execution (* com.liyu.service.impl.PersonServiceBean.*(..))")
private void anyMethod() {}//声明一个切入点
@Before("anyMethod() && args(name)")
public void doAccsCheck(String name) {
System.out.println("前置通知:"+name);
}
@AfterReturning(pointcut="anyMethod()",returning="result")
public void doAfterReturning(String result) {
System.out.println("后置通知:"+result);
}
@After("anyMethod()")
public void doAfter() {
System.out.println("最终通知:");
}
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e) {
System.out.println("例外通知:"+e);
}
@Around(("anyMethod()"))
//环绕通知
public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{
System.out.println("进入方法");
Object result=pjp.proceed();
System.out.println("退出方法");
return result;
}
}
测试类:
package com.liyu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.liyu.service.PersonService;
public class AOPtest {
public static void main(String[] args) {
ApplicationContext ctx= new ClassPathXmlApplicationContext("beans.xml");
//MyInterceptor mi=(MyInterceptor)ctx.getBean("myInterceptor");
PersonService ps=(PersonService)ctx.getBean("personServiceBean");
ps.save("sss");
}
}
运行结果
:
2010-6-15 8:27:27 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@147ee05: display name [org.springframework.context.support.ClassPathXmlApplicationContext@147ee05]; startup date [Tue Jun 15 08:27:27 CST 2010]; root of context hierarchy
2010-6-15 8:27:27 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-15 8:27:28 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@147ee05]: org.springframework.beans.factory.support.DefaultListableBeanFactory@273686
2010-6-15 8:27:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@273686: defining beans [org.springframework.aop.config.internalAutoProxyCreator,myInterceptor,personServiceBean]; root of factory hierarchy
前置通知:sss
进入方法
我是save()方法.
后置通知:null
最终通知:
退出方法
我在这里做练习时出错了
PersonService ps=(PersonService)ctx.getBean("personServiceBean");
这里一定要使用的是那个接口
.
原文地址:http://www.c119.cn/home.php?mod=space&uid=174&do=blog&id=348
分享到:
相关推荐
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
commons-logging-1.1.3,spring-test-4.0.0.RELEASE,spring-aop-4.0.0.RELEASE,spring-aspects-4.0.0.RELEASE
演示了spring对aop的支持,包括注解方式、基于xml方式。
spring-aop-4.2.4.RELEASE,spring注解包,代码里面特殊标记,使用注解可以完成功能,相当于语法糖操作
spring-aop-4.0.4.RELEASE.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aspects-4.1.2.RELEASE.jar ...
使用Spring的注解方式实现AOP的细节
spring-aop-4.0.0.RELEASE工具类,用于spring注解装配bean
SSM整合-用springaop-demo01实现了注解AOP,SSM-MybatisOneForOne-demo01实现了
草稿:SSM整合-用springaop-demo01实现了注解AOP,SSM-MybatisOneForOne-demo01实现
spring-aop注解用到的jar包,解压后直接导入即可使用。
Spring4--2.bean注解和AOP
Spring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOPSpring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOP
spring 注解 aop demo spring 注解 aop demo spring 注解 aop demo spring 注解 aop demo
Spring注解扫描需要的包。Spring 经过十多年的快速发展和更新,以其独特的创新带来了大量的拥趸者,在选择方面还带来了更多选择!
2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用
代码中有注解实例,spring框架实例,aop切片实例
SpringBoot下的Spring——DAY04——动态代理总结、AOP、自定义注解进行拦截、动态获取注解参数、通知方法 1.动态代理总结 1.1 JDK动态代理特点 1.2 CGlib动态代理 1.2.1 CGLib特点说明 1.3 动态代理的作用 2 Spring...
资源名称:拓薪教育-Spring内幕深入剖析和实战精讲资源目录...拓薪教育-spring3.2-AOP和其他功能介绍【】05.拓薪教育-spring3.2-环境搭建【】06.拓薪教育-s 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,AOP的原理就是java的动态代理机制