[英]Spring Restful Service with Spring Boot - NoSuchBeanDefinitionException
I am trying to build a Spring RESTful Webservice. 我正在尝试构建一个Spring RESTful Web服务。 I have ended up in NoSuchBeanDefinitionException.
我最终遇到了NoSuchBeanDefinitionException。
Could anyone please help me? 谁能帮我吗? Thanks in advance.
提前致谢。
Packages
包
ProfileRepository
ProfileRepository
@Repository
public interface ProfileRepository extends JpaRepository<AbstractProfile, Long> {
}
ProfileService
ProfileService
public interface ProfileService {
List<AbstractProfile> findAll();
}
ProfileServiceImpl
ProfileServiceImpl
@Service
public class ProfileServiceImpl implements ProfileService {
@Autowired
private ProfileRepository repository;
@Override
public List<AbstractProfile> findAll() {
return repository.findAll();
}
}
ProfileController
ProfileController可
@RestController
@ContextConfiguration(locations = "classpath:META-INF/mysql-spring-context.xml")
public class ProfileController {
@Autowired
private ProfileService service;
@RequestMapping("/profile")
public List<AbstractProfile> getAllProfiles() {
return service.findAll();
}
}
Spring ProfileApp
Spring ProfileApp
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EntityScan
public class ProfileApp {
public static void main(String[] args) {
SpringApplication.run(ProfileApp.class, args);
}
}
Exception
例外
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ch.example.repositories.ProfileRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Spring-Context
Spring的上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Database -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/entrevista_db" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- Entity Manager -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="m-entrevista" />
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Jpa Rep -->
<jpa:repositories base-package="ch.example.repositories">
</jpa:repositories>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="ch.example.service"/>
<bean id="service" class="ch.example.service.ProfileServiceImpl"></bean>
</beans>
As per the documentation @ComponentScan
- Configures component scanning directives for use with @Configuration classes. 按照文档
@ComponentScan
ComponentScan-配置与@Configuration类一起使用的组件扫描指令。 Provides support parallel with Spring XML's <context:component-scan>
element. 提供与Spring XML的
<context:component-scan>
元素并行的支持。 One of basePackageClasses(), basePackages() or its alias value() must be specified. 必须指定basePackageClasses(),basePackages()之一或其别名value()。
So change it to 因此将其更改为
@ComponentScan({"ch.example.repositories","ch.example.service"})
The problem was that the spring-boot didn't load the spring-context.xml, where I had all the configurations. 问题在于spring-boot不会加载spring-context.xml,在该目录中我具有所有配置。 Therefore I have to modify my ProfileApp like this.
因此,我必须像这样修改我的ProfileApp。
Spring-Boot: Profile App
春季启动:个人资料应用
@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:META-INF/mysql-spring-context.xml")
public class ProfileApp {
public static void main(String[] args) {
SpringApplication.run(ProfileApp.class, args);
}
}
That's all. 就这样。 It worked..
有效..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.