merge upstream

This commit is contained in:
DianeDuan
2016-11-27 12:40:32 +08:00
80 changed files with 1983 additions and 275 deletions

View File

@@ -1,2 +1,3 @@
### Relevant Articles:
- [Wiring in Spring: @Autowired, @Resource and @Inject](http://www.baeldung.com/spring-annotations-resource-inject-autowire)
- [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory)

View File

@@ -4,14 +4,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>dependency-injection</artifactId>
<artifactId>spring-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>dependency-injection</name>
<description>Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely
Resource, Inject, and Autowired
</description>
<name>spring-core</name>
<dependencies>
<dependency>

View File

@@ -0,0 +1,23 @@
package com.baeldung.constructordi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.baeldung.constructordi.domain.Engine;
import com.baeldung.constructordi.domain.Transmission;
@Configuration
@ComponentScan("com.baeldung.constructordi")
public class Config {
@Bean
public Engine engine() {
return new Engine("v8", 5);
}
@Bean
public Transmission transmission() {
return new Transmission("sliding");
}
}

View File

@@ -0,0 +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);
}
}

View File

@@ -0,0 +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);
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.constructordi.domain;
public class Engine {
private String type;
private int volume;
public Engine(String type, int volume) {
this.type = type;
this.volume = volume;
}
@Override
public String toString() {
return String.format("%s %d", type, volume);
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.constructordi.domain;
public class Transmission {
private String type;
public Transmission(String type) {
this.type = type;
}
@Override
public String toString() {
return String.format("%s", type);
}
}

View File

@@ -5,6 +5,7 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class FactoryBeanAppConfig {
@Bean
public ToolFactory toolFactory() {
ToolFactory factory = new ToolFactory();

View File

@@ -3,6 +3,7 @@ package com.baeldung.factorybean;
import org.springframework.beans.factory.config.AbstractFactoryBean;
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
private int factoryId;
private int toolId;

View File

@@ -4,6 +4,7 @@ import org.springframework.beans.factory.config.AbstractFactoryBean;
//no need to set singleton property because default value is true
public class SingleToolFactory extends AbstractFactoryBean<Tool> {
private int factoryId;
private int toolId;

View File

@@ -3,6 +3,7 @@ package com.baeldung.factorybean;
import org.springframework.beans.factory.FactoryBean;
public class ToolFactory implements FactoryBean<Tool> {
private int factoryId;
private int toolId;

View File

@@ -0,0 +1,20 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="toyota" class="com.baeldung.constructordi.domain.Car">
<constructor-arg index="0" ref="engine"/>
<constructor-arg index="1" ref="transmission"/>
</bean>
<bean id="engine" class="com.baeldung.constructordi.domain.Engine">
<constructor-arg index="0" value="v4"/>
<constructor-arg index="1" value="2"/>
</bean>
<bean id="transmission" class="com.baeldung.constructordi.domain.Transmission">
<constructor-arg value="sliding"/>
</bean>
</beans>

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="singleTool" class="com.baeldung.factorybean.SingleToolFactory">
<property name="factoryId" value="3001"/>

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="tool" class="com.baeldung.factorybean.ToolFactory">
<property name="factoryId" value="9090"/>