Code for Dependency Injection Article.
This commit is contained in:
parent
d32d3edcc9
commit
9e00c1cdff
@ -1,31 +1,31 @@
|
|||||||
package com.baeldung.constructordi;
|
package com.baeldung.constructordi;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
import com.baeldung.constructordi.domain.Car;
|
import com.baeldung.constructordi.domain.Car;
|
||||||
|
|
||||||
public class SpringRunner {
|
public class SpringRunner {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Car toyota = getCarFromXml();
|
Car toyota = getCarFromXml();
|
||||||
|
|
||||||
System.out.println(toyota);
|
System.out.println(toyota);
|
||||||
|
|
||||||
toyota = getCarFromJavaConfig();
|
toyota = getCarFromJavaConfig();
|
||||||
|
|
||||||
System.out.println(toyota);
|
System.out.println(toyota);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Car getCarFromJavaConfig() {
|
private static Car getCarFromJavaConfig() {
|
||||||
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
||||||
|
|
||||||
return context.getBean(Car.class);
|
return context.getBean(Car.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Car getCarFromXml() {
|
private static Car getCarFromXml() {
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("baeldung.xml");
|
ApplicationContext context = new ClassPathXmlApplicationContext("constructordi.xml");
|
||||||
|
|
||||||
return context.getBean(Car.class);
|
return context.getBean(Car.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
package com.baeldung.constructordi.domain;
|
package com.baeldung.constructordi.domain;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class Car {
|
public class Car {
|
||||||
private Engine engine;
|
private Engine engine;
|
||||||
private Transmission transmission;
|
private Transmission transmission;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public Car(Engine engine, Transmission transmission) {
|
public Car(Engine engine, Transmission transmission) {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
this.transmission = transmission;
|
this.transmission = transmission;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("Engine: %s Transmission: %s", engine, transmission);
|
return String.format("Engine: %s Transmission: %s", engine, transmission);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user