File tree Expand file tree Collapse file tree 7 files changed +162
-0
lines changed
java/com/ripjava/constructordi Expand file tree Collapse file tree 7 files changed +162
-0
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.ripjava</groupId >
8+ <artifactId >spring-core</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+
11+ <dependencies >
12+
13+ <dependency >
14+ <groupId >org.springframework</groupId >
15+ <artifactId >spring-context</artifactId >
16+ <version >5.1.9.RELEASE</version >
17+ </dependency >
18+
19+ </dependencies >
20+
21+ <build >
22+ <plugins >
23+ <plugin >
24+ <groupId >org.apache.maven.plugins</groupId >
25+ <artifactId >maven-compiler-plugin</artifactId >
26+ <version >3.8.1</version >
27+ <configuration >
28+ <source >1.8</source >
29+ <target >1.8</target >
30+ </configuration >
31+ </plugin >
32+ </plugins >
33+ </build >
34+
35+ </project >
Original file line number Diff line number Diff line change 1+ package com .ripjava .constructordi ;
2+
3+ import com .ripjava .constructordi .domain .Engine ;
4+ import com .ripjava .constructordi .domain .Transmission ;
5+ import org .springframework .context .annotation .Bean ;
6+ import org .springframework .context .annotation .ComponentScan ;
7+ import org .springframework .context .annotation .Configuration ;
8+
9+ @ Configuration
10+ @ ComponentScan ("com.ripjava.constructordi" )
11+ public class Config {
12+
13+ @ Bean
14+ public Engine engine () {
15+ return new Engine ("v8" , 5 );
16+ }
17+
18+ @ Bean
19+ public Transmission transmission () {
20+ return new Transmission ("sliding" );
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .ripjava .constructordi ;
2+
3+ import com .ripjava .constructordi .domain .Car ;
4+ import org .springframework .context .ApplicationContext ;
5+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
6+ import org .springframework .context .support .ClassPathXmlApplicationContext ;
7+
8+ public class SpringDIRunner {
9+ public static void main (String [] args ) {
10+ Car toyota = getCarFromXml ();
11+
12+ System .out .println (toyota );
13+
14+ toyota = getCarFromJavaConfig ();
15+
16+ System .out .println (toyota );
17+ }
18+
19+ private static Car getCarFromJavaConfig () {
20+ ApplicationContext context = new AnnotationConfigApplicationContext (Config .class );
21+
22+ return context .getBean (Car .class );
23+ }
24+
25+ private static Car getCarFromXml () {
26+ ApplicationContext context = new ClassPathXmlApplicationContext ("constructordi.xml" );
27+
28+ return context .getBean (Car .class );
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package com .ripjava .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+
9+ private Engine engine ;
10+ private Transmission transmission ;
11+
12+ @ Autowired
13+ public Car (Engine engine , Transmission transmission ) {
14+ this .engine = engine ;
15+ this .transmission = transmission ;
16+ }
17+
18+ @ Override
19+ public String toString () {
20+ return String .format ("Engine: %s Transmission: %s" , engine , transmission );
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .ripjava .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 .ripjava .constructordi .domain ;
2+
3+
4+ public class Transmission {
5+ private String type ;
6+
7+ public Transmission (String type ) {
8+ this .type = type ;
9+ }
10+
11+ @ Override
12+ public String toString () {
13+ return String .format ("%s" , type );
14+ }
15+ }
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+ https://www.springframework.org/schema/beans/spring-beans.xsd" >
5+
6+ <bean id =" toyota" class =" com.ripjava.constructordi.domain.Car" >
7+ <constructor-arg index =" 0" ref =" engine" />
8+ <constructor-arg index =" 1" ref =" transmission" />
9+ </bean >
10+
11+ <bean id =" engine"
12+ class =" com.ripjava.constructordi.domain.Engine" >
13+ <constructor-arg index =" 0" value =" v4" />
14+ <constructor-arg index =" 1" value =" 2" />
15+ </bean >
16+
17+ <bean id =" transmission"
18+ class =" com.ripjava.constructordi.domain.Transmission" >
19+ <constructor-arg value =" sliding" />
20+ </bean >
21+
22+ </beans >
You can’t perform that action at this time.
0 commit comments