首页 新闻 会员 周边 捐助

Spring aop没起作用,也没报错

0
[已关闭问题] 关闭于 2017-06-15 16:04

程序未报错,在Test.test()没有输出切面方法????

切面方法:

@Aspect
public class AspectMethod {
    public  AspectMethod() {
        System.out.println("-------------");    
    }
    @Before("execution(public void com.aspect.Test.test())")
    public void printDate(){
        System.out.println(new Date(System.currentTimeMillis()));
    }
}

目标类:

package demo.aspect;

import org.springframework.stereotype.Component;

@Component
public class Test {
    public Test(){
        System.out.println("test");
    }
    public void test(){
        System.out.println("target Object is running");
    }
}

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:context="http://www.springframework.org/schema/context"
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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
      <context:annotation-config />
    <context:component-scan base-package="demo.aspect"/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
   <aop:aspectj-autoproxy expose-proxy="true"></aop:aspectj-autoproxy>
</beans>

Main方法:

package demo.aspect;

import java.lang.annotation.Annotation;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
public class Main {
    public static void main(String[] args) {
        ClassPathResource res=new ClassPathResource("beans.xml");          
        /*创建一个BeanFactory*/
         DefaultListableBeanFactory factory=new DefaultListableBeanFactory();
        /*创建一个载入BeanDefiniton的读取器*/
        XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);
        /**从定义好的资源位置读入配置信息**/
        reader.loadBeanDefinitions(res);
        Test test=factory.getBean(Test.class);
        test.test();
    }
}
不被女生喜欢好多年的主页 不被女生喜欢好多年 | 初学一级 | 园豆:39
提问于:2017-06-15 15:25
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册