spring靜態工廠和例項工廠的區別
阿新 • • 發佈:2019-02-01
新聞實體類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