spring的静态工厂和实例工厂方法
2017-05-29 11:10
246 查看
直接上代码吧,好理解
package com.norelax.www.factory;
/**
* 新闻实体类
*
* @author wusong
* @create 2017-05-29 上午10:29
**/
public class News {
String title;
String content;
public News(String title, String content) {
this.title = title;
this.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;
}
@Override
public String toString() {
return "News{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
静态工厂方法实体类:
package com.norelax.www.factory;
import java.util.HashMap;
import java.util.Map;
/**
* 静态工厂
*
* @author wusong
* @create 2017-05-29 上午10:30
**/
public class StaticFactory {
private static Map<String, News> newsMap = new HashMap<String, News>();
static{
newsMap.put("news1", new News("标题1", "内容1"));
newsMap.put("news2", new News("标题2", "内容2"));
}
public static News getNews(String name){
return newsMap.get(name);
}
}
实例工厂:
package com.norelax.www.factory;
import java.util.HashMap;
import java.util.Map;
/**
* 实例工厂方法
*
* @author wusong
* @create 2017-05-29 上午10:51
**/
public class InstanceFactory {
Map<String,News> newsMap=new HashMap<String, News>();
public InstanceFactory() {
newsMap.put("news1",new News("标题1","内容1"));
newsMap.put("news2",new News("标题2","内容2"));
}
public News getNews(String name){
return newsMap.get(name);
}
}
静态工厂和实例工厂方法配置:
<?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="news1" class="com.norelax.www.factory.StaticFactory" factory-method="getNews">
<constructor-arg value="news1"/>
</bean>
<!--实例工厂方法是先将工厂实例化-->
<bean id="instanceFactory" class="com.norelax.www.factory.InstanceFactory"/>
<bean id="news2" factory-bean="instanceFactory" factory-method="getNews">
<constructor-arg value="news2"/>
</bean>
</beans>
测试类:
package com.norelax.www.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试方法
*
* @author wusong
* @create 2017-05-29 上午10:39
**/
public class FactoryTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-factory.xml");
News news1 = (News) applicationContext.getBean("news1");
System.out.println(news1);
News news2 = (News) applicationContext.getBean("news2");
System.out.println(news2);
}
}
测试结果:
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@69d0a921: startup date [Mon May 29 11:01:30 CST 2017]; root of context hierarchy
五月 29, 2017 11:01:30 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-factory.xml]
News{title='标题1', content='内容1'}
News{title='标题2', content='内容2'}
Process finished with exit code 0
package com.norelax.www.factory;
/**
* 新闻实体类
*
* @author wusong
* @create 2017-05-29 上午10:29
**/
public class News {
String title;
String content;
public News(String title, String content) {
this.title = title;
this.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;
}
@Override
public String toString() {
return "News{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
静态工厂方法实体类:
package com.norelax.www.factory;
import java.util.HashMap;
import java.util.Map;
/**
* 静态工厂
*
* @author wusong
* @create 2017-05-29 上午10:30
**/
public class StaticFactory {
private static Map<String, News> newsMap = new HashMap<String, News>();
static{
newsMap.put("news1", new News("标题1", "内容1"));
newsMap.put("news2", new News("标题2", "内容2"));
}
public static News getNews(String name){
return newsMap.get(name);
}
}
实例工厂:
package com.norelax.www.factory;
import java.util.HashMap;
import java.util.Map;
/**
* 实例工厂方法
*
* @author wusong
* @create 2017-05-29 上午10:51
**/
public class InstanceFactory {
Map<String,News> newsMap=new HashMap<String, News>();
public InstanceFactory() {
newsMap.put("news1",new News("标题1","内容1"));
newsMap.put("news2",new News("标题2","内容2"));
}
public News getNews(String name){
return newsMap.get(name);
}
}
静态工厂和实例工厂方法配置:
<?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="news1" class="com.norelax.www.factory.StaticFactory" factory-method="getNews">
<constructor-arg value="news1"/>
</bean>
<!--实例工厂方法是先将工厂实例化-->
<bean id="instanceFactory" class="com.norelax.www.factory.InstanceFactory"/>
<bean id="news2" factory-bean="instanceFactory" factory-method="getNews">
<constructor-arg value="news2"/>
</bean>
</beans>
测试类:
package com.norelax.www.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试方法
*
* @author wusong
* @create 2017-05-29 上午10:39
**/
public class FactoryTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-factory.xml");
News news1 = (News) applicationContext.getBean("news1");
System.out.println(news1);
News news2 = (News) applicationContext.getBean("news2");
System.out.println(news2);
}
}
测试结果:
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@69d0a921: startup date [Mon May 29 11:01:30 CST 2017]; root of context hierarchy
五月 29, 2017 11:01:30 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-factory.xml]
News{title='标题1', content='内容1'}
News{title='标题2', content='内容2'}
Process finished with exit code 0
相关文章推荐
- Spring的实例工厂方法和静态工厂方法
- spring 静态工厂 &实例工厂实例方法 & 实现factorybean接口
- spring实例工厂,静态工厂以及写有工厂方法的程序
- Spring的实例工厂方法和静态工厂方法
- Spring的实例工厂方法和静态工厂方法实例代码
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
- spring实例化bean之简单工厂静态方法实例化
- Spring学习笔记九--静态工厂方法和实例工厂方法
- Spring4 学习系列之——bean的静态工厂方法 和 实例方法
- spring创建bean的三种方式-通过构造器,通过静态工厂方法,通过实例工厂方法
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
- Spring的实例工厂方法和静态工厂方法
- Spring学习之使用静态工厂方法以及实例工厂方法配置bean
- 从spring容器中得到bean_使用类构造器实例化_使用静态工厂方法实例化_使用实例工厂方法实例化_例子
- spring学习小结7_使用工厂方法来配置bean:静态工厂方法和实例工厂方法
- spring 9 bean配置--通过工厂方法(静态工厂方法&实例工厂方法
- Spring使用教程(二)配置bean:静态工厂方法和实例工厂方法
- spring中通过静态工厂方法获取bean实例化对象
- Spring-装配Bean的3种方式-工厂静态方法方式配置Bean
- spring实例化bean之简单工厂实例方法实例化