Announcement Announcement Module
No announcement yet.
Mixing XML and java beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mixing XML and java beans


    I wanted to use existing xml beans with the newer (well, to our organization) @Configuration and @Bean annotations, eg

        TransferServiceConfig.class,//pretend like accountRepository is not here
    public class IntegrationTests {
    	private TransferService transferService;
    	private AccountRepository accountRepository;
    	public void transferTenDollars() throws InsufficientFundsException {
    //		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    //		ctx.getEnvironment().setDefaultProfiles("dev");
    //		ctx.register(TransferServiceConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
    //		ctx.refresh();
    //		TransferService transferService = ctx.getBean(TransferService.class);
    ... continue on with example
    Taken from the examples that introduce profiles and modified for a use case I'm considering. Now there are beans that I would not want to configure in java because that means a recompile to change (yes, I know, "profiles", just go with me here). There are also beans where it might be a good idea to construct them in java. Consider this, where stuff happens and sometimes there's a bean wiring problem:

    public class MyDAOConfig {
    	public MyDAO getMyDAO() {
    		try {
    			return new DefaultMyDAO(getMissingService(), getWorkingService());
    		catch (java.lang.NoClassDefFoundError e) {
    			logger.warn("Could not load some class for the MyDAO, probably DefaultMyDAO, but this is TEST, so mock it out and continue", e);
    			return new MockMyDAO(getMockService());
    		catch (org.springframework.beans.factory.NoSuchBeanDefinitionException e) {
    			logger.warn("Could not find some bean for the MyDAO, but this is TEST, so mock it out and continue", e);
    			return new MockMyDAO(getMockService());
    The basic idea is use the regular bean if possible, or mock it out if not possible. That way, we can continue the integration test and still be aware of this particular problem which may have nothing to do with what we are testing.

    But the problem is that of course we can't have 2 @ContextConfiguration nor can I have both locations and classes in one annotation, else it throws 2013-01-16 17:45:19,844 ERROR [org.springframework.test.context.ContextConfigurat ionAttributes] - <Test class [] has been configured with @ContextConfiguration's 'locations' (or 'value') {classpath:/com/bank/config/xml/transfer-service-config.xml} and 'classes' {class, class, class} attributes. Only one declaration of resources is permitted per @ContextConfiguration annotation.>

    Any ideas on how to do what I want, or should this be put on the feature wishlist?

  • #2
    Management Project

    whow de we find the web services?
    online project planning


    • #3
      I found what I wanted. In my beans .xml I use <context:annotation-config/> and then <bean class=""/>, couldn't be easier.