[英]Assign data member of a domain class using Spring
Is it possible? 可能吗?
Here is a more detailed case: 这是更详细的情况:
class A {
private service x;
//other members
//some more methods.
}
bean definition is available for service
but I do not want class A
to be a spring bean and neither x to be static. bean定义可用于service
但我不希望class A
是spring bean,也不希望x是静态的。
Is this achievable. 这是可以实现的。
EDIT: 编辑:
My spring configuration: 我的弹簧配置:
<bean id="Service" class="com.xx.yy.Service" />
<--!I do not register class A as a bean. Hence cannot use @autowired directly.-->
I think you should create the factory for instances of class A which should take care of setting x. 我认为您应该为类A的实例创建工厂,该工厂应注意设置x。
@Service
class FactoryA {
@Autowired service x;
public A create() {
return new A(x);
}
}
It can only be achieved by manually getting the bean from the context. 这只能通过从上下文手动获取Bean来实现。
Service service = context.getBean(Service.class);
A a = new A(service);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.