Master (#1250)
* Bean Injection Project is added Different Types of Bean Injection article codes are added. * Java-based configuration added Java-based configuration and tests are added. Coding styles are fixed. * List of Lists Article Codes added. List of Lists Article Codes added.
This commit is contained in:
parent
d4576d9762
commit
325466a782
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.list.listoflist;
|
||||
|
||||
public class Pen implements Stationery {
|
||||
|
||||
public String name;
|
||||
|
||||
public Pen(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.list.listoflist;
|
||||
|
||||
public class Pencil implements Stationery{
|
||||
|
||||
public String name;
|
||||
|
||||
public Pencil(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.list.listoflist;
|
||||
|
||||
public class Rubber implements Stationery {
|
||||
|
||||
public String name;
|
||||
|
||||
public Rubber(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.list.listoflist;
|
||||
|
||||
public interface Stationery {
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.list.listoflist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ListOfListsTest {
|
||||
|
||||
private List<ArrayList<? extends Stationery>> listOfLists = new ArrayList<ArrayList<? extends Stationery>>();
|
||||
private ArrayList<Pen> penList = new ArrayList<>();
|
||||
private ArrayList<Pencil> pencilList = new ArrayList<>();
|
||||
private ArrayList<Rubber> rubberList = new ArrayList<>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Before
|
||||
public void init() {
|
||||
listOfLists.add(penList);
|
||||
listOfLists.add(pencilList);
|
||||
listOfLists.add(rubberList);
|
||||
|
||||
((ArrayList<Pen>) listOfLists.get(0)).add(new Pen("Pen 1"));
|
||||
((ArrayList<Pencil>) listOfLists.get(1)).add(new Pencil("Pencil 1"));
|
||||
((ArrayList<Rubber>) listOfLists.get(2)).add(new Rubber("Rubber 1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfLists_thenCheckNames() {
|
||||
assertEquals("Pen 1", ((Pen) listOfLists.get(0)
|
||||
.get(0)).getName());
|
||||
assertEquals("Pencil 1", ((Pencil) listOfLists.get(1)
|
||||
.get(0)).getName());
|
||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(2)
|
||||
.get(0)).getName());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void givenListOfLists_whenRemovingElements_thenCheckNames() {
|
||||
((ArrayList<Pencil>) listOfLists.get(1)).remove(0);
|
||||
listOfLists.remove(1);
|
||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(1)
|
||||
.get(0)).getName());
|
||||
listOfLists.remove(0);
|
||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(0)
|
||||
.get(0)).getName());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package org.baeldung.bean.config;
|
||||
|
||||
import org.baeldung.bean.injection.Helm;
|
||||
import org.baeldung.bean.injection.Ship;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ConstructorBasedShipConfig {
|
||||
|
||||
@Bean
|
||||
public Ship ship() {
|
||||
return new Ship(helm());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Helm helm() {
|
||||
return new Helm();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package org.baeldung.bean.config;
|
||||
|
||||
import org.baeldung.bean.injection.Helm;
|
||||
import org.baeldung.bean.injection.Ship;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
public class SetterBasedShipConfig {
|
||||
|
||||
@Bean
|
||||
public Ship ship() {
|
||||
return new Ship();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Helm helm() {
|
||||
return new Helm();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.baeldung.bean.injection;
|
||||
|
||||
public class Helm {
|
||||
|
||||
private String brandOfHelm = "HelmBrand";
|
||||
|
||||
public String getBrandOfHelm() {
|
||||
return brandOfHelm;
|
||||
}
|
||||
|
||||
public void setBrandOfHelm(String brandOfHelm) {
|
||||
this.brandOfHelm = brandOfHelm;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package org.baeldung.bean.injection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class Ship {
|
||||
|
||||
@Autowired
|
||||
private Helm helm;
|
||||
|
||||
public Ship() {
|
||||
helm = new Helm();
|
||||
}
|
||||
|
||||
public Ship(Helm helm) {
|
||||
this.helm = helm;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setHelm(Helm helm) {
|
||||
this.helm = helm;
|
||||
}
|
||||
|
||||
public Helm getHelm() {
|
||||
return this.helm;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
|
||||
>
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
|
||||
|
||||
<context:property-placeholder location="classpath:bar.properties" order="2"/>
|
||||
<context:property-placeholder location="classpath:bar.properties"
|
||||
order="2" />
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.xsd">
|
||||
|
||||
<bean id="ship" class="org.baeldung.bean.injection.Ship">
|
||||
<constructor-arg>
|
||||
<ref bean="helm" />
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="helm" class="org.baeldung.bean.injection.Helm" />
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.xsd">
|
||||
|
||||
<bean id="ship" class="org.baeldung.bean.injection.Ship">
|
||||
<property name="helm">
|
||||
<ref bean="helm" />
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="helm" class="org.baeldung.bean.injection.Helm" />
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,23 @@
|
|||
package org.baeldung.bean.injection;
|
||||
|
||||
import org.baeldung.bean.config.ConstructorBasedShipConfig;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class ConstructorBasedBeanInjectionWithJavaConfigTest {
|
||||
private static final String HELM_NAME = "HelmBrand";
|
||||
|
||||
@Test
|
||||
public void givenJavaConfigFile_whenUsingConstructorBasedBeanInjection_thenCorrectHelmName() {
|
||||
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ConstructorBasedShipConfig.class);
|
||||
ctx.refresh();
|
||||
|
||||
Ship ship = ctx.getBean(Ship.class);
|
||||
|
||||
Assert.assertEquals(HELM_NAME, ship.getHelm()
|
||||
.getBrandOfHelm());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package org.baeldung.bean.injection;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class ConstructorBasedBeanInjectionWithXMLConfigTest {
|
||||
|
||||
private static final String HELM_NAME = "HelmBrand";
|
||||
|
||||
@Test
|
||||
public void givenXMLConfigFile_whenUsingConstructorBasedBeanInjection_thenCorrectHelmName() {
|
||||
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-constructor.xml");
|
||||
|
||||
final Ship shipConstructorBean = (Ship) applicationContext.getBean("ship");
|
||||
Assert.assertEquals(HELM_NAME, shipConstructorBean.getHelm()
|
||||
.getBrandOfHelm());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package org.baeldung.bean.injection;
|
||||
|
||||
import org.baeldung.bean.config.SetterBasedShipConfig;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class SetterBasedBeanInjectionWithJavaConfigTest {
|
||||
|
||||
private static final String HELM_NAME = "HelmBrand";
|
||||
|
||||
@Test
|
||||
public void givenJavaConfigFile_whenUsingSetterBasedBeanInjection_thenCorrectHelmName() {
|
||||
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(SetterBasedShipConfig.class);
|
||||
ctx.refresh();
|
||||
|
||||
Ship ship = ctx.getBean(Ship.class);
|
||||
|
||||
Assert.assertEquals(HELM_NAME, ship.getHelm()
|
||||
.getBrandOfHelm());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package org.baeldung.bean.injection;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class SetterBasedBeanInjectionWithXMLConfigTest {
|
||||
|
||||
private static final String HELM_NAME = "HelmBrand";
|
||||
|
||||
@Test
|
||||
public void givenXMLConfigFile_whenUsingSetterBasedBeanInjection_thenCorrectHelmName() {
|
||||
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-setter.xml");
|
||||
|
||||
final Ship shipSetterBean = (Ship) applicationContext.getBean("ship");
|
||||
Assert.assertEquals(HELM_NAME, shipSetterBean.getHelm()
|
||||
.getBrandOfHelm());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue