当前位置:网站首页>Three methods of bean instantiation

Three methods of bean instantiation

2022-06-21 07:51:00 Xiao Zhao can order Java

The first construction method ( Commonly used ):

step 1: Create an interface BookDao

package com.cdcas.dao;

public interface BookDao {
    
    void save();
}

step 2: Create implementation classes BookDaoImpl

package com.cdcas.dao.impl;

import com.cdcas.dao.BookDao;

public class BookDaoImpl implements BookDao {
    
    public BookDaoImpl() {
    
        System.out.println("Spring Container instantiation by construction method BookImpl object ");
    }
    public void save(){
    
        System.out.println("book dao save...");
    }
}

step 3: stay xml Configuration in file bean

  <bean id="bookDao" class="com.cdcas.dao.impl.BookDaoImpl"/>

step 4: To test

@org.junit.Test
    public void test2(){
    
        // obtain IoC Containers 
        ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
        // Get through the construction method bean
        BookDao bookDao=(BookDao) appCon.getBean("bookDao");
    }

result
 Insert picture description here

The second kind of static factory ( understand ):

step 1: establish OrderDao Interface

package com.cdcas.dao;

public interface OrderDao {
    
    void save();
}

step 2: establish OrderDaoImpl Implementation class

package com.cdcas.dao.impl;

import com.cdcas.dao.OrderDao;

public class OrderDaoImpl implements OrderDao {
    

    @Override
    public void save() {
    
        System.out.println("Spring Containers are instantiated through static factories OderDaoImpl object ");
    }
}

step 3: stay xml Configuration in file bean

    <bean id="orderDao" class="com.cdcas.factory.OrderDaoFactory" factory-method="getOrderDao"/>

step 4: To test

    @org.junit.Test
    public void test3(){
    
        // obtain IoC Containers 
        ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
        // Obtained through a static factory bean
        OrderDao orderDao=(OrderDao) appCon.getBean("orderDao");
        orderDao.save();
    }

result  Insert picture description here

The third example factory ( understand )

step 1: establish UserDao Interface

package com.cdcas.dao;

public interface UserDao {
    
    void save();
}

step 2: establish UserDaoImpl Implementation class

package com.cdcas.dao.impl;

import com.cdcas.dao.UserDao;

public class UserDaoImpl implements UserDao {
    

    @Override
    public void save() {
    
        System.out.println("Spring The container is instantiated through the instance factory UserDaoImpl object ");
    }
}

step 3: Create an instance factory UserDaoFactory

package com.cdcas.factory;

import com.cdcas.dao.UserDao;
import com.cdcas.dao.impl.UserDaoImpl;

public class UserDaoFactory {
    
    private static final UserDao userDao=new UserDaoImpl();
    public UserDao getUserDao(){
    
        return userDao;
    }
}

step 4: To configure xml

  <!-- Instance factory -->
    <bean id="userFactory" class="com.cdcas.factory.UserDaoFactory"/>
    <bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>

step 5: test

//  Instance factory instance object 
    @org.junit.Test
    public void test4(){
    
        // obtain IoC Containers 
        ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
        // Obtained through a static factory bean
        UserDao userDao=(UserDao) appCon.getBean("userDao");
        userDao.save();
    }

result :
 Insert picture description here

Implementation of the fourth method FactoryBean Interface :

By observing the example of xml To configure :

  <!-- Instance factory -->
    <bean id="userFactory" class="com.cdcas.factory.UserDaoFactory"/>
    <bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>

We can find out , Only by instantiating a factory object can you call the methods in the factory to get UserDao Example . It's a hassle , therefore Spring
Provides FactoryBean Interface to solve this problem . Here is the implementation process :

step 1: establish UserDaoFactoryBean Implementation class implementation FactoryBean Interface

import com.cdcas.dao.UserDao;
import com.cdcas.dao.impl.UserDaoImpl;
import org.springframework.beans.factory.FactoryBean;

public class UserDaoFactoryBean implements FactoryBean {
    
    private static UserDao userDao=new UserDaoImpl();
    // Instead of creating objects in the original instance factory 
    @Override
    public Object getObject() throws Exception {
    
            return userDao;
    }

    @Override
    public Class<?> getObjectType() {
    
        return UserDao.class;
    }
}

step 2xml:

 <!-- Use FactoryBean Instantiation bean-->
    <bean id="userDao2" class="com.cdcas.factory.UserDaoFactoryBean"/>

step 3:

  //  example FactoryBean Instance object 
    @org.junit.Test
    public void test5(){
    
        // obtain IoC Containers 
        ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
        // Obtained through a static factory bean
        UserDao userDao=(UserDao) appCon.getBean("userDao2");
        userDao.save();
    }

result :
 Insert picture description here

原网站

版权声明
本文为[Xiao Zhao can order Java]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210748439829.html