Spring容器外注入依赖package testimport org.springframework.beans.factory.annotation.Configurable@
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";}}
<?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" />
<bean id="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());
}
}
1 楼 Norther 2006-11-14 楼主,你好。。我没有试成功,我在spring的reference上看到,并不用在JVM上加参数和创建AOP。XML,只是用了Configurable的Annotation,但是你的方法我也没试成功,应该被注入的对象引用是null,能不能再详细的解释下2.0 里的aspectj的容器外注射的原理,谢谢
2 楼 quaff 2006-11-14 Norther 写道楼主,你好。。我没有试成功,我在spring的reference上看到,并不用在JVM上加参数和创建AOP。XML,只是用了Configurable的Annotation,但是你的方法我也没试成功,应该被注入的对象引用是null,能不能再详细的解释下2.0 里的aspectj的容器外注射的原理,谢谢
这个是在ClassLoader级别植入的,不是靠动态代理,需要加jvm启动参数,你仔细看看reference 3 楼 Norther 2006-11-15 谢谢你,我搞定了,还挺麻烦。。呵呵不过性能差距太大
我做了个测试
构造5万个对象 用ASPECTJ植入和不植入的性能差距300倍,
50万个对象差距1000倍,汗。。。。 4 楼 partech 2006-11-15 JRockit对AOP LTW做了优化,会快很多。
JVM 用-Xmanagement:class=org.aspectj.weaver.loadtime.JRockitAgent参数。
5 楼 Norther 2006-11-20 我用hibernate spring webwork 给DOMIAN MODEL注入总是发生莫名其妙的问题,很不稳定,例如这种错误
Caused by: org.springframework.beans.factory.BeanCreationException: Error creat
ng bean with name 'com.xxxx.domain.user.UserInfo$$EnhancerByCGLIB$$
3a79798': Error setting property values; nested exception is org.springframewor
.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException:
Property 'sessionFactory' threw exception; nested exception is org.hibernate.La
yInitializationException: unexpected case hit, method=createHibernateTemplate
请问楼主 你在生产环境中使用过该方法吗?可靠吗?