package test;
import org.springframework.beans.factory.annotation.Configurable;
@Configurable("testBean")
public class TestBean {
private TestService testService;
public TestService getTestService() {
return testService;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
}
package test;
public class TestService {
public String sayHello() {
return "Hello";
}
}
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<aop:spring-configured />
<bean id="testService" class="test.TestService"
depends-on="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" />
<bean id="testBean" class="test.TestBean" scope="prototype">
<property name="testService" ref="testService" />
</bean>
</beans>
在META-INF里面加入aop.xml
<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<aspects>
<include
within="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" />
</aspects>
<weaver
<include within="test.TestBean" />
</weaver>
</aspectj>
用的是aspectj的加载期植入代码,jvm的启动参数加上-javaagent:aspectjweaver.jar
现在来测试
package test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String... strings) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext-test.xml");
TestBean bean = new TestBean();
System.out.println(bean.getTestService());
}
}