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