evaluation article, different types of bean injection. (#3511)
* Different types of bean injection in spring, initial write-up * Evaluation article code example, different types of bean injection.
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
83d5f6a485
commit
54bd8a5cdd
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.dependencyinjectiontypes;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Student {
|
||||
|
||||
private TeacherFinder teacherFinder;
|
||||
|
||||
@Autowired
|
||||
public Student(TeacherFinder teacherFinder) {
|
||||
this.teacherFinder = teacherFinder;
|
||||
}
|
||||
|
||||
public String getTeacher() {
|
||||
return teacherFinder.getTeacherFinder();
|
||||
}
|
||||
// business logic that actually uses the injected teacherFinders is omitted...
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.dependencyinjectiontypes;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Student2 {
|
||||
|
||||
private TeacherFinder teacherFinder;
|
||||
|
||||
@Autowired
|
||||
public void setTeacherFinder(TeacherFinder teacherFinder) {
|
||||
this.teacherFinder = teacherFinder;
|
||||
}
|
||||
|
||||
public String getTeacher() {
|
||||
return teacherFinder.getTeacherFinder();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.dependencyinjectiontypes;
|
||||
public class TeacherFinder {
|
||||
|
||||
private String teacherFinder;
|
||||
|
||||
public String getTeacherFinder() {
|
||||
return teacherFinder;
|
||||
}
|
||||
|
||||
public void setTeacherFinder(String teacherFinder) {
|
||||
this.teacherFinder = teacherFinder;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,35 +1,58 @@
|
||||
package com.baeldung.setterdi;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.setterdi.domain.Engine;
|
||||
import com.baeldung.setterdi.domain.Trailer;
|
||||
import com.baeldung.setterdi.domain.Transmission;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.setterdi")
|
||||
public class Config {
|
||||
|
||||
@Bean
|
||||
public Engine engine() {
|
||||
Engine engine = new Engine();
|
||||
engine.setType("v8");
|
||||
engine.setVolume(5);
|
||||
return engine;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Transmission transmission() {
|
||||
Transmission transmission = new Transmission();
|
||||
transmission.setType("sliding");
|
||||
return transmission;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Trailer trailer() {
|
||||
Trailer trailer = new Trailer();
|
||||
return trailer;
|
||||
}
|
||||
package com.baeldung.setterdi;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.dependencyinjectiontypes.Student;
|
||||
import com.baeldung.dependencyinjectiontypes.Student2;
|
||||
import com.baeldung.dependencyinjectiontypes.TeacherFinder;
|
||||
import com.baeldung.setterdi.domain.Engine;
|
||||
import com.baeldung.setterdi.domain.Trailer;
|
||||
import com.baeldung.setterdi.domain.Transmission;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.setterdi")
|
||||
public class Config {
|
||||
|
||||
@Bean
|
||||
public Engine engine() {
|
||||
Engine engine = new Engine();
|
||||
engine.setType("v8");
|
||||
engine.setVolume(5);
|
||||
return engine;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Transmission transmission() {
|
||||
Transmission transmission = new Transmission();
|
||||
transmission.setType("sliding");
|
||||
return transmission;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Trailer trailer() {
|
||||
Trailer trailer = new Trailer();
|
||||
return trailer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TeacherFinder teacherFinder(){
|
||||
TeacherFinder teacherFinder =new TeacherFinder();
|
||||
teacherFinder.setTeacherFinder("author");
|
||||
return teacherFinder;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Student student() {
|
||||
return new Student(teacherFinder());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Student2 student2() {
|
||||
Student2 student2 = new Student2();
|
||||
student2.setTeacherFinder(teacherFinder());
|
||||
return student2;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user