5

I am turning old xml/java configuration into pure java config. In xml I used injection of parameters into configuration file like this:

<bean class="com.project.SpringRestConfiguration">
    <property name="parameters" ref="parameters" />
</bean>



@Configuration
public class SpringRestConfiguration {

    private Parameters parameters;

    public void setParameters(Parameters parameters) {
        this.parameters = parameters;
    }

    // @Bean definitions
    ...
}

Is it possible to inject Parameters in javaconfig? (Without the need of using autowiring!)

@Configuration
@Import(SpringRestConfiguration.class)

EDIT: With @Import I can't see any chance to inject Parameters into SpringRestConfiguration

Ondřej Míchal
  • 573
  • 2
  • 5
  • 14
  • Why don't you want to use @Autowire? – Jokab Aug 03 '15 at 08:57
  • Because sometimes autowiring is a little bit confusing. E.g. when I have more objects of the same (super)type. In xml I was able to say exactly, which bean I want to inject. Or just the case, where you use 3rd party library/configuration and because of autowiring you do not have full control (happened to me with Spring boot) – Ondřej Míchal Aug 03 '15 at 09:41
  • Your question is not clear. Do you want to create `SpringRestConfiguration` object with `parameters`? – Karthik Aug 03 '15 at 09:41
  • I have updated my question – Ondřej Míchal Aug 03 '15 at 09:46
  • @xandre I did not understand completely but can you check my answer? – Karthik Aug 03 '15 at 09:50
  • 1
    @xandre in autowiring, just like via xml, you can refer to an implementing bean by its name. – yair Aug 03 '15 at 10:14
  • 1
    You can use @Autowired with names. Have a look at the answer to my question here: http://stackoverflow.com/q/24014919/2083523. I'm sure you'll find what you need there. – Avi Aug 03 '15 at 10:15

2 Answers2

1

Basically you would need to use @Autowired but you can still use a name and not type interpretation like this:

@Configuration
public class SpringRestConfiguration {

    @Autowired
    @Qualifier("parameters") // Somewhere in your context you should have a bean named 'parameters'. It doesn't matter if it was defined with XML, configuration class or with auto scanning. As long as such bean with the right type and name exists, you should be good.
    private Parameters parameters;

    // @Bean definitions
    ...
}

This solves the confusion problem you mentioned when using @Autowired - there's no question here which bean is injected, the bean that is named parameters.

You can even do a little test, leave the parameters bean defined in the XML as before, use @Autowired, see that it works. Only then migrate parameters to @Configuration class.

In my answer here you can find a complete explanation of how you should migrate XML to @Configuration step by step.

You can also skip the private member altogether and do something like this:

@Configuration
public class SpringRestConfiguration {

    @Bean
    public BeanThatNeedsParamters beanThatNeedsParamters (@Qualifier("parameters") Parameters parameters) {
       return new BeanThatNeedsParamters(parameters)
    }

}
Community
  • 1
  • 1
Avi
  • 21,182
  • 26
  • 82
  • 121
0

If I have understood your question properly, this is what you are trying to do :

@Component
public class SomeConfiguration {
   @Bean(name="parameters")
   public Parameters getParameters(){
      Parameters parameters = new Parameters();
      // add your stuff
      return parameters;
   }

   @Bean(name="springRestConfiguration")
   public SpringRestConfiguration springRestConfiguration(){
      SpringRestConfiguration springRestConfiguration = new SpringRestConfiguration();
      springRestConfiguration.setParametere(getParameters());
     return springRestConfiguration;
   }

}

and use it like :

ApplicationContext appContext = new AnnotationConfigApplicationContext(SomeConfiguration.class);
SpringRestConfiguration springRestConfiguration = (SpringRestConfiguration) appContext.getBean("springRestConfiguration");
Karthik
  • 4,950
  • 6
  • 35
  • 65
  • So you mean Configuration can be imported by @Bean and "new"? Now I have tried that, but it doesn't seem to be working. Beans defined inside of SpringRestConfiguration are not found in Spring context. – Ondřej Míchal Aug 03 '15 at 10:07
  • @xandre what do you mean by not found? Did you use `ApplicationContext appContext = new AnnotationConfigApplicationContext(SomeConfiguration.class);` ? – Karthik Aug 03 '15 at 10:10
  • I think you should annotate `SomeConfiguration` with `@Configuration` rather than `@Component` – yair Aug 03 '15 at 10:19
  • This way, I would create a new spring context, wouldn't I? But I already have one - web application context – Ondřej Míchal Aug 03 '15 at 10:21