Java 原因:org.springframework.beans.factory.NoSuchBean定义异常:没有类型为'的合格bean;repository.RoleRepository';可获得的
我是spring boot新手,在启动应用程序时遇到问题,出现以下错误:Java 原因:org.springframework.beans.factory.NoSuchBean定义异常:没有类型为'的合格bean;repository.RoleRepository';可获得的,java,spring-boot,Java,Spring Boot,我是spring boot新手,在启动应用程序时遇到问题,出现以下错误: Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMet
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'repository.RoleRepository' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:347)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1107)
at mereuta.marian.tennis01.Tennis01Application.main(Tennis01Application.java:23)
... 5 more
这是我的申请代码:
package mereuta.marian.tennis01;
import model.Horaire;
import model.Role;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import repository.HoraireRepository;
import repository.RoleRepository;
import java.sql.Time;
@SpringBootApplication
public class Tennis01Application {
public static void main(String[] args) {
ApplicationContext context= SpringApplication.run(Tennis01Application.class, args);
// HoraireRepository horaireRepository=context.getBean(HoraireRepository.class);
//horaireRepository.save(new Horaire(new Time(8, 0,0), new Time(22,0,0),"Lundi"));
RoleRepository roleRepository=context.getBean(RoleRepository.class);
roleRepository.save(new Role("client"));
}
}
您好,请尝试在
roleRepository
的main
函数之外使用@Autowired
,并使用另一个带有@PostConstruct
的函数来保存某些内容。我发现了错误,因为我的包在java spring boot的主包之外