我正在开发一个应用程序,我只想使用 Spring 的 DI 功能。我的问题是我无法禁用 Spring Boot 的自动配置功能。我不断收到此异常:
2020-06-26 14:00:03.240 [main] TRACE o.s.b.diagnostics.FailureAnalyzers - Failed to load org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer
java.lang.NoClassDefFoundError: org/springframework/jdbc/CannotGetJdbcConnectionException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.springframework.boot.diagnostics.FailureAnalyzers.loadFailureAnalyzers(FailureAnalyzers.java:75)
at org.springframework.boot.diagnostics.FailureAnalyzers.<init>(FailureAnalyzers.java:66)
at org.springframework.boot.diagnostics.FailureAnalyzers.<init>(FailureAnalyzers.java:60)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204)
at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:441)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:427)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at MyApplication.main(MyApplication.java:19)
我尝试不使用@SpringBootApplicationonly @ComponentScan,但它没有用。我还尝试排除相关的自动配置类:
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
XADataSourceAutoConfiguration.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication .class, args);
}
}
但这也不起作用。我怎样才能完全摆脱自动配置?我没有找到@DisableAutoConfiguration注释,但是有一个@EnableAutoConfiguration.
编辑:
我的 bean 配置在与我的应用程序类相同的包中的单独文件中:
@Configuration
public class BeanConfig {
@Bean
public Database database() {
return DatabaseConfig.configureDatabase();
}
@Bean
public UserRepository userRepository() {
return new InMemoryUserRepository(
Collections.singletonList(new UserEntity(
1,
"user",
Arrays.asList(Permission.ADOPT.name(), Permission.EDIT_PROFILE.name()))));
}
@DependsOn("database")
@Bean
public DogRepository dogRepository() {
return new H2DogRepository();
}
@Bean
public AdoptDog adoptDog() {
return new AdoptDog(dogRepository());
}
@Bean
public FindDogs findDogs() {
return new FindDogs(dogRepository());
}
@Bean
public CreateDog createDog() {
return new CreateDog(dogRepository());
}
@Bean
public DeleteDogs deleteDogs() {
return new DeleteDogs(dogRepository());
}
@Bean
public FindUser findUser() {
return new FindUser(userRepository());
}
@PostConstruct
public void initialize() {
ApplicationEngine engine = ServerConfig.configureServer(
userRepository(), adoptDog(), findDogs(), createDog(), findUser(), deleteDogs());
DogUtils.loadDogs().forEach(dogRepository()::create);
CheckerUtils.runChecks(engine, userRepository());
}
}
我有一个不同的服务器技术,我正在使用这个项目来演示 Kotlin-Java 互操作性(所有这些文件都是 Kotlin 文件,在此引用)。