Spring Restful 服务 Spring 启动 - NoSuchBeanDefinitionException

Spring Restful Service with Spring Boot - NoSuchBeanDefinitionException

我正在尝试构建一个 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)}

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>

根据 documentation @ComponentScan - 配置组件扫描指令以与@Configuration 类 一起使用。提供与 Spring XML 的 <context:component-scan> 元素平行的支持。 必须指定 basePackageClasses()、basePackages() 或其别名 value() 之一。

所以改成

@ComponentScan({"ch.example.repositories","ch.example.service"})

问题是 spring-boot 没有加载 spring-context.xml,而我拥有所有配置。因此我必须像这样修改我的 ProfileApp。

Spring-启动:配置文件应用程序

@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:META-INF/mysql-spring-context.xml")
public class ProfileApp {

    public static void main(String[] args) {
        SpringApplication.run(ProfileApp.class, args);
    }
}

就是这样。成功了..