Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带Spring引导的Spring Restful服务-NoSuchBeanDefinitionException_Spring_Spring Mvc_Jpa_Spring Data_Spring Boot - Fatal编程技术网

带Spring引导的Spring Restful服务-NoSuchBeanDefinitionException

带Spring引导的Spring Restful服务-NoSuchBeanDefinitionException,spring,spring-mvc,jpa,spring-data,spring-boot,Spring,Spring Mvc,Jpa,Spring Data,Spring Boot,我正在尝试构建一个Spring RESTful Web服务。我最终没有这样的定义例外 谁能帮帮我吗? 提前谢谢 包裹 实体-ch.example.Entities.core repositories-ch.example.repositories 服务-ch.example.service 档案库 例外情况 Spring上下文 根据 @ComponentScan-配置组件扫描指令以与@Configuration类一起使用。提供与SpringXML的元素并行的支持。 必须指定BasePack

我正在尝试构建一个Spring RESTful Web服务。我最终没有这样的定义例外

谁能帮帮我吗? 提前谢谢

包裹

  • 实体-ch.example.Entities.core
  • repositories-ch.example.repositories
  • 服务-ch.example.service
档案库

例外情况

Spring上下文


根据
@ComponentScan
-配置组件扫描指令以与@Configuration类一起使用。提供与SpringXML的
元素并行的支持。 必须指定BasePackageClass()、basePackages()或其别名值()中的一个。

所以把它改成

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

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

springboot:Profile应用程序


就这些。它起作用了

对。但是当我把它改为@ComponentScan({…})时,它就不起作用了。因此,我将这个标记放在context.xml中,然后导入xml资源。然后它工作了您链接到的文档是针对较旧版本的Spring的。在Spring 3.2及更高版本中,
@ComponentScan
不要求指定任何包,“如果未定义特定包,则将从带有此注释的类的包中进行扫描。”Spring Boot使用Spring 4.0或4.1(取决于您使用的Boot版本)。您可能需要了解Boot的自动配置支持。在Spring Boot中,大部分XML配置是不必要的,可以用
src/main/resources/application.properties
中的几行代码替换。使用properties而不是context.XML有什么用?它的性能会更好/更快吗?它是关于减少您必须编写和维护的配置量。通过自己配置所有东西,您错过了Boot的主要优点之一。您应该能够用Boot的自动配置和application.properties中的几行代码替换所有XML来配置数据源
public interface ProfileService {
    List<AbstractProfile> findAll();
}
 @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();
    }
}
@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>
@ComponentScan({"ch.example.repositories","ch.example.service"})
@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:META-INF/mysql-spring-context.xml")
public class ProfileApp {

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