BAEL-453 some improvments, formatting etc.
This commit is contained in:
parent
d6daf8235c
commit
b8ade6f6da
@ -5,6 +5,7 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class FactoryBeanAppConfig {
|
public class FactoryBeanAppConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ToolFactory tool() {
|
public ToolFactory tool() {
|
||||||
ToolFactory factory = new ToolFactory();
|
ToolFactory factory = new ToolFactory();
|
||||||
|
@ -7,6 +7,7 @@ import org.springframework.beans.factory.InitializingBean;
|
|||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
public class InitializationToolFactory implements FactoryBean<Tool>, InitializingBean {
|
public class InitializationToolFactory implements FactoryBean<Tool>, InitializingBean {
|
||||||
|
|
||||||
private int factoryId;
|
private int factoryId;
|
||||||
private int toolId;
|
private int toolId;
|
||||||
private String toolName;
|
private String toolName;
|
||||||
|
@ -3,6 +3,7 @@ package com.baeldung.factorybean;
|
|||||||
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||||
|
|
||||||
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
|
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||||
|
|
||||||
private int factoryId;
|
private int factoryId;
|
||||||
private int toolId;
|
private int toolId;
|
||||||
private String toolName;
|
private String toolName;
|
||||||
|
@ -8,6 +8,7 @@ import org.springframework.beans.factory.FactoryBean;
|
|||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
public class PostConstructToolFactory implements FactoryBean<Tool> {
|
public class PostConstructToolFactory implements FactoryBean<Tool> {
|
||||||
|
|
||||||
private int factoryId;
|
private int factoryId;
|
||||||
private int toolId;
|
private int toolId;
|
||||||
private String toolName;
|
private String toolName;
|
||||||
|
@ -4,6 +4,7 @@ import org.springframework.beans.factory.config.AbstractFactoryBean;
|
|||||||
|
|
||||||
//no need to set singleton property because default value is true
|
//no need to set singleton property because default value is true
|
||||||
public class SingleToolFactory extends AbstractFactoryBean<Tool> {
|
public class SingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||||
|
|
||||||
private int factoryId;
|
private int factoryId;
|
||||||
private int toolId;
|
private int toolId;
|
||||||
private String toolName;
|
private String toolName;
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
package com.baeldung.factorybean;
|
package com.baeldung.factorybean;
|
||||||
|
|
||||||
public class Tool {
|
public class Tool {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
public Tool() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Tool(int id, String name, double price) {
|
public Tool(int id, String name, double price) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -3,6 +3,7 @@ package com.baeldung.factorybean;
|
|||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
|
||||||
public class ToolFactory implements FactoryBean<Tool> {
|
public class ToolFactory implements FactoryBean<Tool> {
|
||||||
|
|
||||||
private int factoryId;
|
private int factoryId;
|
||||||
private int toolId;
|
private int toolId;
|
||||||
private String toolName;
|
private String toolName;
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package com.baeldung.factorybean;
|
package com.baeldung.factorybean;
|
||||||
|
|
||||||
public class Worker {
|
public class Worker {
|
||||||
|
|
||||||
private String number;
|
private String number;
|
||||||
private Tool tool;
|
private Tool tool;
|
||||||
|
|
||||||
public Worker() {
|
public Worker() {}
|
||||||
}
|
|
||||||
|
|
||||||
public Worker(String number, Tool tool) {
|
public Worker(String number, Tool tool) {
|
||||||
this.number = number;
|
this.number = number;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns="http://www.springframework.org/schema/beans"
|
xmlns="http://www.springframework.org/schema/beans"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
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">
|
<bean id="singleTool" class="com.baeldung.factorybean.SingleToolFactory">
|
||||||
<property name="factoryId" value="3001"/>
|
<property name="factoryId" value="3001"/>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns="http://www.springframework.org/schema/beans"
|
xmlns="http://www.springframework.org/schema/beans"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
<bean id="initializationTool" class="com.baeldung.factorybean.InitializationToolFactory">
|
<bean id="initializationTool" class="com.baeldung.factorybean.InitializationToolFactory">
|
||||||
<property name="factoryId" value="1010"/>
|
<property name="factoryId" value="1010"/>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:context="http://www.springframework.org/schema/context"
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
xmlns="http://www.springframework.org/schema/beans"
|
xmlns="http://www.springframework.org/schema/beans"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||||
|
|
||||||
<context:annotation-config/>
|
<context:annotation-config/>
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns="http://www.springframework.org/schema/beans"
|
xmlns="http://www.springframework.org/schema/beans"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
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">
|
<bean id="tool" class="com.baeldung.factorybean.ToolFactory">
|
||||||
<property name="factoryId" value="9090"/>
|
<property name="factoryId" value="9090"/>
|
||||||
|
@ -9,6 +9,7 @@ import org.springframework.context.ApplicationContext;
|
|||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
public class AbstractFactoryBeanTest {
|
public class AbstractFactoryBeanTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSingleToolFactory() {
|
public void testSingleToolFactory() {
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-abstract-spring-ctx.xml");
|
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-abstract-spring-ctx.xml");
|
||||||
|
@ -5,6 +5,7 @@ import org.springframework.beans.factory.BeanCreationException;
|
|||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
public class FactoryBeanInitializeTest {
|
public class FactoryBeanInitializeTest {
|
||||||
|
|
||||||
@Test(expected = BeanCreationException.class)
|
@Test(expected = BeanCreationException.class)
|
||||||
public void testInitializationToolFactory() {
|
public void testInitializationToolFactory() {
|
||||||
new ClassPathXmlApplicationContext("classpath:factorybean-init-spring-ctx.xml");
|
new ClassPathXmlApplicationContext("classpath:factorybean-init-spring-ctx.xml");
|
||||||
|
@ -9,6 +9,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
|||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
public class FactoryBeanTest {
|
public class FactoryBeanTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConstructWorkerByXml() {
|
public void testConstructWorkerByXml() {
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-spring-ctx.xml");
|
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-spring-ctx.xml");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user