【问题标题】:Dependency injection fails with @SpringBootTest@SpringBootTest 依赖注入失败
【发布时间】:2017-05-18 18:11:29
【问题描述】:

我正在使用 Spring Boot 1.5.3.RELEASE 构建一个新的 MVC 应用程序。

我有如下客户实体:

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "name", length = 100, nullable = false)
    private String name;

    @Column(name = "enabled", nullable = false)
    private boolean enabled;
}

我有一个 CustomerRepository 如下:

public interface CustomerRepository extends CrudRepository<Customer, Integer> {

}

我有如下 CustomerService:

@Service
public class CustomerService {

    @Autowired
    CustomerRepository customerRepository;

    @Autowired
    CustomerMapper customerMapper;

    public CustomerDto save(CustomerDto customerDto) {

        Customer customer = customerMapper.map(customerDto, Customer.class);

        customerRepository.save(customer);

        return customerMapper.map(customer, CustomerDto.class);
    }

    public CustomerDto findById(int id) {

        Customer customer = customerRepository.findOne(id);

        return customerMapper.map(customer, CustomerDto.class);
    }
}

我的应用程序定义为:

@SpringBootApplication
public class CoreApplication {

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

我的 JPA 配置是:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
public class PersistenceConfig {

}

我写了一个测试如下:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTests {

    @Autowired
    CustomerService customerService;

    @Test
    public void testCustomerService() {

        CustomerDto customerDtoIn = new CustomerDto();

        customerDtoIn.setName("Test Customer");
        customerDtoIn.setEnabled(true);

        customerService.save(customerDtoIn);

        CustomerDto customerDtoOut = customerService.findById(customerDtoIn.getId());

        assertThat(customerDtoOut).isEqualTo(customerDtoIn);
    }
}

我遵循的结构是:

com.app.core <- CoreApplication lives here
com.app.core.repositories <- Repositories here
com.app.core.services <- Services here

但是,当我尝试运行此测试用例时,我得到以下信息:

org.springframework.beans.factory.NoSuchBeanDefinitionException
No qualifying bean of type 'com.app.core.repositories.CustomerRepository' available

谁能告诉我依赖注入失败的原因?通过阅读 spring-boot 文档 @SpringBootTest@RunWith(SpringRunner.class) 来测试我的服务层。

【问题讨论】:

  • 你的主班是什么样子的?您遵循的目录结构是什么?你能运行你的应用程序吗
  • 我已经用更多细节更新了这个问题。我还没有创建控制器层,所以没有要运行的应用程序。如果我跳过测试,我可以很好地构建应用程序。
  • 您不需要任何控制器来运行您的应用程序。只需尝试启动您的应用程序,看看它是否没有任何异常
  • 对不起,我误解了你的问题。我已经尝试运行该应用程序,但它失败并出现同样的异常。这一定意味着问题出在基本配置上,但我无法确定问题出在哪里。
  • 好的。所以这意味着这不是您的测试的问题。它是您的应用程序本身的问题。它没有扫描您的存储库。你的PersistenceConfig 怎么样?

标签: java spring spring-boot


【解决方案1】:

您必须将基础包和存储库基础包添加到ApplicationContext.xml。你可以使用下面的代码:-

<context:component-scan annotation-config="true"
    base-package="com.demo.test" />

<jpa:repositories base-package="com.demo.test.repository" />

【讨论】:

  • 我更新了我的问题并提供了更多细节。我没有使用基于 xml 的配置,我相信 @SpringBootApplication 注释就是我所需要的。
猜你喜欢
  • 2017-09-16
  • 2012-08-13
  • 2017-09-24
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多