简体   繁体   English

如何将属性注入未实例化的对象?

[英]How do I inject a property into an object I don't instantiate?

Pretty simple scenario: I'm using Spring and Spring Security, and I'm trying to inject a property into the AuthenticationProvider used by my AuthenticationManager. 非常简单的场景:我正在使用Spring和Spring Security,并且试图将一个属性注入到我的AuthenticationManager使用的AuthenticationProvider中。 I just had the thought that I might be able to wire it up in the applicationContext, but not sure if that would work. 我只是想到我可以在applicationContext中将其连接起来,但是不确定是否可行。

applicationContext.xml: applicationContext.xml:

<http use-expressions="true">
    <intercept-url pattern="/" access="permitAll" />
    <intercept-url pattern="/**" access="hasRole('USER')"/>
    <form-login login-page="/index" />
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="twitterAuthenticationProvider" />
</authentication-manager>

<beans:bean id="twitterAuthenticationProvider" class="com.bottlingday.model.auth.TwitterAuthenticationProvider" />

TwitterAuthenticationProvider: TwitterAuthenticationProvider:

public class TwitterAuthenticationProvider implements AuthenticationProvider
{
@Autowired
private AppEngineUserStore userStore;

static Log log = LogFactory.getLog(TwitterAuthenticationProvider.class.getName());

public void Test()
{
            //userStore is always null here
    log.info("test");
}
}

In my controller this fails because userStore is a null ref in TwitterAuthenticationProvider: 在我的控制器中,此操作失败,因为userStore是TwitterAuthenticationProvider中的空引用:

Authentication authentication = this.authenticationManager.authenticate(authToken);

Here's the exact error: 这是确切的错误:

java.lang.NullPointerException
at com.bottlingday.model.auth.TwitterAuthenticationProvider.authenticate(TwitterAuthenticationProvider.java:33)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
at com.bottlingday.auth.TwitterAuthController.OauthCallback(TwitterAuthController.java:129)

Edit: I am using autowiring, and it works fine for other things. 编辑:我正在使用自动装配,它在其他方面也可以正常工作。 If I autowire a AppEngineUserStore in a MVC controller, it will be there every time. 如果我在MVC控制器中自动装配AppEngineUserStore,则每次都会在那儿。

 I just had the thought that I might be able to wire it up in the applicationContext, but not sure if that would work. 

This would work. 这会起作用。 You can specify the wiring of your properties in your application context. 您可以在应用程序上下文中指定属性的连接。 However, this is not the same as autowiring (Which is what you have in your AuthenticationProvider . 但是,这与自动装配不同(这是AuthenticationProvider

If you wish to autowire your AppEngineUserStore you will need to register it in your application context and turn on autowiring. 如果您希望自动连接AppEngineUserStore,则需要在应用程序上下文中注册它并打开自动装配。 This can be done as follows: 可以按照以下步骤进行:

<context:annotation-config />

Spring can also scan for beans to autowire if you use: 如果使用以下命令,Spring还可以扫描要自动接线的bean:

<context:component-scan base-package="com.my.package" />

This will automatically turn on autowiring 这将自动打开自动接线

If you are using autowired you say that you are using annoations in the spring-context. 如果您正在使用自动装配,则表示您在弹簧上下文中使用标注。

<context:annotation-config />

Else you need to do xml based DI. 否则,您需要执行基于XML的DI。

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

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