简体   繁体   中英

Bean property list definition in JavaConfig class

I'm trying to convert my context.xml configuration file to pure javaConfig and for the most part i think i'd found my answers but one thing exactly is a black magic for me.

My 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>

I've managed to replace everything but:

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

and my resulting JavaConfig class looks like this:

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();
    }
}

My tries to find an answer for my question missed my point, becouse @Bean properties are often misstreated as properties in app.properties file. I'm rather new to SPRING and trying to understand javaConfig class. I've found out, that simple dependency can be injected as this:

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

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

but in this case my IDE throws an error that ContentNegotiatingViewResolver has only default constructor. And summarizing, I'd like to know how to define this ContentNegotiatingViewResolver in javaConfig.

you need to use ContentNegotiatingViewResolver.setViewResolvers(list), which accepts list of view resolvers. Please check below configuration.

 * 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();
}

Check the this tutorial here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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