Spring boot: apply @ configuration for specific package only

I use @Configurationcookies for configuration files, while there are 2 packages in my project, and I just want to apply the configuration to one of the packages.
Is there a way to install the target package for @Configuration?

Package structure:
--app
---- package Hotel
------MyConfigClass.java
---- packageB

@EnableJdbcHttpSession(maxInactiveIntervalInSeconds = 1800)
@Configuration
public class MyConfigClass extends WebMvcConfigurerAdapter {
@Bean
    public CookieSerializer cookieSerializer() {
        // I want the follow cookie config only apply to packageA
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("myCookieName");
        serializer.setCookiePath("/somePath/");
        return serializer;
    }
}
+4
source share
4 answers

@ComponentScan, , @EnableAutoConfiguration, , , . .

@EnableAutoConfiguration(exclude = { Class1.class,
        Class2.class,
        Class3.class }, 
excludeName = {"mypackage.classname"}))
@Configuration
@ComponentScan(basePackages = { "mypackage" })
public class MyApplication {

public static void main(String[] args) throws Exception {
        SpringApplication.run(MyApplication.class, args);
    }
}

, .

# AUTO-CONFIGURATION
spring.autoconfigure.exclude= # Auto-configuration classes to exclude.
0

Spring Boot , @SpringBootApplicatio, @Configuration, @EnableAutoConfiguration @ComponentScan , . exclude @SpringBootApplication , , .

- Spring Boot application , :

----

------

------ MyConfigClass.java

---- packageB

+1

, , . @SpringBootApplication :

  • , spring ( )
  • , spring - ( beans, ..

If you want a spring boot application that is only looking for configuration in specific places, I would do something like this:

@Configuration
@EnableAutoConfiguration
@Import(MyConfigClass.class)
public class MySpringBootApp { ... }

IMO, it’s much more clear to include what you want in such a scenario, rather than scanning with exceptions. Perhaps you want to rebuild the application so that you do not have to do this in the first place? Using a profile is one option, so these unwanted configurations only apply when the profile is on.

0
source

Source: https://habr.com/ru/post/1677655/


All Articles