首页 > Spring AOP 切面没有执行

Spring AOP 切面没有执行

我想试试aop怎么用,但是写好之后没有调用切面,而IDEA却找到了我的切面和切入点。。

配置类:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class CarConfig {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                        new AnnotationConfigApplicationContext(CarConfig.class);
        System.out.println("-------------Spring context loaded-------------");
        Ferrari ferrari = context.getBean(Ferrari.class);
        Driver driver = new Driver(new License("s3j3"));
        int time = driver.drive(ferrari, 1000);
        System.out.println("spent " + time + " hours");
        System.out.println("-------------Spring context is going to close-----------");
        context.close();
    }
}

Driver:

public class Driver {

    private License license;

    public Driver(License license) {
        this.license = license;
    }

    public int drive(Car car, int distance) {
        if (license == null) return -1;
        if (distance <= 0) return 0;
        if (car == null || car.getSpeed() <= 0) return -1;
        return distance / car.getSpeed() + 1;
    }
}

Aspect:

@Aspect
@Component
public class Monitor {

    @Pointcut(value = "execution(int cn.gaoyuexiang.spring.demo.test.aop.driver.Driver" +
                    ".drive(cn.gaoyuexiang.spring.demo.test.aop.car.Car, int)) " +
                    "&& args(car, distance)")
    public void drive(Car car, int distance) {}

    @Before("drive(car, distance)")
    public void aboard(Car car, int distance) {
        String carName = car.getClass().getName();
        System.out.println("the driver will drive a " + carName +
                        " to run " + distance + " kilometers");
    }
}

目录结构:

.
├── aop
│   └── Monitor.java
├── car
│   ├── Car.java
│   └── Ferrari.java
├── CarConfig.java
└── driver
    ├── Driver.java
    └── license
        └── License.java

Spring输出:

15:53:46.560 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
15:53:46.572 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
15:53:46.573 [main] DEBUG org.springframework.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
15:53:46.741 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7fbe847c: startup date [Sun Aug 21 15:53:46 CST 2016]; root of context hierarchy
15:53:46.743 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@7fbe847c: org.springframework.beans.factory.support.DefaultListableBeanFactory@204f30ec: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,carConfig]; root of factory hierarchy
15:53:46.766 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
15:53:46.767 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
15:53:46.806 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
15:53:46.810 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
15:53:46.868 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
15:53:46.869 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
15:53:46.870 [main] DEBUG org.springframework.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
15:53:46.887 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved classpath location [cn/gaoyuexiang/spring/demo/test/aop/] to resources [URL [file:/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/]]
15:53:46.889 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Looking for matching resources in directory tree [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop]
15:53:46.890 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Searching directory [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop] for files matching pattern [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/**/*.class]
15:53:46.896 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Searching directory [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/aop] for files matching pattern [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/**/*.class]
15:53:46.898 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Searching directory [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/car] for files matching pattern [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/**/*.class]
15:53:46.900 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Searching directory [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/driver] for files matching pattern [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/**/*.class]
15:53:46.901 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Searching directory [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/driver/license] for files matching pattern [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/**/*.class]
15:53:46.906 [main] DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:cn/gaoyuexiang/spring/demo/test/aop/**/*.class] to resources [file [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/CarConfig.class], file [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/aop/Monitor.class], file [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/car/Car.class], file [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/car/Ferrari.class], file [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/driver/Driver.class], file [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/driver/license/License.class]]
15:53:46.947 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/aop/Monitor.class]
15:53:46.948 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [/home/melo/IdeaProjects/spring.demo.test/build/classes/main/cn/gaoyuexiang/spring/demo/test/aop/car/Ferrari.class]
15:53:47.221 [main] DEBUG org.springframework.context.annotation.ConfigurationClassEnhancer - Successfully enhanced cn.gaoyuexiang.spring.demo.test.aop.CarConfig; enhanced class name is: cn.gaoyuexiang.spring.demo.test.aop.CarConfig$$EnhancerBySpringCGLIB$$8c55e143
15:53:47.222 [main] DEBUG org.springframework.context.annotation.ConfigurationClassPostProcessor - Replacing bean definition 'carConfig' existing class 'cn.gaoyuexiang.spring.demo.test.aop.CarConfig' with enhanced class 'cn.gaoyuexiang.spring.demo.test.aop.CarConfig$$EnhancerBySpringCGLIB$$8c55e143'
15:53:47.227 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
15:53:47.227 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
15:53:47.229 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
15:53:47.229 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
15:53:47.230 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
15:53:47.230 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
15:53:47.231 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
15:53:47.231 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
15:53:47.231 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
15:53:47.231 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
15:53:47.236 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
15:53:47.237 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
15:53:47.237 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
15:53:47.237 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
15:53:47.237 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor' to allow for resolving potential circular references
15:53:47.238 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
15:53:47.238 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
15:53:47.238 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
15:53:47.238 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor' to allow for resolving potential circular references
15:53:47.239 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
15:53:47.243 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@4450d156]
15:53:47.246 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@1dd92fe2]
15:53:47.248 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@204f30ec: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,carConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,monitor,ferrari]; root of factory hierarchy
15:53:47.249 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
15:53:47.249 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
15:53:47.249 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
15:53:47.249 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
15:53:47.249 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
15:53:47.250 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
15:53:47.261 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular references
15:53:47.297 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
15:53:47.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
15:53:47.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
15:53:47.299 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular references
15:53:47.306 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
15:53:47.306 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'carConfig'
15:53:47.306 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'carConfig'
15:53:47.309 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'carConfig' to allow for resolving potential circular references
15:53:47.315 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'carConfig'
15:53:47.315 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
15:53:47.315 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
15:53:47.316 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'monitor'
15:53:47.316 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'monitor'
15:53:47.324 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'monitor' to allow for resolving potential circular references
15:53:47.327 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'monitor'
15:53:47.328 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'ferrari'
15:53:47.328 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'ferrari'
15:53:47.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'ferrari' to allow for resolving potential circular references
15:53:47.333 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'ferrari'
15:53:47.334 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
15:53:47.398 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@436a4e4b]
15:53:47.399 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
15:53:47.402 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
-------------Spring context loaded-------------
15:53:47.406 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'ferrari'
spent 3 hours
-------------Spring context is going to close-----------
15:53:47.408 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7fbe847c: startup date [Sun Aug 21 15:53:46 CST 2016]; root of context hierarchy
15:53:47.409 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
15:53:47.409 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@204f30ec: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,carConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,monitor,ferrari]; root of factory hierarchy

Process finished with exit code 0

Spring的输出发现Monitor已经被扫描到了,但是切面却没有执行,不知道是为什么


你的Driver类需要声明为一个Spring Bean。而且Driver类是你自己new的,所以Spring没法知道它是什么时候被实例化的。

【热门文章】
【热门文章】