将XML中定义的Bean自动装配到Spring的Java Config中

时间:2014-06-15 00:13:38

标签: spring spring-security spring-annotations

我有一个xml configuration春天如下:

<bean id="processor"
    class="org.springframework.security.saml.processor.SAMLProcessorImpl">
    <constructor-arg>
        <list>
            <ref bean="redirectBinding" />
            <ref bean="postBinding" />
            <ref bean="artifactBinding" />
            <ref bean="soapBinding" />
            <ref bean="paosBinding" />
        </list>
    </constructor-arg>
</bean>

我还有另一个Spring的Spring配置如下:

@ImportResource({ "classpath:security/samlMetadata.xml" })
@Configuration
public class SecConfig{

@Autowired
private SAMLProcessorImpl processor;

@Bean(name ="webSSOProfileConsumer")
public WebSSOProfileConsumer webSSOProfileConsumer(){
    WebSSOProfileConsumerImpl webSSOProfileConsumerImpl = new WebSSOProfileConsumerImpl();
    try {
        webSSOProfileConsumerImpl.setProcessor(processor);
        webSSOProfileConsumerImpl.afterPropertiesSet();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return webSSOProfileConsumerImpl;
}

}

我将processor bean视为 null ,如果我在自动装配时遗漏了一些基本的东西,请帮助我。

修改

以下是web.xml的相关部分我跳过的唯一内容是welcome-file-list

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.config.WebConfig
    </param-value>
</context-param>
<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>production</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>security</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>security</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
<filter>
    <filter-name>filterChainProxy</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>filterChainProxy</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

EDIT2

@Configuration
@ImportResource({ "classpath:security/samlMetadata.xml" })
@Import({SecureConfig.class})
@Profile("production")
@ComponentScan(basePackages = "com.config")
public class WebConfig extends WebMvcConfigurationSupport {

@Bean(name = "viewResolver")
public InternalResourceViewResolver viewResolver() throws Exception {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/jsp/");
    viewResolver.setSuffix(".jsp");

    return viewResolver;
}

} 

对不起,我之前没有添加这个课程。

0 个答案:

没有答案