Code and related files for the dependency injection demos.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.autowired;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Type.xml"})
|
||||
public class FieldAutowiredDemo {
|
||||
|
||||
@Autowired
|
||||
private ArbitraryDependency fieldDependency;
|
||||
|
||||
@Test
|
||||
public void fieldDependency_MUST_BE_AUTOWIRED_Correctly() {
|
||||
assertNotNull(fieldDependency);
|
||||
assertEquals("Arbitrary Dependency", fieldDependency.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.autowired;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Name.xml"})
|
||||
public class FieldAutowiredNameDemo {
|
||||
|
||||
@Autowired
|
||||
private ArbitraryDependency autowiredFieldDependency;
|
||||
|
||||
@Test
|
||||
public void autowiredFieldDependency_MUST_BE_AUTOWIRED_Correctly() {
|
||||
assertNotNull(autowiredFieldDependency);
|
||||
assertEquals("Arbitrary Dependency", autowiredFieldDependency.toString());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.autowired;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Qualifier.xml"})
|
||||
public class FieldQualifierAutowiredDemo {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("autowiredFieldDependency")
|
||||
private ArbitraryDependency fieldDependency1;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("anotherAutowiredFieldDependency")
|
||||
private ArbitraryDependency fieldDependency2;
|
||||
|
||||
@Test
|
||||
public void fieldDependency1_MUST_BE_AUTOWIRED_Correctly() {
|
||||
assertNotNull(fieldDependency1);
|
||||
assertEquals("Arbitrary Dependency", fieldDependency1.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldDependency2_MUST_BE_AUTOWIRED_Correctly() {
|
||||
assertNotNull(fieldDependency2);
|
||||
assertEquals("Another Arbitrary Dependency", fieldDependency2.toString());
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
public class AnotherArbitraryDependency extends ArbitraryDependency {
|
||||
|
||||
private final String label = "Another Arbitrary Dependency";
|
||||
|
||||
public String toString() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
public class ArbitraryDependency {
|
||||
|
||||
private final String label = "Arbitrary Dependency";
|
||||
|
||||
public String toString() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
public class YetAnotherArbitraryDependency extends ArbitraryDependency {
|
||||
|
||||
private final String label = "Yet Another Arbitrary Dependency";
|
||||
|
||||
public String toString() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.inject;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Name.xml"})
|
||||
public class FieldByNameInjectDemo {
|
||||
|
||||
@Inject
|
||||
@Named("yetAnotherFieldInjectDependency")
|
||||
private ArbitraryDependency yetAnotherFieldInjectDependency;
|
||||
|
||||
@Test
|
||||
public void yetAnotherFieldInjectDependency_MUST_BE_INJECTED_Correctly() {
|
||||
assertNotNull(yetAnotherFieldInjectDependency);
|
||||
assertEquals("Yet Another Arbitrary Dependency", yetAnotherFieldInjectDependency.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.inject;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Type.xml"})
|
||||
public class FieldInjectDemo {
|
||||
|
||||
@Inject
|
||||
private ArbitraryDependency inject1Dependency;
|
||||
|
||||
@Test
|
||||
public void fieldDependency_MUST_BE_INJECTED_Successfully() {
|
||||
assertNotNull(inject1Dependency);
|
||||
assertEquals("Arbitrary Dependency", inject1Dependency.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.inject;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Qualifier.xml"})
|
||||
public class FieldQualifierInjectDemo {
|
||||
|
||||
@Inject
|
||||
@Qualifier("defaultFile")
|
||||
private ArbitraryDependency defaultDependency;
|
||||
|
||||
@Inject
|
||||
@Qualifier("namedFile")
|
||||
private ArbitraryDependency namedDependency;
|
||||
|
||||
@Test
|
||||
public void defaultDependency_MUST_BE_INJECTED_Successfully() {
|
||||
assertNotNull(defaultDependency);
|
||||
assertEquals("Arbitrary Dependency", defaultDependency.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedDependency_MUST_BE_INJECTED_Correctly() {
|
||||
assertNotNull(defaultDependency);
|
||||
assertEquals("Another Arbitrary Dependency", namedDependency.toString());
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class FieldResourceInjectionDemo {
|
||||
|
||||
@Resource(name="namedFile")
|
||||
private File defaultFile;
|
||||
|
||||
@Test
|
||||
public void plainResourceAnnotation_MUST_FIND_DefaultFile() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-Qualifier.xml"})
|
||||
public class MethodByQualifierResourceDemo {
|
||||
|
||||
private File arbDependency;
|
||||
private File anotherArbDependency;
|
||||
|
||||
@Test
|
||||
public void dependencies_MUST_BE_INJECTED_Correctly() {
|
||||
assertNotNull(arbDependency);
|
||||
assertEquals("namedFile.txt", arbDependency.getName());
|
||||
assertNotNull(anotherArbDependency);
|
||||
assertEquals("defaultFile.txt", anotherArbDependency.getName());
|
||||
}
|
||||
|
||||
@Resource
|
||||
@Qualifier("namedFile")
|
||||
public void setArbDependency(File arbDependency) {
|
||||
this.arbDependency = arbDependency;
|
||||
}
|
||||
|
||||
@Resource
|
||||
@Qualifier("defaultFile")
|
||||
public void setAnotherArbDependency(File anotherArbDependency) {
|
||||
this.anotherArbDependency = anotherArbDependency;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class MethodByTypeResourceDemo {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
@Resource
|
||||
protected void setDefaultFile(File defaultFile) {
|
||||
this.defaultFile = defaultFile;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_INJECTED_Correctly() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class MethodResourceInjectionDemo {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
@Resource(name="namedFile")
|
||||
protected void setDefaultFile(File defaultFile) {
|
||||
this.defaultFile = defaultFile;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_INJECTED_Correctly() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class NamedResourceTest {
|
||||
|
||||
@Resource(name="namedFile")
|
||||
private File testFile;
|
||||
|
||||
@Test
|
||||
public void namedResource_MUST_FIND_SPECIFIED_File() {
|
||||
assertNotNull(testFile);
|
||||
assertTrue(testFile.getName().equals("namedFile.txt"));
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-Qualifier.xml"})
|
||||
public class QualifierResourceInjectionDemo {
|
||||
|
||||
@Resource
|
||||
private File defaultFile;
|
||||
|
||||
@Resource
|
||||
@Qualifier("namedFile")
|
||||
private File namedFile;
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_Valid() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedFile_MUST_BE_Valid() {
|
||||
assertNotNull(namedFile);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class SetterResourceInjectionDemo {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
@Resource
|
||||
protected void setDefaultFile(File defaultFile) {
|
||||
this.defaultFile = defaultFile;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setter_MUST_INJECT_Resource() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user