spring框架学习之路(一)-入门基础(3)-IOC和AOP的综合应用
2017-11-09 16:54
901 查看
我们认识完动态代理之后,就可以正式使用spring里的AOP了,实现spring的AOP有两种方式:
1)基于xml文件
2)基于注解
1. 通过配置XML文件,实现AOP.
用到的接口及API:
前置通知: MethodBeforeAdvice
后置通知: AfterReturningAdvice
MethodInterceptor
用于处理事务: ThrowsAdvice
配置XML文件
如果在org.springframework.aop.framework.ProxyFactoryBean 的<bean>中未配置proxyInterfaces那么就会自动通过CGglib实现
2. 用注解的方式实现AOP
1)@component基本注解,标识了一个受Spring管理的组件
2)@Reponsitory 标识的是持久层的组件
3)@service标识的是服务层的组件
4)@Controller 控制层的组件
5)@aspect 表示切面
依赖关系的注解(组件的装配)
1)@AutoWired -----常用
2) @Resource -----常用
3) @Inject
用法示栗~
注意:切面类一定要添加@Component这样才会被XML扫描到
这样写需要在xml里面配置:
1) 注意 添加命名空间
1)基于xml文件
2)基于注解
1. 通过配置XML文件,实现AOP.
用到的接口及API:
前置通知: MethodBeforeAdvice
public void before(Method arg0, Object[] arg1, Object arg2)throws Throwable{}
后置通知: AfterReturningAdvice
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable
MethodInterceptor
public Object invoke(MethodInvocation methodInvocation) throws Throwable { Object result = methodInvocation.proceed(); return result; }
用于处理事务: ThrowsAdvice
public void afterThrowing([Method method, Object[] args, Object target, Exception ex)
配置XML文件
<bean id="userTarget" class="code.bean.UserA"></bean> //真实角色 <bean id="methodbeforeTAdvice" class="code.bean.BeforTest"></bean> <bean id="afterTest" class="code.bean.AfterTest"></bean> //后置通知 <bean id="user" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> //真实角色的接口s <value>code.bean.User</value> </property> <property name="target" ref="userTarget"></property>//代理目标 <property name="interceptorNames"> //嵌入通知 <list> <value>afterTest</value> <value>methodbeforeTAdvice</value> </list> </property> </bean>
如果在org.springframework.aop.framework.ProxyFactoryBean 的<bean>中未配置proxyInterfaces那么就会自动通过CGglib实现
2. 用注解的方式实现AOP
1)@component基本注解,标识了一个受Spring管理的组件
2)@Reponsitory 标识的是持久层的组件
3)@service标识的是服务层的组件
4)@Controller 控制层的组件
5)@aspect 表示切面
依赖关系的注解(组件的装配)
1)@AutoWired -----常用
2) @Resource -----常用
3) @Inject
用法示栗~
@Aspect @Order(2) @Component public class Myaspect { @Pointcut("execution(public * login(..)) && target(com.neu.controller.UserController)") public void pointCut(){} @Before("execution(public * login(..)) && target(com.neu.controller.UserController)") public void before(JoinPoint joinPoint){ System.out.println(joinPoint.getSignature().getName()); System.out.println("before"); } @After("pointCut()") public void after(JoinPoint joinPoint){ System.out.println(joinPoint.getSignature().getName()); System.out.println("after"); } @Around("pointCut()") public Object around(ProceedingJoinPoint point) throws Throwable{ System.out.println("before.."); Object object=point.proceed(); System.out.println("after.."); return object; } }
注意:切面类一定要添加@Component这样才会被XML扫描到
这样写需要在xml里面配置:
1) 注意 添加命名空间
<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:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">[/code]
2) 设置扫描路径
context:component-scan:自动扫描注解的配置文件
base-package:制定一个需要的包,如果多个包之间用,隔开
resource-pattern:更加精确的去确定扫描那个包下的哪个类<context:component-scan base-package="com.neu" [resource-pattern="" use-default-filters="true"]> [<context:exclude-filter type="annotation" expression=""/>] --按注释排除 [<context:include-filter type="assignable" expression=""/>]--按接口单一包含 </context:component-scan>
3)设置切面自动代理(在设置@Aspect和@Before等的前提下)<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
如果不设置@Aspect和@Before的话,也可以通过只配置xml的方式实现AOP<bean id="aspect" class="com.neu.main.Myaspect"></bean> <aop:config> <aop:pointcut expression="execution(public * login(..)) and target(com.neu.controller.UserController)" id="pointcut"/> <aop:aspect ref="aspect" order="1"> <aop:after method="before" pointcut-ref="pointcut"/> <aop:before method="after" pointcut-ref="pointcut"/> <aop:around method="around" pointcut-ref="pointcut"/> </aop:aspect> </aop:config>
5. Spring提供了两种类型的IOC容器实现
1)BeanFactory :IOC容器最基本的实现
2) ApplicationContext:Web项目常用的获取bean的方式,特性比较高级,ClassPathXmlApplicationContext这个是用的最多的一种实现方式
4. Spring的注入数值问题:
1)字面值String:value
2) 基本数据类型:字面值
3)引用的是引用类型 Ref这个属性
4)如果字面值包含了特殊的字符CDATA节进行包裹,<![CDATA[]]>
附录:
Spring AOP切入点的表达式
1)execution(修饰符? 返回值类型 声明类型? 方法名称(方法的参数) 异常类型? )
1 执行任意的public方法
execution(public * * (..))
2 执行任意的get开头的方法
execution(* get*(..))
3 执行Service包下的所有的类中的任何方法
execution(* com.neusoft.service.*(..))
4 执行至少有一个参数,并且第一个参数的类型为int类型的方法
execution( * * (java.lang.Integer,..))
5执行Service包下及其子包下的所有的方法
execution(* com.neusoft.service..(..))
6 执行可能会发生异常的所有的方法
execution(* * (..) throws Exception)
相关文章推荐
- spring框架学习之路(一)-入门基础(2)-动态代理和AOP(面向切面编程)
- 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
- Spring框架IOC与AOP简单应用浅解-适合初入SSH框架学习
- spring框架学习(八)—静态代理、JDK与CGLIB动态代理、AOP+IoC
- Spring AOP基础(Spring 3.x企业应用开发实战第六章)学习总结
- Python学习之路【第一篇】-Python简介和基础入门
- Spring学习笔记(二)Spring基础AOP、IOC
- 我的java/Spring框架入门学习过程(2):IOC的基本操作
- spring环境搭建&spring快速入门---【小白系列】0基础到熟练应用spring框架(一)
- Spring学习笔记(一) Spring基础IOC、AOP
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
- 框架学习前基础加强 泛型高级,注解,反射(泛型&注解)应用案例,IOC,Servlet3.0,动态代理,类加载器
- Python学习之路【第一篇】-Python简介和基础入门
- [Spring框架]Spring AOP基础入门总结一.
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.2 相关Java基础知识 类装载器 反射机制
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
- 第二天 - Scala学习之路(基础入门)
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.2 相关Java基础知识 类装载器 反射机制
- JavaEE框架——Spring入门基础(控制反转Ioc和切面技术Aop)
- JAVA基础学习之IP简述使用、反射、正则表达式操作、网络爬虫、可变参数、了解和入门注解的应用、使用Eclipse的Debug功能(7)