Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot application is ignoring java config

I have a fairly simple Spring Boot app I am working on that uses a few Java Config classes. However, it seems that the configuration is not being picked up. I have break points all over, but nothing gets tripped. I even tossed a few RuntimeExceptions just to see if maybe my debugger was on the fritz.

In my main class, I have the standard Spring Boot main:

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

As you can see, I tagged it with @ComponentScan and @EnableAutoConfiguration. The Application class lives at the root of the classpath. My understanding of the @ComponentScan annotation is that it will search for all configuration classes beneath it.

In a package one layer down I have all the config classes:

My "Common" configuration

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

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

And my Spring Security configuration

@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
    }
}

However, when I run the app, it doesn't seem to call any of the methods in either of these config classes. It's as if they are being completely ignored. As I said, I have tried setting break points and even throwing a RuntimeException right in the beginning of all the methods like so:

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

Admittedly I have not had much experience with using Java Config, but I have been over the docs again and again and I am not seeing the missing piece(s).

like image 387
CodeChimp Avatar asked Jan 24 '26 05:01

CodeChimp


1 Answers

I think you need your Application to be a @Configuration.

It's not a great idea to do a @ComponentScan from the default package (I assume that's what you mean by "the root of the classpath"). That would definitely switch some things off, but more seriously it causes a huge scan of all jars on your classpath, which is not a great idea (and can cause the app to fail).

like image 177
Dave Syer Avatar answered Jan 27 '26 01:01

Dave Syer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!