AOP切点切面内容
Posted 丶不学无术丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AOP切点切面内容相关的知识,希望对你有一定的参考价值。
一、实现接口MethodBeforeAdvice该拦截器会在调用方法前执行
实现接口 AfterReturningAdvice该拦截器会在调用方法后执行
实现接口 MethodInterceptor该拦截器会在调用方法前后都执行,实现环绕结果。
- package com.ly.model;
- import java.lang.reflect.Method;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- import org.springframework.aop.AfterReturningAdvice;
- import org.springframework.aop.MethodBeforeAdvice;
- public class Advice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{
- @Override
- public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
- saveBeforeMessage();
- }
- @Override
- public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
- saveAfterMessage();
- }
- public void saveBeforeMessage(){
- System.out.println("调用BeforeAdvice成功");
- }
- public void saveAfterMessage(){
- System.out.println("调用AfterAdvice成功");
- }
- @Override
- public Object invoke(MethodInvocation arg0) throws Throwable {
- System.out.println("调用RoundService之前成功");
- Object result=arg0.proceed();
- System.out.println("调用RoundService之后成功");
- return result;
- }
- }
以下为AOPservice的实现
- package com.ly.service.impl;
- import com.ly.service.AOPService;
- public class AOPServiceImpl implements AOPService{
- @Override
- public void print(String message) {
- System.out.println(message);
- }
- @Override
- public void save() {
- System.out.println("保存信息成功");
- }
- }
以下为配置文件信息
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <!--目标对象 -->
- <bean id="AOPservice" class="com.ly.service.impl.AOPServiceImpl">
- </bean>
- <!-- advice通知 -->
- <bean id="adviceMessage" class="com.ly.model.Advice"></bean>
- <!-- 切入点adviser -->
- <bean id="adviser" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
- <property name="advice" ref="adviceMessage"></property>
- <!-- pattern的值使用正则表达式精确指定切入点 ,将print方法设为切入点 -->
- <property name="pattern"
- value="com\.ly\.service\.impl\.AOPServiceImpl\.print"></property>
- </bean>
- <!-- 代理对象 返回实例是目标对象 target属性指定的AOPservice对象-->
- <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
- <!-- 设置目标(target)bean为 AOPservice,
- 多个bean可以设置list集合如
- <property name="interceptorNames">
- <list>
- <value>advisor</value>
- <value>advisor1</value>
- </list>
- </property>-->
- <property name="target">
- <ref bean="AOPservice" />
- </property>
- <!--源码内固定的属性private String[] interceptorNames; -->
- <property name="interceptorNames">
- <value>adviser</value>
- </property>
- </bean>
- </beans>
以下为测试类
- package com.ly.control;
- import java.io.IOException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.FileSystemResource;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.UrlResource;
- import com.ly.service.AOPService;
- public class SpringTest {
- public static void main(String[] args) throws IOException {
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"springBeans.xml"});
- AOPService aopservice= (AOPService) applicationContext.getBean("proxyService");
- //由于在springBeans.xml中只配置了 value="com\.ly\.service\.impl\.AOPServiceImpl\.print"适配print方法,没有适配save方法,故只有调用print方法时才会执行advice通知
- aopservice.print("调用print成功==========》");
- aopservice.save();
- }
- }
二、在web应用中调用时需要用WebApplicationContext这个来得到Spring配置中的bean类
- public class UserAction extends ActionSupport {
- private static final Logger log = Logger.getLogger(UserAction.class);
- private UserService userService;
- public UserService getUserService(){
- if(userService == null){
- WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getRequest().getServletContext());
- userService = (UserService) applicationContext.getBean("proxyService");
- // userService = new UserServiceImpl();
- }
- return userService;
- }
以上是关于AOP切点切面内容的主要内容,如果未能解决你的问题,请参考以下文章
Spring Aop中四个重要概念,切点,切面,连接点,通知