Skip to content

Commit 4b74772

Browse files
Different Types of Bean Injection in Spring code
1 parent b840f15 commit 4b74772

7 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.ditypes;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@ComponentScan("com.baeldung.ditypes")
9+
public class ConfigurationContext {
10+
11+
@Bean
12+
public Host host(){
13+
return new Host("Jovem Nerd");
14+
}
15+
16+
@Bean
17+
public Guest guest(){
18+
return new Guest("Azaghal");
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.ditypes;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Guest {
7+
8+
private String name;
9+
10+
public Guest(final String name){
11+
this.name = name;
12+
}
13+
14+
// standard setters and getters
15+
16+
@Override
17+
public String toString() {
18+
return String.format("Guest: %s", this.name);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.ditypes;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Host {
7+
8+
private String name;
9+
10+
public Host(final String name){
11+
this.name = name;
12+
}
13+
14+
// standard setters and getters
15+
16+
@Override
17+
public String toString() {
18+
return String.format("Host: %s", this.name);
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.ditypes;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
6+
import com.baeldung.ditypes.constructor.PodcastConstructor;
7+
import com.baeldung.ditypes.inner.PodcastField;
8+
import com.baeldung.ditypes.setter.PodcastSetter;
9+
10+
public class Runner {
11+
12+
public static void main(String[] args) {
13+
ApplicationContext context = new AnnotationConfigApplicationContext(ConfigurationContext.class);
14+
15+
PodcastConstructor podcastConstructor = context.getBean(PodcastConstructor.class);
16+
podcastConstructor.info();
17+
18+
PodcastField podcastField = context.getBean(PodcastField.class);
19+
podcastField.info();
20+
21+
PodcastSetter podcastSetter = context.getBean(PodcastSetter.class);
22+
podcastSetter.info();
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.ditypes.constructor;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import com.baeldung.ditypes.Guest;
7+
import com.baeldung.ditypes.Host;
8+
9+
@Component
10+
public class PodcastConstructor {
11+
12+
private String name;
13+
private Host host;
14+
private Guest guest;
15+
16+
@Autowired
17+
public PodcastConstructor(final Host host, final Guest guest){
18+
this.name = "Constructor Injection";
19+
this.host = host;
20+
this.guest = guest;
21+
}
22+
23+
// standard setters and getters
24+
25+
public void info(){
26+
System.out.println(String.format("Name: %s, %s, %s", this.name, this.host, this.guest));
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.ditypes.inner;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import com.baeldung.ditypes.Guest;
7+
import com.baeldung.ditypes.Host;
8+
9+
@Component
10+
public class PodcastField {
11+
12+
private String name = "Field Injection";
13+
14+
@Autowired
15+
private Host host;
16+
17+
@Autowired
18+
private Guest guest;
19+
20+
public void info(){
21+
System.out.println(String.format("Name: %s, %s, %s", this.name, this.host, this.guest));
22+
}
23+
24+
// standard setters and getters
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.ditypes.setter;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import com.baeldung.ditypes.Guest;
7+
import com.baeldung.ditypes.Host;
8+
9+
@Component
10+
public class PodcastSetter {
11+
12+
private String name = "Setter Injection";
13+
private Host host;
14+
private Guest guest;
15+
16+
public void info(){
17+
System.out.println(String.format("Name: %s, %s, %s", this.name, this.host, this.guest));
18+
}
19+
20+
@Autowired
21+
public void setHost(Host host) {
22+
this.host = host;
23+
}
24+
25+
@Autowired
26+
public void setGuest(Guest guest) {
27+
this.guest = guest;
28+
}
29+
30+
// standard setters and getters
31+
}

0 commit comments

Comments
 (0)