spring

静态工厂创建Bean

定义接口

public interface Being {
    public void testBeing();
}

接口实现

复制代码
public class Dog implements Being {
    private String msg;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void testBeing() {
        System.out.println(msg+",dogs like bones");
    }
}
复制代码
复制代码
public class Cat implements Being {
    private String msg;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void testBeing() {
        System.out.println(msg+",cats like rats");
    }
}
复制代码

静态工厂

复制代码
public class BeingFactory {
    public static Being getBeing(String arg){
        if(arg.equalsIgnoreCase("dog")){
            return new Dog();
        }
        else{
            return new Cat();
        }
    }
}
复制代码

Spring配置services.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="dog" class="org.mythsky.springdemo1.BeingFactory" factory-method="getBeing">
        <constructor-arg value="dog"></constructor-arg>
        <property name="msg" value="this is a dog"></property>
    </bean>
    <bean id="cat" class="org.mythsky.springdemo1.BeingFactory" factory-method="getBeing">
        <constructor-arg value="cat"></constructor-arg>
        <property name="msg" value="this is a dog"></property>
    </bean>


</beans>
复制代码

测试

复制代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    public static void main(String[] args){
        ApplicationContext context=new ClassPathXmlApplicationContext("services.xml");
        Being b1=context.getBean("dog",Being.class);
        Being b2=context.getBean("cat",Being.class);
        b1.testBeing();
        b2.testBeing();
    }
}
复制代码

结果

 实例工厂创建Bean

public interface Person {
    void sayHello(String name);
    void sayGoodbye(String name);
}
复制代码
public class American implements Person {
    public void sayHello(String name) {
        System.out.println(name+",Hello!");
    }

    public void sayGoodbye(String name) {
        System.out.println(name+",Good Bye!");
    }
}
复制代码
复制代码
public class Chinese implements Person {
    public void sayHello(String name) {
        System.out.println(name+"你好!");
    }

    public void sayGoodbye(String name) {
        System.out.println(name+"再见!");
    }
}
复制代码
复制代码
public class PersonFactory {
    public Person getPerson(String country){
        if(country.equalsIgnoreCase("US")){
            return new American();
        }else{
            return new Chinese();
        }
    }
}
复制代码
<bean id="personFactory" class="org.mythsky.springdemo1.PersonFactory"></bean>
    <bean  id="chinese" factory-bean="personFactory" factory-method="getPerson">
        <constructor-arg value="chinese"></constructor-arg>
    </bean>
    <bean  id="american" factory-bean="personFactory" factory-method="getPerson">
        <constructor-arg value="us"></constructor-arg>
    </bean>
Person p1=context.getBean("chinese",Person.class);
        Person p2=context.getBean("american",Person.class);
        String name="tom";
        p1.sayHello(name);
        p1.sayGoodbye(name);
        p2.sayHello(name);
        p2.sayGoodbye(name);

 spring默认是单例模式,如果每次需要新建的话,需要设置scope

Dog dog1=context.getBean("dog",Dog.class);
        Dog dog2=context.getBean("dog",Dog.class);
        System.out.println(dog1==dog2);

<bean id="dog" class="org.mythsky.springdemo1.BeingFactory" factory-method="getBeing" scope="prototype">
        <constructor-arg value="dog"></constructor-arg>
        <property name="msg" value="this is a dog"></property>
    </bean>

再次运行

 

posted @   uptothesky  阅读(277)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
点击右上角即可分享
微信分享提示