spring-boot – Spring boot:仅将@Configuration应用于某个包

我正在使用@Configuration配置cookie,而在我的项目中有2个包,我只想将配置应用到其中一个包中.

有没有办法为@Configuration设置目标包?

包结构:
–app
—-程序包A
——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指定要扫描的包,使用exclude选项指定@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.
点赞