Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - autowire a class that have a constructor [duplicate]

Possible Duplicate:
Anyway to @Autowire a bean that requires constructor arguments?

In my controller I want to use @Autowired to inject a class using the method / constructor autowiring. for example using:

@Autowired 
private InjectedClass injectedClass; 

My problem is that the injected class injectedClass have a constructor, and I need to pass a variable to the constructor from the controller. How can I pass values to the constructors?

like image 528
webmobileDev Avatar asked Apr 25 '12 10:04

webmobileDev


People also ask

Is there a way to @autowire a bean that requires constructor arguments?

And this can be done either by using the @Autowired annotation or the @Value annotation. You use the @Autowired notation when the constructor argument is another Object, while the @Value annotation comes in handy when the contructor argument can easily be evaluated using Spring expression.

Can you Autowire byType when more than one bean with the same type exists True or false?

Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown.

Can we use @autowired on constructor?

You can apply @Autowired to constructors as well. A constructor @Autowired annotation indicates that the constructor should be autowired when creating the bean, even if no <constructor-arg> elements are used while configuring the bean in XML file.

Can you Autowire by type when more than one being with the same type exist?

Autowiring using property type. Allows a property to be autowired if exactly one bean of property type exists in the container. If more than one exists, it's a fatal exception is thrown, which indicates that you may not used byType autowiring for that bean.


2 Answers

If you are using annotations you can apply @Autowired annotation to MyClass's constructor, which will auto wire beans you are passing to MyClass's special constructor. Consider following e.g.

public class MovieRecommender {

  @Autowired
  private MovieCatalog movieCatalog;

  private CustomerPreferenceDao customerPreferenceDao;

  @Autowired
  public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
      this.customerPreferenceDao = customerPreferenceDao;
  }

  // ...
}
like image 161
Nandkumar Tekale Avatar answered Oct 05 '22 23:10

Nandkumar Tekale


You can either mark private data members with @Resource(name = "x") annotation OR wire them using constructor injection in the application context XML.

Annotations and XML configuration can be mixed in Spring. It need not be all or nothing.

<bean id="myClass" class="foo.bar.MyClass">
    <constructor-arg ref="yourArgRefHere"/> 
</bean>
like image 37
duffymo Avatar answered Oct 05 '22 22:10

duffymo