JavaConfig类中的Bean属性列表定义

时间:2017-09-13 11:31:10

标签: java spring

我正在尝试将我的context.xml配置文件转换为纯javaConfig,并且在大多数情况下我认为我找到了答案,但有一件事对我来说是一个黑魔法。

我的context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:property-placeholder location="classpath:app.properties"
        system-properties-mode="OVERRIDE" />

    <context:component-scan
        base-package="mypackage.servlets.dao,mypackage.servlets" />

    <mvc:annotation-driven />

    <alias name="${dao}" alias="daoType"/>

    <bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="defaultViews">
            <list>
                <bean
                    class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
            </list>
        </property>
    </bean>
</beans>

我设法替换了所有东西,但是:

<bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="defaultViews">
            <list>
                <bean
                    class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
            </list>
        </property>
    </bean>

我生成的JavaConfig类如下所示:

package mypackage.servlets;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import mypackage.servlets.dao.LibraryDAO;

@Configuration
@ComponentScan("mypackage.servlets.dao,mypackage.servlets")
@EnableWebMvc
public class ConfigClass {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Autowired
    private ApplicationContext context;

    @Bean
    public LibraryDAO MyServiceAlias(@Value("${dao}") String qualifier) {
        return (LibraryDAO) context.getBean(qualifier);
    }

    @Bean
    public ContentNegotiatingViewResolver myContentNegotiation()
    {
        return new ContentNegotiatingViewResolver();
    }

    @Bean
    public MappingJackson2JsonView myJackson()
    {
        return new MappingJackson2JsonView();
    }
}

我试图找到我的问题的答案错过了我的观点,因为@Bean属性经常被错误地视为app.properties文件中的属性。我对SPRING很新,并试图理解javaConfig类。我发现,这个简单的依赖可以这样注入:

@Bean
    public ContentNegotiatingViewResolver myContentNegotiation()
    {
        return new ContentNegotiatingViewResolver(myJackson());
    }

    @Bean
    public MappingJackson2JsonView myJackson()
    {
        return new MappingJackson2JsonView();
    }

但在这种情况下,我的IDE会抛出ContentNegotiatingViewResolver只有默认构造函数的错误。总结一下,我想知道如何在javaConfig中定义这个ContentNegotiatingViewResolver

1 个答案:

答案 0 :(得分:0)

您需要使用ContentNegotiatingViewResolver.setViewResolvers(list),它接受视图解析器列表。请检查以下配置。

 * Configure ContentNegotiationManager
 */
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.ignoreAcceptHeader(true).defaultContentType(
            MediaType.TEXT_HTML);
}

/*
 * Configure ContentNegotiatingViewResolver
 */
@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
    ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
    resolver.setContentNegotiationManager(manager);

    // Define all possible view resolvers
    List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
    resolvers.add(jsonViewResolver());

    resolver.setViewResolvers(resolvers);
    return resolver;
}

 /*
 * Configure View resolver to provide JSON output using JACKSON library to
 * convert object in JSON format.
 */
@Bean
public ViewResolver jsonViewResolver() {
    return new JsonViewResolver();
}

在此处查看this教程。