簡體   English   中英

春季零件掃描順序

[英]Order of component scanning in spring

春天剛來的,已經定義了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,因此無法從屬性文件獲取數據。

而且spring-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" />

請指導我如何設置執行順序?

您可以使用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");
    }

    //
    //

}

希望對您有幫助,再見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM