Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "constructor based injection" and " autowire by constructor mode" in Spring

I know and understand constructor based injection. But, the autowiring modes confuse me. 1) I have read that default mode for autowiring is 'no autowiring' i.e. We have to manually set the properties in xml file. But, isn't xml based configuration a type of autowiring? How can it be considered 'No autowiring'? 2) Other modes for autowiring are i) byName ii) byType iii)constructor iv) auto-detect. Am i correct to assume the following:

a) When using xml configuration based autowiring, the default mode is 'byName'(i.e. I have to keep the name of property reference the same as the name of the bean which is being used as a property.)

b) When using Annotations, default mode is 'byType'( Regardless of the place the @Autowired keyword is placed i.e on the setter, on the constructor or on the property, it will search the type of the property being autowried)

3) What is the difference between constructor based injection and 'constructor' mode of autowiring?(I have read that constructor mode means it applies byType mode on all the constructor arguments, but how is it different from placing @Autowired keyword on the constructor)

4) I know that to enable autowired mode byName in annotations, in the bean definition in the xml file, I have to use " autowire = 'byName' ". But, suppose I am using Annotations only config( using @Component, and no bean definitions in the xml ), and I want to use byName autowire mode, then what is the way of doing that?

like image 787
T Anna Avatar asked Nov 24 '25 18:11

T Anna


1 Answers

I think you are a bit confused. First, you need to understand dependency injection (see here). There is ton of info about DI but in short, it means some third party (e.g spring IOC) passes the dependencies to the objects rather than the objects to create/obtain the references themselves. This could happen either through constructor or setter. For instance, consider constructor DI

class B{
}

class A{
  private B b;
  public A(B b){
    this.b = b;
  } 
} 

Some third party will inject an instance of class B into A rather class A create a reference to B itself. Very often you would use an interface so class A wouldn't even know what object will be injected into it.

Now in Spring there are different ways to configure these associations between the objects (the example above). You can either use XML, Java Config or Autowiring. They are independent but do the same stuff.

In both XML and JAVA config you need to configure the dependencies explicitly - either in xml file or having a @Configuration class for the JAVA Config and annotating the beans with @Bean. The autowiring is different. There you create simple POJOs which you annotate with @Component, @Controller, @Service or @Repository. They will be automatically registered as beans via component scanning. With autowiring you don't need to explicitly configure the dependencies in XML file or JAVA Config class. You can do it in code directly. For instance, if we have to compare java config vs autowiring using the previous example

Java Config (explicit config in a config class)

@Bean
public A getA(){
  return new A(new B());
}

Autowiring (implicit - done in code)

   @Component 
   class B{
   }

@Component
class A{
  private B b;

  @Autowired
  public A(B b){
    this.b = b;
  } 
} 

In the latter we autowire class B into class A (they both will be registered as beans due to @Component annotation) without having explicitly defined this association in an xml file or java config class. I hope it makes sense.

like image 125
george Avatar answered Nov 27 '25 10:11

george



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!