I have following class as:-
@SpringBootApplication
@ComponentScan("com.ma.demospringboot")
public class DemoSpringBootApp {
public static void main(String[] args) {
SpringApplication.run(DemoSpringBootApp.class, args);
}
}
I have following class as:-
@Service
public class TopicService {
// if I comment out following autowired, then it is ok.
@Autowired
private TopicRepository topicRepository;
}
I have following interface as:-
public interface TopicRepository extends CrudRepository<Topic, String> {
}
I have following class as:-
@Entity
public class Topic {
@Id
private String id;
private String name;
private String description;
public Topic() {
}
}
I got following error when try to execute:-
APPLICATION FAILED TO START
Description:
Field topicRepository in com.ma.demospringboot.service.TopicService required a bean of type 'com.ma.demospringboot.repository.TopicRepository' that could not be found.
Action:
Consider defining a bean of type 'com.ma.demospringboot.repository.TopicRepository' in your configuration.
Spring container is not finding your repository classes during the scan, add @EnableJpaRepositories
to explicitly specify the packages of where your repository classes exist, as shown below:
@SpringBootApplication
@ComponentScan("com.ma.demospringboot")
@EnableJpaRepositories("com.ma.demospringboot.repository")
@EntityScan(basePackages = "com.ma.demospringboot.domain")
public class DemoSpringBootApp {
//your current code here
}
UPDATE1:
Not a managed type: class com.ma.demospringboot.domain.Topic
Now, your Topic
entity class not found, so you need to @EntityScan(basePackages = "com.ma.demospringboot.domain")
to scan the entity classes (as show above).
UPDATE2:
I did exactly as you suggested, but NOT working
There is a problem with the way that you are packaging the classes, double check on that, also ensure that the latest classes have been compiled/built & used by the server.
Your Spring
container is unable to find and identify your JPA repositories.
Add @EnableJpaRepositories("com.ma.demospringboot.repository")
on your DemoSpringBootApp
class
@SpringBootApplication
@ComponentScan("com.ma.demospringboot")
@EnableJpaRepositories("com.ma.demospringboot.repository")
public class DemoSpringBootApp {
public static void main(String[] args) {
SpringApplication.run(DemoSpringBootApp.class, args);
}
}
在您的ApplicationContext.xml中添加以下代码
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.