【Spring框架】Spring入门(二)——静态工厂与实例工厂

静态工厂:
调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当需要请求对象时,只需要调用静态方法即可。

案例实现代码:
创建一个静态工厂:

public class MyBeanFactory {
    /*
    创建实例
    需要为静态方法
     */
    public static UserService createService(){
        return new UserServiceImpl();
    }
}

UserService接口与UserServiceImpl实现类:

public interface UserService {
    public void addUser();
}


public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("b_static_factory");
    }
}

在beans.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">
        <!--将静态工厂创建的实例交予spring
                class 确定静态工厂全限定类名
                factory-method 确定静态方法名
        -->
        <bean id="UserServiceId" class="com.spring.c_inject.b_static_factory.MyBeanFactory" factory-method="createService"></bean>
</beans>

测试实现类:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStaticFactory {
    @Test
    public void demo01() {
        //自定义工厂
        UserService userService = MyBeanFactory.createService();
        userService.addUser();
    }

    @Test
    public void demo02() {
        //spring工厂
        String xmlPath = "com/spring/c_inject/b_static_factory/beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        UserService userService = applicationContext.getBean("UserServiceId",UserService.class);//这样就不用强转了
        userService.addUser();
    }
}

实例工厂:
将对象的创建过程封装到另外一个对象实例的方法里,当需要请求对象时, 只需要调用该实例方法。

案例实现代码:
创建一个实例工厂:

/**
 * 实例工厂,所有方法非静态
 */
public class MyBeanFactory {
    /*
    创建实例
     */
    public UserService createService(){
        return new UserServiceImpl();
    }
}

UserService接口与UserServiceImpl实现类:

public interface UserService {
    public void addUser();
}


public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("c_factory");
    }
}

在beans.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="myBeanFactoryId" class="com.spring.c_inject.c_factory.MyBeanFactory" ></bean>
        <!--获得userservice
            * factory-bean 确定工厂实例
            * factory-method 确定普通方法
        -->
        <bean id="UserServiceId" factory-bean="myBeanFactoryId" factory-method="createService" ></bean>
</beans>

测试实现类:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestFactory {
    @Test
    public void demo01() {
        //自定义工厂
        //1.创建工厂
        MyBeanFactory myBeanFactory = new MyBeanFactory();
        //2.通过工厂实例,获得对象
        UserService userService = myBeanFactory.createService();
        userService.addUser();
    }

    @Test
    public void demo02() {
        //spring工厂
        String xmlPath = "com/spring/c_inject/c_factory/beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        UserService userService = applicationContext.getBean("UserServiceId", UserService.class);//这样就不用强转了
        userService.addUser();
    }
}

下一节:【Spring框架】Spring入门(三)——作用域与生命周期

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谦风(Java)

一起学习,一起进步(✪ω✪)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值