方面没有被调用

时间:2016-05-04 13:42:51

标签: spring aop aspectj

我试图在方法执行后调用aspect。但是甚至没有去代码内部。

这是我的配置类:

Cow::Owned

这是我的方面:

use std::borrow::Cow;
use std::io::{BufReader, BufRead};
use std::iter::IntoIterator;
use std::fs::File;

fn parse<'a, I>(lines: I)
    where I: IntoIterator,
          I::Item: Into<Cow<'a, str>>
{
    for line in lines {
        let line: Cow<'a, str> = line.into();
        let line: String = line.into_owned();
        // or
        let line = line.into().into_owned()
        println!("{}", line);
    }
}

fn main() {
    let fin = BufReader::new(File::open("/etc/hosts").expect("cannot open file"));
    parse(fin.lines().map(|r| r.expect("file read failed")));

    let content: &'static str = "some\nlong\ntext";
    parse(content.lines());
}

这是我想用我的方面提供的功能签名:

@Configuration
@PropertySource("classpath:auditorium.properties")
@ComponentScan(basePackages = { "com.epam.spring.hometask.*" })
@EnableAspectJAutoProxy
public class AppConfig {
      ///Bean definitions
}

并测试:

@Component
@Aspect
public class CounterAspect {

    @AfterReturning(pointcut = "execution(* com.epam.spring.hometask.service.EventService.getByName(..))", returning = "event")
    public void calculateAccessEventByNameCounter(Event event) {
        System.out.println("Aspect running");       
    }

getByName调用

后没有打印任何内容

1 个答案:

答案 0 :(得分:0)

Event event = new Event();
event.getByName();

事件应该是应用方面的spring-managed bean,即你应该从ApplicationContext获取Event类型的对象,如下所示:

ClassPathXmlApplicationContext context = 
    new ClassPathXmlApplicationContext("classpath:META-INF/Spring.xml");
Event event = context.getBean(Event.class);

或使用@Resource或@Autowired注释注入它们。

然后,对事件对象的getByName()调用将通过Spring框架创建的AOP代理,并且执行与方法定义匹配的AOP建议中的代码。

使用new运算符创建对象时获取spring-managed bean的另一种方法是使用@Configurable批注。

相关问题