春季启动:仅将@Configuration应用于某些软件包

编程入门 行业动态 更新时间:2024-10-25 18:35:26
本文介绍了春季启动:仅将@Configuration应用于某些软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 @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应用于某些软件包

本文发布于:2023-07-05 07:50:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:软件包   应用于   春季   Configuration

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!