简体   繁体   English

将属性注入不是Spring管理的bean的对象中

[英]Inject properties into object which is not a spring managed bean

I have a spring application where in some cases I need to create an object at runtime, as opposed to a spring injected bean. 我有一个Spring应用程序,在某些情况下,我需要在运行时创建一个对象,而不是Spring注入的bean。 This object should be created with properties that come from the application context however. 但是,应使用来自应用程序上下文的属性来创建该对象。 If this object is not spring-injected, how can I still take advantage of IoC? 如果该对象不是弹簧注入的,我如何仍然可以利用IoC? Should I make these object properties static and inject them into a bean via non-static setters? 我应该将这些对象属性设为静态,然后通过非静态设置器将它们注入到bean中吗?

You can still inject properties for object which is not created by spring. 您仍然可以为不是由spring创建的对象注入属性。 To do that you should use @Configurable annotation. 为此,您应该使用@Configurable批注。 And you should enable LoadTimeWeaving or CompileTimeWeaving. 并且您应该启用LoadTimeWeaving或CompileTimeWeaving。 But I think that can be overkill for your situation. 但我认为这可能对您的情况有些大材小用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM