2017-09-13 6 views
0

私は純粋javaConfigに私のcontext.xml構成ファイルを変換しようとしていると、ほとんどの部分のために私は私の答えを見つけたと思うが、1つの事は正確に私のために黒魔術です。ビーンプロパティリストの定義

私はしかし、すべて置き換えることができた私の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のプロパティがしばしばpとして誤って処理されるため、私の質問に対する答えを見つけられなかったファイル番号:app.properties私はむしろSPRINGには新しく、javaConfigクラスを理解しようとしています。

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

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

をが、この場合には、私のIDEはContentNegotiatingViewResolverは、デフォルトコンストラクタを持っているというエラーがスローされます:私は、単純な依存関係がこのように注入することができること、を発見しました。まとめると、javaConfigでこれを定義する方法を知りたいのですが、ContentNegotiatingViewResolverです。

答えて

0

あなたはContentNegotiatingViewResolver.setViewResolversビューリゾルバのリストを受け付ける(リスト)を、使用する必要があります。以下の設定を確認してください。

ここでthisのチュートリアルを確認してください。