Spring boot: apply @Configuration to certain package only

孤人 提交于 2020-01-14 10:16:26

问题


I am using @Configuration to config cookies, while in my project there is 2 packages and I only want to apply the config to one of the package.
Are there any ways to set the target package for @Configuration?

package structure:
--app
----packageA
------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;
    }
}

回答1:


Actually, you can use @ComponentScan to specify which packages to scan for and @EnableAutoConfiguration with the exclude options to omit the classes that you want to omit. You have to use this in your main application class.

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

Alternatively, you can also provide the classes that you want to exclude in your configuration file.

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



回答2:


You can try with @ComponentScan("packageA")

check http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html




回答3:


In Spring Boot, your main class annotated with @SpringBootApplicatio will already include @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes, so all your classes will be auto scanned. Using exclude in @SpringBootApplication will only exclude classes, but if you have lots of classes in your package, the code will look nasty.

In your case, the easiest way is to move your main Spring Boot application entry class into the package you want to be configured and auto scanned:

----packageA

------app

------MyConfigClass.java

----packageB




回答4:


If you want to go fine-grained like this, I wouldn't use component scan at all. @SpringBootApplication is a shortcut for three things:

  1. Enable auto-configuration
  2. Enable component scan in the package where the spring boot app resides (including sub packages)
  3. Make sure the spring boot application itself is a configuration (so that you can contribute extra beans, import configurations, etc

If you want a Spring Boot application that only looks for configuration in a certain places, I'd do something like this:

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

IMO, it is much more clear to include what you want in such scenario rather than having scanning with excludes. Perhaps you may want to restructure your app so you don't have to do this in the first place? Using profile is one option so that those unwanted configurations only applies when a profile is enabled.



来源:https://stackoverflow.com/questions/44103043/spring-boot-apply-configuration-to-certain-package-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!