Skip to content

Commit ae47827

Browse files
author
alv21
committed
Different Types of Bean Injection in Spring
1 parent 0743dec commit ae47827

15 files changed

Lines changed: 253 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.beansinjectiontypes;
2+
3+
public class Cabriolet implements Car{
4+
5+
public void honk(){
6+
System.out.println("I'm a cabriolet!");
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.beansinjectiontypes;
2+
3+
public interface Car {
4+
5+
public void honk();
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.beansinjectiontypes;
2+
3+
public class DealershipTraditionalStyle {
4+
private Car car = new Sedan();
5+
6+
public Car getCar() {
7+
return car;
8+
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.beansinjectiontypes;
2+
3+
public class DealershipWithDependenciesInjection {
4+
private Car car;
5+
6+
public DealershipWithDependenciesInjection(Car car) {
7+
this.car = car;
8+
}
9+
10+
public Car getCar() {
11+
return car;
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.beansinjectiontypes;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Sedan implements Car {
7+
public void honk(){
8+
System.out.println("I'm a sedan!");
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.beansinjectiontypes.constructor;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import com.baeldung.beansinjectiontypes.Car;
7+
import com.baeldung.beansinjectiontypes.Sedan;
8+
9+
@Configuration(value="constructorConfig")
10+
public class Config {
11+
@Bean
12+
public Car car() {
13+
return new Sedan();
14+
}
15+
16+
@Bean
17+
public Dealership dealership() {
18+
return new Dealership(car());
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.beansinjectiontypes.constructor;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import com.baeldung.beansinjectiontypes.Car;
7+
8+
@Component(value="constructorDealership")
9+
public class Dealership {
10+
private Car car;
11+
12+
@Autowired
13+
public Dealership(Car car) {
14+
this.car = car;
15+
}
16+
17+
public Car getCar() {
18+
return car;
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.beansinjectiontypes.constructor;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
public class HornCheckerFile {
6+
7+
public static void main(String[] args) {
8+
try (AnnotationConfigApplicationContext context =
9+
new AnnotationConfigApplicationContext(Config.class)) {
10+
Dealership dealership = (Dealership) context.getBean("dealership");
11+
12+
dealership.getCar().honk();
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.beansinjectiontypes.constructor;
2+
3+
import org.springframework.context.support.FileSystemXmlApplicationContext;
4+
5+
public class HornCheckerXML {
6+
7+
public static void main(String[] args) {
8+
try (FileSystemXmlApplicationContext context =
9+
new FileSystemXmlApplicationContext("src/main/resources/beansinjectiontypes-ctx.xml")) {
10+
Dealership dealership = (Dealership) context.getBean("constructorDealership");
11+
12+
dealership.getCar().honk();
13+
}
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.beansinjectiontypes.setter;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import com.baeldung.beansinjectiontypes.Car;
7+
import com.baeldung.beansinjectiontypes.Sedan;
8+
9+
@Configuration(value="setterConfig")
10+
public class Config {
11+
@Bean
12+
public Car car() {
13+
return new Sedan();
14+
}
15+
16+
@Bean
17+
public Dealership dealership() {
18+
Dealership dealership = new Dealership();
19+
dealership.setCar(car());
20+
return dealership;
21+
}
22+
}

0 commit comments

Comments
 (0)