【问题标题】:Spring Restful Service with Spring Boot - NoSuchBeanDefinitionException带有 Spring Boot 的 Spring Restful 服务 - NoSuchBeanDefinitionException
【发布时间】:2015-03-05 15:54:12
【问题描述】:

我正在尝试构建一个 Spring RESTful Web 服务。我以 NoSuchBeanDefinitionException 告终。

谁能帮帮我? 提前致谢。

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

ProfileRepository

@Repository
public interface ProfileRepository extends JpaRepository<AbstractProfile, Long> {
}

配置文件服务

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();
    }
 }

配置文件控制器

@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>

【问题讨论】:

    标签: spring spring-mvc jpa spring-data spring-boot


    【解决方案1】:

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

    所以改成

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

    【讨论】:

    • 没错。但是当我将其更改为 @ComponentScan({...}) 时,它不起作用。所以我把这个标签放在我的 context.xml 中,然后导入 XML 资源。然后它工作了
    • 您链接到的文档适用于旧版本的 Spring。在 Spring 3.2 及更高版本中,@ComponentScan 不需要指定任何包,并且“如果未定义特定包,则将从具有此注释的类的包中进行扫描”。 Spring Boot 使用 Spring 4.0 或 4.1(取决于您使用的 Boot 版本)。
    【解决方案2】:

    问题是 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);
        }
    }
    

    就是这样。成功了。。

    【讨论】:

    • 您可能想了解 Boot 的自动配置支持。您的大部分 XML 配置在 Spring Boot 中是不必要的,可以用 src/main/resources/application.properties 中的几行替换。
    • 用属性代替context.xml有什么用?它会表现得更好/更快吗?
    • 这是关于减少您必须编写和维护的配置数量。通过自己配置所有内容,您错过了 Boot 的主要好处之一。您应该能够用 Boot 的自动配置和 application.properties 中的几行替换所有 XML 来配置 DataSource
    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 2018-10-25
    • 2015-01-14
    • 2016-12-06
    • 2015-04-29
    • 2015-08-16
    • 2017-12-15
    • 2015-07-19
    相关资源
    最近更新 更多