我正在使用 @Configuration 来配置cookie,而在我的项目中有2个软件包,我只想将配置应用于其中一个软件包。 是否可以通过任何方式为 @Configuration 设置目标软件包?
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?
程序包结构: --app ---- packageA - ---MyConfigClass.java ---- packageB
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; } }推荐答案
实际上,您可以使用 @ComponentScan 指定要扫描的软件包,并使用 @EnableAutoConfiguration 的排除选项来省略类您想忽略的地方。您必须在主应用程序类中使用它。
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.更多推荐
春季启动:仅将@Configuration应用于某些软件包
发布评论