Я новичок в пружине, и я пытаюсь сделать класс конфигурации для настройки Spring Security для API Spring Boot. Однако, когда я создал класс с аннотацией @configuration , я получил ошибку. Это та же ошибка, пытаюсь ли я определить бобы в классе или если это пустой класс. Я попытался сделать это в более простом проекте, и я не получил эту ошибку. Вот мой класс конфигурации: < /p>
package com.school_database_access_api;
import javax.sql.DataSource;
import org.springframework.config.java.annotation.Configuration;
@Configuration
public class SecurityConfig
{
}
< /code>
А вот есть трассировка стека: < /p>
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m [2m (v3.4.5)[0;39m
[2m2025-05-01T16:18:07.658+02:00[0;39m [32m INFO[0;39m [35m12848[0;39m [2m--- [SchoolDbAccessAPI] [ restartedMain] [0;39m[36mc.s.SchoolDbAccessApiApplication [0;39m [2m:[0;39m Starting SchoolDbAccessApiApplication using Java 21.0.6 with PID 12848 (C:\Users\lucas\Projets\Eclipse-Workspace\FirstJavaApp\SchoolDbAccessAPI\target\classes started by lucas in C:\Users\lucas\Projets\Eclipse-Workspace\FirstJavaApp\SchoolDbAccessAPI)
[2m2025-05-01T16:18:07.661+02:00[0;39m [32m INFO[0;39m [35m12848[0;39m [2m--- [SchoolDbAccessAPI] [ restartedMain] [0;39m[36mc.s.SchoolDbAccessApiApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2025-05-01T16:18:07.733+02:00[0;39m [32m INFO[0;39m [35m12848[0;39m [2m--- [SchoolDbAccessAPI] [ restartedMain] [0;39m[36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[2m2025-05-01T16:18:07.733+02:00[0;39m [32m INFO[0;39m [35m12848[0;39m [2m--- [SchoolDbAccessAPI] [ restartedMain] [0;39m[36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[2m2025-05-01T16:18:07.891+02:00[0;39m [33m WARN[0;39m [35m12848[0;39m [2m--- [SchoolDbAccessAPI] [ restartedMain] [0;39m[36mConfigServletWebServerApplicationContext[0;39m [2m:[0;39m Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\Users\lucas\Projets\Eclipse-Workspace\FirstJavaApp\SchoolDbAccessAPI\target\classes\com\school_database_access_api\SecurityConfig.class]
[2m2025-05-01T16:18:07.959+02:00[0;39m [31mERROR[0;39m [35m12848[0;39m [2m--- [SchoolDbAccessAPI] [ restartedMain] [0;39m[36mo.s.boot.SpringApplication [0;39m [2m:[0;39m Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\Users\lucas\Projets\Eclipse-Workspace\FirstJavaApp\SchoolDbAccessAPI\target\classes\com\school_database_access_api\SecurityConfig.class]
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:510) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:351) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:277) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:128) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:346) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:281) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:204) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:172) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:418) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:290) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:349) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:118) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:791) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:609) ~[spring-context-6.2.6.jar:6.2.6]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.4.5.jar:3.4.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:753) ~[spring-boot-3.4.5.jar:3.4.5]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-3.4.5.jar:3.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-3.4.5.jar:3.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1362) ~[spring-boot-3.4.5.jar:3.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1351) ~[spring-boot-3.4.5.jar:3.4.5]
at com.school_database_access_api.SchoolDbAccessApiApplication.main(SchoolDbAccessApiApplication.java:11) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.4.5.jar:3.4.5]
Caused by: java.lang.annotation.AnnotationFormatError: Invalid default: public abstract org.springframework.beans.factory.annotation.Autowire org.springframework.config.java.annotation.Configuration.defaultAutowire()
at java.base/java.lang.reflect.Method.getDefaultValue(Method.java:781) ~[na:na]
at org.springframework.core.annotation.AttributeMethods.(AttributeMethods.java:73) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AttributeMethods.compute(AttributeMethods.java:268) ~[spring-core-6.2.6.jar:6.2.6]
at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
at org.springframework.core.annotation.AttributeMethods.forAnnotationType(AttributeMethods.java:251) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMapping.(AnnotationTypeMapping.java:119) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMappings.addIfPossible(AnnotationTypeMappings.java:118) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMappings.addAllMappings(AnnotationTypeMappings.java:80) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMappings.(AnnotationTypeMappings.java:72) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMappings$Cache.createMappings(AnnotationTypeMappings.java:278) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMappings$Cache.lambda$get$0(AnnotationTypeMappings.java:273) ~[spring-core-6.2.6.jar:6.2.6]
at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
at org.springframework.core.annotation.AnnotationTypeMappings$Cache.get(AnnotationTypeMappings.java:273) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMappings.forAnnotationType(AnnotationTypeMappings.java:226) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMappings.forAnnotationType(AnnotationTypeMappings.java:189) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.AnnotationTypeMappings.forAnnotationType(AnnotationTypeMappings.java:175) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.TypeMappedAnnotation.of(TypeMappedAnnotation.java:617) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.annotation.MergedAnnotation.of(MergedAnnotation.java:612) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.type.classreading.MergedAnnotationReadingVisitor.visitEnd(MergedAnnotationReadingVisitor.java:98) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.asm.ClassReader.readElementValues(ClassReader.java:3023) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.asm.ClassReader.accept(ClassReader.java:611) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.asm.ClassReader.accept(ClassReader.java:427) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.type.classreading.SimpleMetadataReader.(SimpleMetadataReader.java:48) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:122) ~[spring-core-6.2.6.jar:6.2.6]
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:470) ~[spring-context-6.2.6.jar:6.2.6]
... 23 common frames omitted
< /code>
Вот мой pom.xml: < /p>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.4.5
com.LucasBrelivet
SchoolDbAccessAPI
0.0.1-SNAPSHOT
SchoolDbAccessAPI
Demo project for Spring Boot
21
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.javaconfig
spring-javaconfig
1.0.0.m3
com.mysql
mysql-connector-j
runtime
org.springframework.boot
spring-boot-starter-hateoas
org.springdoc
springdoc-openapi-starter-webmvc-ui
2.8.6
org.springframework.boot
spring-boot-maven-plugin
< /code>
Трассировка стека упоминает аннотацию Autowire, которую я использую в паре мест, а именно поля в моем контроллере и моих тестах. Видимо, вот это по умолчанию, и это недействительно? По умолчанию для чего? Почему это недействительно, и что я могу сделать, чтобы его изменить?
Подробнее здесь: https://stackoverflow.com/questions/796 ... ed-to-read