spring静态工厂和实例工厂的区别

新闻实体类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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值