Spring容器外注入依赖

阅读更多
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";
	}
}



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">

depends-on="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" />





在META-INF里面加入aop.xml
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">


within="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" />





用的是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());
}

}

你可能感兴趣的:(Spring,AOP,JVM,Hibernate,Bean)