Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Autowired in spring

/* Can be done in 3 ways */

/* As class properties or Field Injection*/
class A {
	
    @Autowired
	private DIInjectable depInj;
    
    public doSomething(){
     	this.depInj.initiate();
      	// Rest of the code
      	// ...
    }
  
  	...
}

/* In constructor - Constructor Injection*/
class A {
  
  	private DIInjectable depInj;
  	
  	@Autowired
  	public A(DIInjectable depInj){
      this.depInj = depInj;
    }
  
  	public doSomething(){
      this.depInj.initiate();
      // Rest of the code
      // ...
    }
  
  	...
}
    
/* Setter Injection*/
class A {
  
  	private DIInjectable depInj;
  	
	...
    
    @Autowired
	public void setDiInjectable(DIInjectable depinj){
      this.depInj = depInj;
    }
  
  
  	public doSomething(){
      this.depInj.initiate();
      // Rest of the code
      // ...
    }
  
  	...
}
 
PREVIOUS NEXT
Tagged: #Autowired #spring
ADD COMMENT
Topic
Name
4+7 =