新闻实体类NewsBean
package com.tang;
/**
* 新闻实体类
*/
public class News {
String title;
String content;
@Override
public String toString() {
return "News{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public News(String title, String content) {
this.title = title;
this.content = content;
}
public News() {
}
}
实例工厂方法
package com.tang;
import java.util.HashMap;
import java.util.Map;
/**
* 实例工厂
*/
public class InstanceFactory {
Map<String,News> newsMap = new HashMap<String,News>();
public InstanceFactory(){
newsMap.put("news1",new News("InstanceFactory标题1","内容1"));
newsMap.put("news2",new News("InstanceFactory标题2","内容2"));
}
public News getNews(String name){
return newsMap.get(name);
}
}
静态工厂方法
package com.tang;
import java.util.HashMap;
import java.util.Map;
/**
* 静态工厂
*/
public class StaticFactory {
private static Map<String,News> newsMap = new HashMap<String,News>();
static {
newsMap.put("news1",new News("StaticFactory标题1","内容1"));
newsMap.put("news2",new News("StaticFactory标题2","内容2"));
}
public static News getNews(String name){
return newsMap.get(name);
}
}
xml配置
<?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="news11" class="com.tang.StaticFactory" factory-method="getNews">
<constructor-arg value="news1"/>
</bean>
<bean id="news12" class="com.tang.StaticFactory" factory-method="getNews">
<constructor-arg value="news2"/>
</bean>
<!--实例工厂方法是先将工厂实例化-->
<bean id="instanceFactory" class="com.tang.InstanceFactory"/>
<bean id="news21" factory-bean="instanceFactory" factory-method="getNews">
<constructor-arg value="news1"/>
</bean>
<bean id="news22" factory-bean="instanceFactory" factory-method="getNews">
<constructor-arg value="news2"/>
</bean>
</beans>
main测试方法(使用单元测试的方法进行测试)
package com.tang;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
private ClassPathXmlApplicationContext ctx;
@Before
public void before() {
//初始化Spring容器,当Spring容器初始化时,会自动加载配置文件,然后根据配置文件中的内容初始化Bean
ctx = new ClassPathXmlApplicationContext("application.xml");
}
@Test
public void test1() {
News news11 = (News) ctx.getBean("news11");
System.out.println(news11);
News news12 = (News) ctx.getBean("news12");
System.out.println(news12);
}
@Test
public void test2() {
News news21 = (News) ctx.getBean("news21");
System.out.println(news21);
News news22 = (News) ctx.getBean("news22");
System.out.println(news22);
}
}
测试结果
test1:
News{title='StaticFactory标题1', content='内容1'}
News{title='StaticFactory标题2', content='内容2'}
Process finished with exit code 0
test2:
News{title='InstanceFactory标题1', content='内容1'}
News{title='InstanceFactory标题2', content='内容2'}
Process finished with exit code 0
参考https://blog.csdn.net/woshiwusonghaha/article/details/72801187