Spring社交applicationURL设置异常

时间:2015-04-03 16:50:49

标签: spring-social

对于Spring Social版本1.1.0.RELEASE,我需要为ProviderSignInController设置applicationUrl,因为我的应用程序(Tomcat应用程序)托管在代理(Apache Web服务器)后面。根据{{​​3}},我将其设置如下:

<bean id="providerSignInController"
    class="org.springframework.social.connect.web.ProviderSignInController">
    <property name="signInUrl" value="/accounts/login" />
    <property name="signUpUrl" value="/accounts/signup" />
    <property name="postSignInUrl" value="/accounts/profile" />
    <property name="applicationUrl" value="${applicationUrl}" />
</bean>

但是,在部署应用程序时,我在Tomcat catalina.out中得到一个例外:

PropertyAccessException 1:
org.springframework.beans.MethodInvocationException: Property
'applicationUrl' threw exception; nested exception is
java.lang.NullPointerException>org.springframework.beans.factory.BeanCreationException:
Error creating bean with name
'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0'
defined in ServletContext resource [/WEB-INF/spring-servlet.xml]:
Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'providerSignInController' defined in ServletContext
resource [/WEB-INF/spring-servlet.xml]: Error setting property values;
nested exception is
org.springframework.beans.PropertyBatchUpdateException; nested
PropertyAccessExceptions (1) are:
PropertyAccessException 1:
org.springframework.beans.MethodInvocationException: Property
'applicationUrl' threw exception; nested exception is
java.lang.NullPointerException
        at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)

有什么建议吗?感谢。

1 个答案:

答案 0 :(得分:1)

它应该是Spring Social版本1.1.0.RELEASE中的ProviderSignInController类的错误。在ProviderSignInController类中,在设置属性后创建connectSupport:

public void afterPropertiesSet() throws Exception {
    this.connectSupport = new ConnectSupport(sessionStrategy);
    this.connectSupport.setUseAuthenticateUrl(true);
};

因此,当调用方法setApplicationUrl时,connectSupport仍然为null。

现在,当我按照下面显示的方式配置它时,它可以工作。或者,如果我恢复到版本1.0.3.RELEASE,它也可以正常工作。

<bean id="providerSignInController"
    class="org.springframework.social.connect.web.ProviderSignInController">
    <property name="signInUrl" value="/accounts/login" />
    <property name="signUpUrl" value="/accounts/signup" />
    <property name="postSignInUrl" value="/accounts/profile" />
</bean>
<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="providerSignInController" />
    <property name="targetMethod" value="setApplicationUrl" />
    <property name="arguments">
        <list>
            <bean class="java.lang.String">
                <constructor-arg value="${applicationUrl}" />
            </bean>
        </list>
    </property>
</bean>

<强>后续

此问题已在Spring Social版本1.1.3.RELEASE中得到解决。在ProviderSignInController类中,方法afterPropertiesSet已更新为:

public void afterPropertiesSet() throws Exception {
    this.connectSupport = new ConnectSupport(sessionStrategy);
    this.connectSupport.setUseAuthenticateUrl(true);
    if (this.applicationUrl != null) {
        this.connectSupport.setApplicationUrl(applicationUrl);
    }
};

因此,上面给出的配置解决方法(顺便说一下,不再适用于Spring Social版本1.1.3.RELEASE)现在可以简化为:

<bean id="providerSignInController"
    class="org.springframework.social.connect.web.ProviderSignInController">
    <property name="signInUrl" value="/accounts/login" />
    <property name="signUpUrl" value="/accounts/signup" />
    <property name="postSignInUrl" value="/accounts/profile" />
    <property name="applicationUrl" value="${applicationUrl}" />
</bean>