2017-11-15 5 views
0

春の新機能で、そこにApplicaionConfig.javaを定義しました。プロパティファイルの詳細を配置しました。春のコンポーネントスキャンの順序

package com.rao.first; 

// 
// import statements 
// 

@Configuration 
@ComponentScan(basePackageClasses = { ApplicationConfig.class }, basePackages = { "com.rao.first" }) 
@PropertySources({ 
@PropertySource("file:${webapp.root}/resources/config/application.properties"), 
@PropertySource(value = "file:${conf.dir}/someother.properties", ignoreResourceNotFound = true) }) 
class ApplicationConfig 
{ 
    @Bean 
    ServletContextListener logbackConfigListener() 
    { 
     return new LogbackConfigListener(); 

    } 
} 

いくつかのコントローラクラスを定義しました。

package com.rao.first.controller; 
// 
// import statements 
// 
@Controller 
@RequestMapping(value = "/first") 
public class FirstController 
{ 
    private String viewerUrl; 

    @Inject 
    public FirstController(Environment env) 
    { 
     this.viewerUrl = env.getProperty("property1"); 
    } 

    // 
    // 

} 

しかし、ここで最初のコントローラが実行され、そのApplicaitonConfigが実行された後に、プロパティファイルからデータを取得するようにできません。

と春-servlet.xmlファイル構成は

<context:component-scan base-package="com.rao.first.view" /> 
<context:component-scan base-package="com.rao.controller.security" /> 
<context:component-scan base-package="com.rao.controller" /> 

あり、実行の順序を設定することができますどのように私を導いてください?

+0

'@Order(Ordered.HEIGHEST_PRECEDENCE) 'を使用できます。 –

+0

@AtaurRahmanMunna私は' spring 4.2.5'を使用しています...このバージョンでは、注文されたインターフェイスは利用できません。 – Rao

+0

純粋な整数を使用してください。例えば@Order(2)です。数字が小さいほど、優先順位が高くなります。回答:https://stackoverflow.com/a/46278128/4423636を参照してください。 –

答えて

0

org.springframework.context.annotation.PropertySourceアノテーションを使用して、プロパティファイルから値を取得することができます。

だからあなたのコントローラクラスは次のように考えられます

package com.rao.first.controller; 
import org.springframework.context.annotation.PropertySource; 
// 
// import statements 
// 
@Controller 
@RequestMapping(value = "/first") 
@PropertySource("classpath:/com/rao/app.properties") 
public class FirstController 
{ 
    private String viewerUrl; 

    @Autowired 
    Environment env; 

    public FirstController() 
    { 
     this.viewerUrl = env.getProperty("property1"); 
    } 

    // 
    // 

} 

私はそれはさようなら、あなたを助け願っています。