fixups after fork sync
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.beanfactory;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Employee(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.collection;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/22/2018.
|
||||
*/
|
||||
public class BaeldungBean {
|
||||
|
||||
private String name;
|
||||
|
||||
public BaeldungBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Configuration
|
||||
public class CollectionConfig {
|
||||
|
||||
@Bean
|
||||
public CollectionsBean getCollectionsBean() {
|
||||
return new CollectionsBean(new HashSet<>(Arrays.asList("John", "Adam", "Harry")));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public List<String> nameList(){
|
||||
return Arrays.asList("John", "Adam", "Harry", null);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Map<Integer, String> nameMap(){
|
||||
Map<Integer, String> nameMap = new HashMap<>();
|
||||
nameMap.put(1, "John");
|
||||
nameMap.put(2, "Adam");
|
||||
nameMap.put(3, "Harry");
|
||||
return nameMap;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Qualifier("CollectionsBean")
|
||||
@Order(2)
|
||||
public BaeldungBean getElement() {
|
||||
return new BaeldungBean("John");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(3)
|
||||
public BaeldungBean getAnotherElement() {
|
||||
return new BaeldungBean("Adam");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(1)
|
||||
public BaeldungBean getOneMoreElement() {
|
||||
return new BaeldungBean("Harry");
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.collection;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/18/2018.
|
||||
*/
|
||||
public class CollectionInjectionDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(CollectionConfig.class);
|
||||
CollectionsBean collectionsBean = context.getBean(CollectionsBean.class);
|
||||
collectionsBean.printNameList();
|
||||
collectionsBean.printNameSet();
|
||||
collectionsBean.printNameMap();
|
||||
collectionsBean.printBeanList();
|
||||
collectionsBean.printNameListWithDefaults();
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.baeldung.collection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/18/2018.
|
||||
*/
|
||||
public class CollectionsBean {
|
||||
|
||||
@Autowired
|
||||
private List<String> nameList;
|
||||
|
||||
private Set<String> nameSet;
|
||||
|
||||
private Map<Integer, String> nameMap;
|
||||
|
||||
@Autowired(required = false)
|
||||
@Qualifier("CollectionsBean")
|
||||
private List<BaeldungBean> beanList = new ArrayList<>();
|
||||
|
||||
@Value("${names.list:}#{T(java.util.Collections).emptyList()}")
|
||||
private List<String> nameListWithDefaultValue;
|
||||
|
||||
public CollectionsBean() {
|
||||
}
|
||||
|
||||
public CollectionsBean(Set<String> strings) {
|
||||
this.nameSet = strings;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setNameMap(Map<Integer, String> nameMap) {
|
||||
this.nameMap = nameMap;
|
||||
}
|
||||
|
||||
public void printNameList() {
|
||||
System.out.println(nameList);
|
||||
}
|
||||
|
||||
public void printNameSet() {
|
||||
System.out.println(nameSet);
|
||||
}
|
||||
|
||||
public void printNameMap() {
|
||||
System.out.println(nameMap);
|
||||
}
|
||||
|
||||
public void printBeanList() {
|
||||
System.out.println(beanList);
|
||||
}
|
||||
|
||||
public void printNameListWithDefaults() {
|
||||
System.out.println(nameListWithDefaultValue);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class FactoryBeanAppConfig {
|
||||
|
||||
@Bean(name = "tool")
|
||||
public ToolFactory toolFactory() {
|
||||
ToolFactory factory = new ToolFactory();
|
||||
factory.setFactoryId(7070);
|
||||
factory.setToolId(2);
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||
|
||||
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||
|
||||
private int factoryId;
|
||||
private int toolId;
|
||||
|
||||
public NonSingleToolFactory() {
|
||||
setSingleton(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return Tool.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Tool createInstance() throws Exception {
|
||||
return new Tool(toolId);
|
||||
}
|
||||
|
||||
public int getFactoryId() {
|
||||
return factoryId;
|
||||
}
|
||||
|
||||
public void setFactoryId(int factoryId) {
|
||||
this.factoryId = factoryId;
|
||||
}
|
||||
|
||||
public int getToolId() {
|
||||
return toolId;
|
||||
}
|
||||
|
||||
public void setToolId(int toolId) {
|
||||
this.toolId = toolId;
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
### Relevant Articles:
|
||||
- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean)
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
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;
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return Tool.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Tool createInstance() throws Exception {
|
||||
return new Tool(toolId);
|
||||
}
|
||||
|
||||
public int getFactoryId() {
|
||||
return factoryId;
|
||||
}
|
||||
|
||||
public void setFactoryId(int factoryId) {
|
||||
this.factoryId = factoryId;
|
||||
}
|
||||
|
||||
public int getToolId() {
|
||||
return toolId;
|
||||
}
|
||||
|
||||
public void setToolId(int toolId) {
|
||||
this.toolId = toolId;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
public class Tool {
|
||||
private int id;
|
||||
|
||||
public Tool() {
|
||||
}
|
||||
|
||||
public Tool(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
|
||||
public class ToolFactory implements FactoryBean<Tool> {
|
||||
|
||||
private int factoryId;
|
||||
private int toolId;
|
||||
|
||||
@Override
|
||||
public Tool getObject() throws Exception {
|
||||
return new Tool(toolId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return Tool.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getFactoryId() {
|
||||
return factoryId;
|
||||
}
|
||||
|
||||
public void setFactoryId(int factoryId) {
|
||||
this.factoryId = factoryId;
|
||||
}
|
||||
|
||||
public int getToolId() {
|
||||
return toolId;
|
||||
}
|
||||
|
||||
public void setToolId(int toolId) {
|
||||
this.toolId = toolId;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
<?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">
|
||||
|
||||
<bean id="singleTool" class="com.baeldung.factorybean.SingleToolFactory">
|
||||
<property name="factoryId" value="3001"/>
|
||||
<property name="toolId" value="1"/>
|
||||
</bean>
|
||||
|
||||
<bean id="nonSingleTool" class="com.baeldung.factorybean.NonSingleToolFactory">
|
||||
<property name="factoryId" value="3002"/>
|
||||
<property name="toolId" value="2"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,10 +0,0 @@
|
||||
<?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">
|
||||
|
||||
<bean id="tool" class="com.baeldung.factorybean.ToolFactory">
|
||||
<property name="factoryId" value="9090"/>
|
||||
<property name="toolId" value="1"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.beanfactory;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BeanFactoryWithClassPathResourceIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void createBeanFactoryAndCheckEmployeeBean() {
|
||||
Resource res = new ClassPathResource("beanfactory-example.xml");
|
||||
BeanFactory factory = new XmlBeanFactory(res);
|
||||
Employee emp = (Employee) factory.getBean("employee");
|
||||
|
||||
assertTrue(factory.isSingleton("employee"));
|
||||
assertTrue(factory.getBean("employee") instanceof Employee);
|
||||
assertTrue(factory.isTypeMatch("employee", Employee.class));
|
||||
assertTrue(factory.getAliases("employee").length > 0);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
### Relevant Articles:
|
||||
- [Guide to the Spring BeanFactory](http://www.baeldung.com/spring-beanfactory)
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
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 = { "classpath:factorybean-abstract-spring-ctx.xml" })
|
||||
public class AbstractFactoryBeanIntegrationTest {
|
||||
|
||||
@Resource(name = "singleTool")
|
||||
private Tool tool1;
|
||||
@Resource(name = "singleTool")
|
||||
private Tool tool2;
|
||||
@Resource(name = "nonSingleTool")
|
||||
private Tool tool3;
|
||||
@Resource(name = "nonSingleTool")
|
||||
private Tool tool4;
|
||||
|
||||
@Test
|
||||
public void testSingleToolFactory() {
|
||||
assertThat(tool1.getId(), equalTo(1));
|
||||
assertTrue(tool1 == tool2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonSingleToolFactory() {
|
||||
assertThat(tool3.getId(), equalTo(2));
|
||||
assertThat(tool4.getId(), equalTo(2));
|
||||
assertTrue(tool3 != tool4);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
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 javax.annotation.Resource;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = FactoryBeanAppConfig.class)
|
||||
public class FactoryBeanJavaConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Tool tool;
|
||||
|
||||
@Resource(name = "&tool")
|
||||
private ToolFactory toolFactory;
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByJava() {
|
||||
assertThat(tool.getId(), equalTo(2));
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(7070));
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
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;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:factorybean-spring-ctx.xml" })
|
||||
public class FactoryBeanXmlConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Tool tool;
|
||||
@Resource(name = "&tool")
|
||||
private ToolFactory toolFactory;
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByXml() {
|
||||
assertThat(tool.getId(), equalTo(1));
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(9090));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<?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-3.0.xsd">
|
||||
|
||||
<bean id="employee" class="com.baeldung.beanfactory.Employee">
|
||||
<constructor-arg name="name" value="Hello! My name is Java"/>
|
||||
<constructor-arg name="age" value="18"/>
|
||||
</bean>
|
||||
|
||||
<alias name="employee" alias="empalias" />
|
||||
</beans>
|
||||
Reference in New Issue
Block a user