Spring Advisor

SpringAdvisor 顾问:在通知的基础之上,在细入我们的切面AOP

通知和顾问都是切面的实现方式

通知是顾问的一个属性

顾问会通过我们的设置,将不同的通知,在不同的时间点把切面织入不同的切入点。

PointCutAdvisor接口!

比较常用的两个实现类

1 根据切入点(主业务方法)名称  织入切面NameMatchMethodPointCutAdvisor

2 根据自定义的正则 表达式织入切面 RegexpMethodPointoutAdvisor                           

接口

public interface UserService {
    public void addUser();
    public  String eat();
}

实现类

public class UserServiceImpl implements UserService {
    public void addUser() {
        System.out.println("添加用户");
    }

    public String eat() {
        System.out.println("eat  a little apple");
        return "apple";
    }
}

前置增强

public class BeforeAdvice implements MethodBeforeAdvice {
    /**
     *
     * @param method 目标方法
     * @param objects  目标方法的参数列表
     * @param o  目标对象
     * @throws Throwable
     */
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("前置增强---------------");
    }
}

后置增强

public class AfterAdvice implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("后置增强");
    }
}

ApplicationContext.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--目标对象-->
    <bean id ="userservice" class="cn.kitty.service.UserServiceImpl"></bean>
    <!--配置前置通知-->
    <bean id="beforeAdvice" class="cn.kitty.service.BeforeAdvice"></bean>
    <!--配置后置通知-->
    <!--<bean id="afterAdvice" class="cn.kitty.service.AfterAdvice"></bean>-->
     <!--配置顾问   根据名称-->
    <!--<bean id="Myadvisors" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="mappedNames" value="addUser"></property>
配置切入点
单个方法使用mappedName

多个方法使用mappedNames
 
</bean>-->  <!--配置顾问 自定义的配置根据正则表达式织入切面-->  <bean id="Myadvisors" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="pattern" value=".*a.*"></property> </bean>

配置切入点
单个表达式使用pattern
多个表达式使用patterns
<!-- 配置代理工厂bean 生成代理类 把通知织入到目标对象--> <bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--注册目标对象--> <property name="target" ref="userservice"></property> <!--注册顾问 --> <property name= "interceptorNames" value="Myadvisors"></property>
 
  
</bean> </beans>

test 测试类

public class Test1011 {
    @Test
    public void aadvisor(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService service= (UserService) context.getBean("userProxy");
        service.addUser();
    }
}

 

转载于:https://www.cnblogs.com/cuixiaomeng/p/7679030.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Advisor is a platform that provides advice and guidance to individuals and organizations on a range of topics. It uses the latest technology, including machine learning and natural language processing, to deliver personalized recommendations and insights. Some of the areas that Spring Advisor can help with include: - Career development: providing guidance on resumes, interviewing, and job searching - Financial planning: helping with budgeting, saving, and investing - Health and wellness: offering tips and recommendations on physical and mental health - Education: assisting with college planning and decision making Spring Advisor is designed to be accessible and user-friendly, allowing users to quickly get the information they need. It can be used as a standalone tool or integrated into other systems, making it a versatile solution for a wide range of users. ### 回答2: SpringAdvisor是Spring AOP框架中的一个组件,用于实现面向切面编程。它是Spring框架中的一个标准接口,用于定义切面规则。 Advisor在Spring AOP中起到了连接切点和切面的桥梁作用。在Spring AOP中,切点表示需要被拦截的方法,切面表示在切点执行之前、之后或者出现异常时需要执行的逻辑。Advisor定义了切点和切面之间的关系,并为切面的实现提供了方法拦截的能力。 Spring框架提供了多种类型的Advisor来满足不同的AOP需求,包括常用的切面类型如MethodBeforeAdvice、AfterReturningAdvice、ThrowsAdvice等。通过实现对应的Advisor接口,开发人员可以将具体的切面逻辑封装在Advisor中,使其可以被应用于不同的切点上。 使用Advisor时,我们需要将其配置到Spring的配置文件中。配置Advisor时,可以通过正则表达式定义切点的匹配规则来选择需要拦截的方法。同时,还可以配置多个Advisor来实现复杂的切面逻辑。 SpringAdvisor机制实现了对AOP的支持,使开发人员能够更加方便地实现横切关注点的功能。通过配置和使用Advisor,我们可以将通用的横切逻辑从具体的业务逻辑中抽离出来,提高了代码的可重用性和可维护性。同时,SpringAdvisor机制还具有灵活性和可扩展性,使得我们可以根据具体的需求来定制切面逻辑。 总的来说,SpringAdvisor是Spring AOP中的一个重要组件,用于定义切点和切面之间的关系。通过配置和使用Advisor,我们可以将切面逻辑应用到具体的切点上,实现对方法的拦截和处理。它是实现面向切面编程的关键之一,为我们提供了更加灵活和可维护的程序设计方式。 ### 回答3: Spring advisor是Spring框架中的一个重要组件,用于提供面向切面编程(AOP)的支持。它可以将通用的横切关注点(例如事务管理、日志记录、安全性等)从业务逻辑中分离出来,使得代码更加清晰、可维护。 在Spring中,advisor是将切面逻辑与目标对象进行关联的中介。它通过在Spring容器中定义一个advisor bean,并在配置文件中指定需要被切入的目标对象,从而实现切面的应用。Advisor可以包含一个或多个切入点和通知,用于指定在何处和何时应用切面逻辑。 Spring advisor的两个核心概念是切入点和通知。切入点定义了在目标对象的哪些方法上应用切面逻辑,可以使用表达式或注解进行指定。通知定义了在切入点位置执行的具体操作,例如在方法执行前后进行日志记录、事务管理等。Spring框架提供了多种类型的通知,包括前置通知、后置通知、返回通知和异常通知,开发者可以根据具体需求选择适合的通知类型。 Spring advisor通过将切面的逻辑与目标对象解耦,提供了更加灵活和可扩展的开发方式。开发者可以根据业务需求自由配置advisor和切入点,并且可以随时更改或移除切面逻辑,而不会对目标对象的代码产生影响。这种机制大大减少了代码的冗余和重复,同时提高了代码的可重用性和可维护性。 总之,Spring advisor是Spring框架中的一个重要组件,用于实现面向切面编程。它通过切入点和通知的概念,将切面逻辑和目标对象解耦,使得代码更加清晰、可维护。它的应用可以提高代码的复用性和可扩展性,使得项目的开发更加高效和灵活。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值