@Autowired并没有在我的托管bean中注入我的服务

时间:2014-03-28 19:35:56

标签: java spring hibernate

我正在尝试执行以下测试:

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes = DBDataManipulatorServiceTestContext.class, loader =  
  SpringApplicationContextLoader.class)
 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
 public class UserInterfaceBeanTest {

 @Autowired
private DBDataManipulatorService dbDataManipulatorService;
 .....

 @Test
public void testGetDistinctWorkPackageId() {

    WorkPackage workPackage1 = new WorkPackage();
    workPackage1.setWorkPackageId("TEST1");
    workPackage1.setWorkPackageName("WP_1");

    WorkPackage workPackage2 = new WorkPackage();
    workPackage2.setWorkPackageId("TEST2");
    workPackage2.setWorkPackageName("WP_2");

    WorkPackage workPackage3 = new WorkPackage();
    workPackage3.setWorkPackageId("TEST3");
    workPackage3.setWorkPackageName("WP_3");

    WorkPackage workPackage4 = new WorkPackage();
    workPackage4.setWorkPackageId("TEST4");
    workPackage4.setWorkPackageName("WP_4");

    Aircraft aircraft1 = new Aircraft();
    aircraft1.setAircraftId(50001);
    aircraft1.setModel("100");

    Aircraft aircraft2 = new Aircraft();
    aircraft2.setAircraftId(50002);
    aircraft2.setModel("100");

    aircraft1.addOrUpdateWorkPackageToAircraft(workPackage1);
    aircraft1.addOrUpdateWorkPackageToAircraft(workPackage3);

    aircraft2.addOrUpdateWorkPackageToAircraft(workPackage2);
    aircraft2.addOrUpdateWorkPackageToAircraft(workPackage3);

    Map<Integer, Aircraft> aircraftsMap = new HashMap<Integer, Aircraft>();
    aircraftsMap.put(50001, aircraft1);
    aircraftsMap.put(50002, aircraft2);


    UserInterfaceBean userInterfaceBean = new UserInterfaceBean();
    userInterfaceBean.setModel("100");

    aircraftsMap = dbDataManipulatorService.saveToDataBase(aircraftsMap);

    List<String> workPackgesIds = userInterfaceBean.getDistinctWorkPackageId();

    assertTrue(workPackgesIds.contains("TEST1"));
    assertTrue(workPackgesIds.contains("TEST2"));
    assertTrue(workPackgesIds.contains("TEST3"));
    assertTrue(!workPackgesIds.contains("TEST4"));
}

但是由于未正确注入的 dbDataManipulatorService ,我得到 NullPointerException ,我的serviceContext如下:

@ComponentScan
@EnableAutoConfiguration
@EnableBatchProcessing
@EnableJpaRepositories
public class DBDataManipulatorServiceContext {

.....

@Bean
public DataSource dataSource() {
    return new EmbeddedDatabaseBuilder().setName("aircraft").setType(H2)
            .build();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
    DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean lef = new  
            LocalContainerEntityManagerFactoryBean();
    lef.setDataSource(dataSource);
    lef.setJpaVendorAdapter(jpaVendorAdapter);
    lef.setPackagesToScan("com");
    return lef;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = 
                                  new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setShowSql(true);
    hibernateJpaVendorAdapter.setGenerateDdl(true);
    hibernateJpaVendorAdapter.setDatabase(Database.H2);
    return hibernateJpaVendorAdapter;
}

..... 

谁能告诉我问题出在哪里?以及如何解决它。

修改

这是我的DBDataManipulatorServiceImpl:

@Service
@Transactional
public class DBDataManipulatorServiceImpl implements DBDataManipulatorService {

.....

}

这是它的界面:

 public interface DBDataManipulatorService {


public AircraftRepository getAircraftRepository();

public Aircraft getAircraftById(String id);

public Map<Integer, Aircraft> saveToDataBase(Map<Integer, Aircraft> aircrafts);

public MyJob findJobByJobId(String jobId);

public WorkPackage findWorkPackageByAircraftIdAndWorkPackageId
                        (int aircraftId, String workPackageId);

public Aircraft findAircraftByaircraftId(int aircraftId);

public List<Aircraft> findAllAircrafts();

public List<String> findAllAvailableAircraftsModels();

public List<Aircraft> findAllAircraftsByModel(String model);

public List<WorkPackage> getWorkPackagesByAircraftModel(String model);

}

3 个答案:

答案 0 :(得分:1)

您有一个@ComponentScan,但您尚未输入要扫描的软件包的名称。默认行为是从您将@ComponentScan基本上放在DBDataManipulatorServiceContext配置类的包中的包中进行扫描。我猜这不是您DBDataManipulatorService所在的位置,如果是,请为您的@ComponentScan

指定一个包名称

答案 1 :(得分:0)

默认情况下,@Autowired注释意味着必须找到一个bean,否则你将找到一个未找到bean的异常。

有一些事情可能导致这种情况:

  • 单元测试通过new DBDataManipulatorServiceContext()实例化,而不是作为Spring Bean扫描,在这种情况下@Autowired被忽略。

  • 有一些XML配置会覆盖注释配置,因此首先使用autowiring on创建bean,然后由XML中定义的bean丢弃,不为缺少的属性提供参数

答案 2 :(得分:0)

您需要以某种方式标记DBDataManipulatorService类。在类名上使用@service或@component注释。这将告诉spring管理类,从而将其注入测试中。