1、无参构造方法

图中就使用了无参构造实例化对象

Spring --04.Spring生成Bean的三种方式_spring

 

2、静态工厂实例化方式

通过调用工厂类的静态方法来生成bean

AddrDao.java接口

package com.day01.dao;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:01 2018/11/8
 */
public interface AddrDao {
    public abstract void save();
}

AddrDaoImpl.java实现类

package com.day01.dao.Impl;

import com.day01.dao.AddrDao;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:01 2018/11/8
 */
public class AddrDaoImpl implements AddrDao {
    @Override
    public void save() {
        System.out.println("持久层。。。。。。");
    }
}

工厂类Factory.java

package com.day01.factory;

import com.day01.dao.AddrDao;
import com.day01.dao.Impl.AddrDaoImpl;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:03 2018/11/8
 */
public class Factory {

    /**
     * 静态方法 实例化AddrDaoImpl类
     * @return
     */
    public static AddrDao create(){
        System.out.println("调用了静态工厂方法。。。");
        return new AddrDaoImpl();
    }
}

applicationContext.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="userDaos" class="com.day01.dao.Impl.UserDaoImpl"
 scope="singleton" init-method="init" destroy-method="destroy"></bean>-->


    <bean id="addrDao" class="com.day01.factory.Factory" factory-method="create"> </bean>

</beans>

注:在配置文件中、配置AddrDaoImpl这个bean时、class属性写的不是AddrDaoImpl全路径名、而是工厂类的全路径名、

factroy-method:指定工厂类中静态方法的名字。创建类时就调用该方法

TestIoc.java测试类

package com.day01.test;

import com.day01.dao.AddrDao;
import com.day01.dao.UserDao;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:30 2018/11/7
 */
public class TestIoC {

    @Test
    public void test5(){
        //创建Spring工厂
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        AddrDao addrDao = (AddrDao) ac.getBean("addrDao");
        addrDao.save();
    }

}

Spring --04.Spring生成Bean的三种方式_spring_02

3、实例工厂实例化方式

修改Factory工厂类、Factory.java

 

package com.day01.factory;

import com.day01.dao.AddrDao;
import com.day01.dao.Impl.AddrDaoImpl;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:03 2018/11/8
 */
public class Factory {

    /**
     * 静态方法 实例化AddrDaoImpl类
     * @return
     */
    public AddrDao create(){
        System.out.println("调用了静态工厂方法。。。");
        return new AddrDaoImpl();
    }
}

applicationContent.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="userDaos" class="com.day01.dao.Impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>-->


    <!--<bean id="addrDao" class="com.day01.factory.Factory" factory-method="create"> </bean>-->
    <!--实例工厂方法来实例化-->
    <bean id="factory" class="com.day01.factory.Factory"></bean>
    <bean id="addrDao" factory-method="create" factory-bean="factory"></bean>
</beans>

 

Spring --04.Spring生成Bean的三种方式_Spring_03

Spring --04.Spring生成Bean的三种方式_实例化_04

factory-bean:指定工厂bean的id;

Factory-method:指定工厂bean的实例工厂方法的名字

TestIoc.java测试方法

package com.day01.test;

import com.day01.dao.AddrDao;
import com.day01.dao.UserDao;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:30 2018/11/7
 */
public class TestIoC {

    @Test
    public void test5(){
        //创建Spring工厂
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        AddrDao addrDao = (AddrDao) ac.getBean("addrDao");
        addrDao.save();
    }

}

Spring --04.Spring生成Bean的三种方式_Spring生成Bean的三种方式_05