Spring 中,Bean的实例化方式大致有四种

构造方法

UserService

public interface UserService {
    void addUser();
}

提供无参构造方法

public class UserServiceImpl implements UserService {

    public UserServiceImpl() {
        System.out.println("构造方法被执行!");
    }

    @Override
    public void addUser() {
        System.out.println("add a User.....");
    }
}

applicationContext.xml

<bean id="userService" class="com.origin.service.impl.UserServiceImpl"/>

UserTest

public class UserTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.addUser();
    }
}

运行结果

image-1652194346779

反例:如果在UserServiceImpl中不存在无参构造方法,会报以下错误:没有默认的构造方法

No default constructor found; nested exception is java.lang.NoSuchMethodException

静态工厂

OrderService

public interface OrderService {
    void addOrder();
}

OrderServiceImpl

public class OrderServiceImpl implements OrderService {

    @Override
    public void addOrder() {
        System.out.println("add a order.....");
    }
}

OrderFactory

public class OrderFactory {
    public static OrderService getOrderService(){
        System.out.println("静态工厂被执行.....");
        return new OrderServiceImpl();
    }
}

applicationContext.xml

    <!--此处要配置静态工厂方法:factory-method-->
    <bean id="orderServiceFactory" class="com.origin.factory.OrderFactory" factory-method="getOrderService"/>

OrderTest

public class OrderTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        OrderService oderService = (OrderService) context.getBean("orderServiceFactory");
        oderService.addOrder();
    }
}

运行结果

image-1652195130272

实例工厂

GoodService

public interface GoodService {
    void addGood();
}

GoodServiceImpl

public class GoodServiceImpl implements GoodService {
    @Override
    public void addGood() {
        System.out.println("add a good.....");
    }
}

GoodFactory

public class GoodFactory {
    //可以看出,非常类似静态工厂,只是变成了非静态方法
    public GoodService getGoodService(){
        System.out.println("实例工厂被执行.....");
        return new GoodServiceImpl();
    }
}

applicationContext.xml

    <!--此处要先配置工厂bean:factory-bean,再配置实例工厂方法:factory-method-->
    <bean id="goodFactory" class="com.origin.factory.GoodFactory"/>
    <bean id="goodService" factory-bean="goodFactory" factory-method="getGoodService"/>

OrderTest

public class GoodTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        GoodService goodService = (GoodService) context.getBean("goodService");
        goodService.addGood();
    }
}

运行结果

image-1652195964887

FactoryBean

FactoryBean可以说是Spring将实例工厂方式的一种封装,基本类似。

BookService

public interface BookService {
    void addBook();
}

GoodServiceImpl

public class BookServiceImpl implements BookService {
    @Override
    public void addBook() {
        System.out.println("add a book.....");
    }
}

BookFactory

public class BookFactory implements FactoryBean<BookService> {

    //代替了原本实例工厂方式下创建对象的方法
    @Override
    public BookService getObject() throws Exception {
        System.out.println("FactoryBean被执行.....");
        return new BookServiceImpl();
    }

    //配置该bean是什么类型
    @Override
    public Class<?> getObjectType() {
        return BookService.class;
    }

    //配置单例
    @Override
    public boolean isSingleton() {
        return true;
    }
}

applicationContext.xml

    <!--直接配置成工厂的bean即可-->
    <bean id="bookFactory" class="com.origin.factory.BookFactory"/>

OrderTest

public class BookTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) context.getBean("bookFactory");
        bookService.addBook();
    }
}

运行结果

image-1652196721957