3

私が取り組んでいるかなり単純な Spring Boot アプリがあり、いくつかの Java Config クラスを使用しています。ただし、構成はピックアップされていないようです。いたるところにブレークポイントがありますが、何もトリップしません。RuntimeExceptionデバッガーがフリッツにあるかどうかを確認するために、数秒間投げさえしました。

私のメインクラスには、標準の Spring Boot メインがあります。

@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

ご覧のとおり、 と でタグ付けしまし@ComponentScan@EnableAutoConfigurationApplicationクラスはクラスパスのルートにあります。注釈についての私の理解では、@ComponentScanその下にあるすべての構成クラスを検索します。

1 層下のパッケージには、すべての構成クラスがあります。

私の「共通」構成

@Configuration
@EnableJpaRepositories("com.codechimp.XXX.repository")
@EnableTransactionManagement
public class AppCommonConfig {
    @Inject
    private Environment environment;

    /* Define common beans here like datasource and such */
}

そして私のSpring Security構成

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Inject
    private LocalXXXUserDetailsService localXXXUserDetailsService;

        /**
     * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(HttpSecurity)
     */
    @Autowired
    protected void configure(HttpSecurity http) throws Exception {
        //  Configure http
    }

    /**
     * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configureGlobal(AuthenticationManagerBuilder)
     */
    @Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
        // Configure auth
    }
}

ただし、アプリを実行すると、これらの構成クラスのいずれのメソッドも呼び出されないようです。あたかも完全に無視されているかのようです。私が言ったように、私はブレークポイントを設定し、次のようにすべてのメソッドの最初に RuntimeException をスローすることさえ試みました:

if (true)
    throw new RuntimeException("Break!");

確かに、Java Config の使用経験はあまりありませんが、ドキュメントを何度も読み返しましたが、欠けている部分は見当たりません。

4

2 に答える 2

1

私はあなたがする必要があると思いApplicationます@Configuration

デフォルトのパッケージから実行するのは良い考えではありません@ComponentScan(「クラスパスのルート」とは、それが意味するものだと思います)。それは確かにいくつかのことをオフにしますが、より深刻なことに、クラスパス上のすべての jar の大規模なスキャンが発生します。これは良い考えではありません (また、アプリが失敗する可能性があります)。

于 2014-04-03T06:44:37.317 に答える