程序未报错,在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(); } }