【问题标题】:Aspect not working witha spring bean方面不适用于 spring bean
【发布时间】:2015-03-17 03:21:07
【问题描述】:

我正在尝试创建一个简单的方面。 这是我的简单春豆

public class SimpleService {
    public void sayHello(){
        System.out.println("hi");
    }

}

这是我的方面类

@Aspect
public class SimpleAspect {
    @Before("execution(void sayHello())")
    public void entering(){
        System.out.println("entering..");
    }
}

这是我的配置文件

<?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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
         <aop:aspectj-autoproxy/>
<bean id="service" class="com.schatt.service.SimpleService"></bean>

我的理解是,当我尝试调用 SimpleService.sayHello() 时,会调用之前的方面,之后会调用 sayHello()。但是方面没有被触发。不明白我在这里缺少什么.

【问题讨论】:

    标签: spring aspectj spring-aop


    【解决方案1】:

    方面需要由 Spring 创建(以便应用代理)。

    <bean id="simpleAspect" class="package-name.SimpleAspect"></bean>
    

    【讨论】:

      【解决方案2】:

      如果你的类没有实现任何接口,你将不得不使用&lt;aop:aspectj-autoproxy proxy-target-class="true"/&gt;

      【讨论】:

        【解决方案3】:

        除了 manishfateddy 所说的之外,还请注意 SimpleService 必须是 Spring @Component 才能使其与 Spring 一起使用AOP。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-22
          • 1970-01-01
          • 2021-11-06
          • 2016-06-30
          相关资源
          最近更新 更多