Spring實例化bean的四種方式詳解

一、bean實例化——構造方法(常用)

bean本質上就是對象,創建bean使用構造方法完成

BookDao接口:

public interface BookDao {
    public void save();
}

BookDaoImpl實現類,利用構造方式提供可訪問的構造方法,輸出相應字符串:

import com.itheima.dao.BookDao;
 
public class BookDaoImpl implements BookDao {
 
    public BookDaoImpl() {
        System.out.println("book dao constructor is running ....");
    }
 
    public void save() {
        System.out.println("book dao save ...");
    }
 
}

AppForinstanceBook:

import com.itheima.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class AppForInstanceBook {
    public static void main(String[] args) {
 
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
 
        bookDao.save();
 
    }
}

applictionContext.xml配置:

<!--方式一:構造方法實例化bean-->
    <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>

執行結果:

 無參構造方法如果不存在,將拋出異常BeanCreationException

二、bean實例化——靜態工廠(瞭解)

OrderDao接口:

public interface OrderDao {
    public void save();
}

OrderDaoImpl實現類:

import com.itheima.dao.OrderDao;
 
public class OrderDaoImpl implements OrderDao {
 
    public void save() {
        System.out.println("order dao save ...");
    }
}

OrderDaoFactory靜態工廠創建對象:

import com.itheima.dao.OrderDao;
import com.itheima.dao.impl.OrderDaoImpl;
//靜態工廠創建對象
public class OrderDaoFactory {
    public static OrderDao getOrderDao(){
        System.out.println("factory setup....");
        return new OrderDaoImpl();
    }
}

AppForinstanceOrder:

import com.itheima.dao.OrderDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class AppForInstanceOrder {
    public static void main(String[] args) {
 
 
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 
        OrderDao orderDao = (OrderDao) ctx.getBean("orderDao");
 
        orderDao.save();
 
    }
}

 applictionContext.xml配置:

  <!--方式二:使用靜態工廠實例化bean-->
    <bean id="orderDao" class="com.itheima.factory.OrderDaoFactory" factory-method="getOrderDao"/>

執行結果:

這種方式是為瞭兼容早期的遺留系統使用,瞭解即可。

三、bean實例化——實例工廠(瞭解)

UserDao接口:

public interface UserDao {
    public void save();
}

UserDaoImpl實現類:

import com.itheima.dao.UserDao;
 
public class UserDaoImpl implements UserDao {
 
    public void save() {
        System.out.println("user dao save ...");
    }
}

UserDaoFactory實例工廠創建對象:

import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoImpl;
//實例工廠創建對象
public class UserDaoFactory {
    public UserDao getUserDao(){
        return new UserDaoImpl();
    }
}

AppForInstanceUser:

import com.itheima.dao.UserDao;
import com.itheima.factory.UserDaoFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class AppForInstanceUser {
    public static void main(String[] args) {
        //創建實例工廠對象
        UserDaoFactory userDaoFactory = new UserDaoFactory();
        //通過實例工廠對象創建對象
        UserDao userDao = userDaoFactory.getUserDao();
        userDao.save();
 
    }
}

因為不是靜態方法,必須用工廠對象對象調用。

也可用Spring方法,不過要在xml文件中進行修改。

AppForInstanceUser:

import com.itheima.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class AppForInstanceUser {
    public static void main(String[] args) {
 
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 
        UserDao userDao = (UserDao) ctx.getBean("userDao");
 
        userDao.save();
 
    }
}

applictionContext.xml配置:

userFactory配合使用的,實際無意義

factory-method方法名不固定,每次都需要配置

<!--方式三:使用實例工廠實例化bean-->
   <bean id="userFactory" class="com.itheima.factory.UserDaoFactory"/>
    
    <bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>

執行結果:

四、bean實例化——FactoryBean(實用)

UserDao接口和UserDaoImpl實現類和實例化三一樣。

UserDaoFactoryBean,代替瞭原始實例化工廠中創建對象的方法:

import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoImpl;
import org.springframework.beans.factory.FactoryBean;
//FactoryBean創建對象
public class UserDaoFactoryBean implements FactoryBean<UserDao> {
    //代替原始實例工廠中創建對象的方法
    public UserDao getObject() throws Exception {
        return new UserDaoImpl();
    }
 
    public Class<?> getObjectType() {
        return UserDao.class;
    }
}

AppForInstanceUser:

import com.itheima.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class AppForInstanceUser {
    public static void main(String[] args) {
 
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 
        UserDao userDao = (UserDao) ctx.getBean("userDao");
 
        userDao.save();
 
    }
}

applictionContext.xml配置:

    <!--方式四:使用FactoryBean實例化bean-->
    <bean id="userDao" class="com.itheima.factory.UserDaoFactoryBean"/>

執行結果:

對象默認單例,需要改非單例時

在UserDaoFactoryBean重寫isSingleton方法,true為單例模式,false為非單例模式

通常為單例,這個方法通常不寫

    public boolean isSingleton() {
        return true;
    }

到此這篇關於Spring實例化bean的四種方式詳解的文章就介紹到這瞭,更多相關Spring實例化bean內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: