我正在尝试构建一个Spring RESTful Web服务。我最终遇到了NoSuchBeanDefinitionException。
有人可以帮帮我吗? 提前谢谢。
包
ProfileRepository
@Repository
public interface ProfileRepository extends JpaRepository<AbstractProfile, Long> {
}
ProfileService
public interface ProfileService {
List<AbstractProfile> findAll();
}
ProfileServiceImpl
@Service
public class ProfileServiceImpl implements ProfileService {
@Autowired
private ProfileRepository repository;
@Override
public List<AbstractProfile> findAll() {
return repository.findAll();
}
}
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
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EntityScan
public class ProfileApp {
public static void main(String[] args) {
SpringApplication.run(ProfileApp.class, args);
}
}
例外
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)}
弹簧背景的
<?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>
答案 0 :(得分:1)
根据documentation
@ComponentScan
- 配置组件扫描指令以与@Configuration类一起使用。提供与Spring XML的<context:component-scan>
元素并行的支持。
必须指定basePackageClasses(),basePackages()或其别名值()之一。
所以将其改为
@ComponentScan({"ch.example.repositories","ch.example.service"})
答案 1 :(得分:1)
问题是spring-boot没有加载spring-context.xml,我有所有的配置。因此,我必须像这样修改我的ProfileApp。
Spring-Boot:个人资料应用
@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:META-INF/mysql-spring-context.xml")
public class ProfileApp {
public static void main(String[] args) {
SpringApplication.run(ProfileApp.class, args);
}
}
这就是全部。它工作..