Spring bean 的4種注入方式

package com.ssm.testmybatis.ioc.interfaces;

import com.ssm.service.InjectionService;
import com.ssm.testmybatis.base.UnitTestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase {
   
   public TestInjection() {
      super("classpath:spring-injection.xml");
   }
   
   @Test
   public void testSetter() {
      InjectionService service = super.getBean("injectionService-setter");
      service.save("一、設值注入-這是要保存的數據");
   }

   @Test
   public void testCons() {
      InjectionService service = super.getBean("injectionService-constructor");
      service.save("二、構造注入-這是要保存的數據");
   }

   @Test
   public void testStaticFactory() {
      InjectionService service = super.getBean("injectionService-staticFactory");
      service.save("三、靜態工廠注入-這是要保存的數據");
   }

   @Test
   public void testFactory() {
      InjectionService service = super.getBean("injectionService-factory");
      service.save("四、實例工廠注入-這是要保存的數據");
   }
}

spring-injection.xml配置以下,注意:下面的四種注入方式同一時刻只能開啓一種注入方式!!!html

<?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" >
        
        <!--一、設值注入setter-->
        <!--<bean id="injectionService-setter" class="com.ssm.dao.impl.InjectionServiceImpl">-->
            <!--<property name="injectionDAO" ref="injectionDAO"/>-->
        <!--</bean>-->

        <!--二、構造注入-->
        <!--<bean id="injectionService-constructor" class="com.ssm.dao.impl.InjectionServiceImpl">-->
             <!--<constructor-arg ref="injectionDAO"/>-->
         <!--</bean>-->
        <!--<bean id="injectionDAO" class="com.ssm.dao.impl.InjectionDAOImpl"></bean>-->

        <!--三、靜態工廠注入-->
        <!--<bean id="injectionService-staticFactory" class="com.ssm.dao.impl.InjectionServiceImpl">-->
             <!--<property name="injectionDAO" ref="userDao_name"/>-->
        <!--</bean>-->
        <!--<bean name="userDao_name" class="com.render.InjectionDaoFactory" factory-method="createInjectDaoStaticInstance" />-->

        <!--四、工廠方法注入-->
        <bean id="injectionService-factory" class="com.ssm.dao.impl.InjectionServiceImpl">
            <property name="injectionDAO" ref="userDao_name"/>
        </bean>
        <bean  name="userDaoFactory_name" class="com.render.InjectionDaoFactory" />
        <bean name="userDao_name" factory-bean="userDaoFactory_name" factory-method="createInjectDaoInstance" />
</beans>

工廠類:spring

package com.render;

import com.ssm.dao.impl.InjectionDAOImpl;

/**
 * Created by yg on 2019/2/25.
 */
public class InjectionDaoFactory {

    public static InjectionDAOImpl createInjectDaoStaticInstance() {
        System.out.println("---- 進入到靜態工廠方法");
        return new InjectionDAOImpl();
    }

    public  InjectionDAOImpl createInjectDaoInstance() {
        System.out.println("---- 進入到工廠方法");
        return new InjectionDAOImpl();
    }
}
package com.ssm.service;

public interface InjectionService {
   
   public void save(String arg);
}
package com.ssm.dao.impl;

import com.ssm.service.InjectionService;
import com.ssm.dao.InjectionDAO;


public class InjectionServiceImpl implements InjectionService {
   
   private InjectionDAO injectionDAO;
   
// //構造器注入
// public InjectionServiceImpl(InjectionDAO injectionDAO1) {
//    this.injectionDAO = injectionDAO1;
// }

   //設值注入,注意:靜態工廠注入和實例工廠注入必須實現setter方法!!!
   public void setInjectionDAO(InjectionDAO injectionDAOcccc) {
      this.injectionDAO = injectionDAOcccc;
   }

   public void save(String arg) {
      //模擬業務操做
      System.out.println("Service接收參數:" + arg);
      arg = arg + ":" + this.hashCode();
      injectionDAO.save(arg);
   }
}
package com.ssm.dao;

public interface InjectionDAO {
   
   public void save(String arg);
}
package com.ssm.dao.impl;

import com.ssm.dao.InjectionDAO;

public class InjectionDAOImpl implements InjectionDAO {
   
   public void save(String arg) {
      //模擬數據庫保存操做
      System.out.println("保存數據:" + arg);
   }
}

Spring中四種實例化bean的方式
Spring中Bean的實例化數據庫

相關文章
相關標籤/搜索