File tree Expand file tree Collapse file tree
java/com/baeldung/constructordi Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .constructordi ;
2+
3+ import org .springframework .context .annotation .Bean ;
4+ import org .springframework .context .annotation .ComponentScan ;
5+ import org .springframework .context .annotation .Configuration ;
6+
7+ import com .baeldung .constructordi .domain .Engine ;
8+ import com .baeldung .constructordi .domain .Transmission ;
9+
10+ @ Configuration
11+ @ ComponentScan ("com.baeldung.constructordi" )
12+ public class Config {
13+
14+ @ Bean
15+ public Engine engine () {
16+ return new Engine ("v8" , 5 );
17+ }
18+
19+ @ Bean
20+ public Transmission transmission () {
21+ return new Transmission ("sliding" );
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .constructordi ;
2+
3+ import org .springframework .context .ApplicationContext ;
4+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
5+ import org .springframework .context .support .ClassPathXmlApplicationContext ;
6+
7+ import com .baeldung .constructordi .domain .Car ;
8+
9+ public class SpringRunner {
10+ public static void main (String [] args ) {
11+ Car toyota = getCarFromXml ();
12+
13+ System .out .println (toyota );
14+
15+ toyota = getCarFromJavaConfig ();
16+
17+ System .out .println (toyota );
18+ }
19+
20+ private static Car getCarFromJavaConfig () {
21+ ApplicationContext context = new AnnotationConfigApplicationContext (Config .class );
22+
23+ return context .getBean (Car .class );
24+ }
25+
26+ private static Car getCarFromXml () {
27+ ApplicationContext context = new ClassPathXmlApplicationContext ("baeldung.xml" );
28+
29+ return context .getBean (Car .class );
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .constructordi .domain ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Component
7+ public class Car {
8+ private Engine engine ;
9+ private Transmission transmission ;
10+
11+ @ Autowired
12+ public Car (Engine engine , Transmission transmission ) {
13+ this .engine = engine ;
14+ this .transmission = transmission ;
15+ }
16+
17+ @ Override
18+ public String toString () {
19+ return String .format ("Engine: %s Transmission: %s" , engine , transmission );
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .constructordi .domain ;
2+
3+ public class Engine {
4+ private String type ;
5+ private int volume ;
6+
7+ public Engine (String type , int volume ) {
8+ this .type = type ;
9+ this .volume = volume ;
10+ }
11+
12+ @ Override
13+ public String toString () {
14+ return String .format ("%s %d" , type , volume );
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .constructordi .domain ;
2+
3+ public class Transmission {
4+ private String type ;
5+
6+ public Transmission (String type ) {
7+ this .type = type ;
8+ }
9+
10+ @ Override
11+ public String toString () {
12+ return String .format ("%s" , type );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ <beans xmlns =" http://www.springframework.org/schema/beans"
2+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3+ xsi : schemaLocation =" http://www.springframework.org/schema/beans
4+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >
5+
6+ <bean id =" toyota" class =" com.baeldung.constructordi.domain.Car" >
7+ <constructor-arg index =" 0" ref =" engine" />
8+ <constructor-arg index =" 1" ref =" transmission" />
9+ </bean >
10+
11+ <bean id =" engine" class =" com.baeldung.constructordi.domain.Engine" >
12+ <constructor-arg index =" 0" value =" v4" />
13+ <constructor-arg index =" 1" value =" 2" />
14+ </bean >
15+
16+ <bean id =" transmission" class =" com.baeldung.constructordi.domain.Transmission" >
17+ <constructor-arg value =" sliding" />
18+ </bean >
19+
20+ </beans >
You can’t perform that action at this time.
0 commit comments