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

导读:本篇文章讲解 【Spring框架】Spring入门(二)——静态工厂与实例工厂,文章出自:https://blog.csdn.net/weixin_43316702/article/details/104954158希望对大家有帮助,欢迎收藏,转发!站点地址:www.javazhiyin.com.com

>>强大,10k+点赞的 SpringBoot 后台管理系统竟然出了详细教程!

静态工厂:
调用静态工厂方法创建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入门(三)——作用域与生命周期