繁体   English   中英

带有Spring Boot的Spring Restful Service-NoSuchBeanDefinitionException

[英]Spring Restful Service with Spring Boot - NoSuchBeanDefinitionException

我正在尝试构建一个Spring RESTful Web服务。 我最终遇到了NoSuchBeanDefinitionException。

谁能帮我吗? 提前致谢。

  • 实体-ch.example.entities.core
  • 存储库-ch.example.repositories
  • 服务-ch.example.service

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>

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

因此将其更改为

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

问题在于spring-boot不会加载spring-context.xml,在该目录中我具有所有配置。 因此,我必须像这样修改我的ProfileApp。

春季启动:个人资料应用

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

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

就这样。 有效..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM