samedi 12 avril 2014

Java - @Autowire pas de paramètre DAO - Stack Overflow


I have the following Spring Java configuration set up:


@Configuration
public class FooConfig {
@Autowired
private IReferenceDataDAO referenceDataDAO;

@Bean
public IReferenceDataService getReferenceDataService() {
return new ReferenceDataServiceImpl(referenceDataDAO);
}
}

I am trying to reference the ReferenceDataService bean in another configuration class using the @Autowire property:


@Configuration
public class BarConfig {

@Autowired
private IRuleService ruleService;

@Autowired
private IReferenceDataService referenceDataService;
}

Here is the config that defines the IReferenceDataDAO:


@Configuration
public class FooBarConfig {

@Bean
public IReferenceDataDAO getReferenceDataDAO() {
return new ReferenceDataDAOImpl(getStaticData(), getMapper());
}
}

Here is the ReferenceDataServiceImpl:


public class ReferenceDataServiceImpl implements IReferenceDataService {

private IReferenceDataDAO dao;

public ReferenceDataServiceImpl(IReferenceDataDAO dao) {
this.dao = dao;
}

The above configs are imported in a master config class:


@Configuration
@Import({
FoosAppConfig.class,
BarAppConfig.class,
FooBarAppConfig.class,
})

I am noticing that this configuration leads to the referenceDAO in the ReferenceDataServiceImpl being set to null. What am I doing wrong? Shouldn't that @Autowire annotation ensure that my bean is fully configured before setting it?




BarConfig needs to import FooConfig @Import({ FooConfig.class })




Try this:


@Configuration
@Import({ FooConfig.class })
public class BarConfig {

@Autowired
private IRuleService ruleService;

@Autowired
private IReferenceDataService referenceDataService;
}

This allows BarConfig to get access to the IReferenceDataService bean.



I have the following Spring Java configuration set up:


@Configuration
public class FooConfig {
@Autowired
private IReferenceDataDAO referenceDataDAO;

@Bean
public IReferenceDataService getReferenceDataService() {
return new ReferenceDataServiceImpl(referenceDataDAO);
}
}

I am trying to reference the ReferenceDataService bean in another configuration class using the @Autowire property:


@Configuration
public class BarConfig {

@Autowired
private IRuleService ruleService;

@Autowired
private IReferenceDataService referenceDataService;
}

Here is the config that defines the IReferenceDataDAO:


@Configuration
public class FooBarConfig {

@Bean
public IReferenceDataDAO getReferenceDataDAO() {
return new ReferenceDataDAOImpl(getStaticData(), getMapper());
}
}

Here is the ReferenceDataServiceImpl:


public class ReferenceDataServiceImpl implements IReferenceDataService {

private IReferenceDataDAO dao;

public ReferenceDataServiceImpl(IReferenceDataDAO dao) {
this.dao = dao;
}

The above configs are imported in a master config class:


@Configuration
@Import({
FoosAppConfig.class,
BarAppConfig.class,
FooBarAppConfig.class,
})

I am noticing that this configuration leads to the referenceDAO in the ReferenceDataServiceImpl being set to null. What am I doing wrong? Shouldn't that @Autowire annotation ensure that my bean is fully configured before setting it?



BarConfig needs to import FooConfig @Import({ FooConfig.class })



Try this:


@Configuration
@Import({ FooConfig.class })
public class BarConfig {

@Autowired
private IRuleService ruleService;

@Autowired
private IReferenceDataService referenceDataService;
}

This allows BarConfig to get access to the IReferenceDataService bean.


0 commentaires:

Enregistrer un commentaire