zoukankan      html  css  js  c++  java
  • Spring(六)—— 装配bean的三种方式

    所谓装配bean【对象】,就是在xml中写一个bean标签。

    一、实例化Bean的三种方式(一)—— 使用构造方法实例化

    二、实例化Bean的三种方式(二)—— 使用静态工法方法实例化

    1. java代码实现静态工厂的方法

    UserServiceFactory1.java

    1 package com.gyf.service;
    2 
    3 public class UserServiceFactory1 {
    4 
    5     public static IUserService createUserService(){
    6         return new UserServiceImpl();
    7     }
    8 }

    Lesson03.java

     1 package com.gyf.test;
     2 
     3 import com.gyf.service.IUserService;
     4 import com.gyf.service.UserServiceFactory1;
     5 
     6 import org.junit.Test;
     7 import org.springframework.context.ApplicationContext;
     8 import org.springframework.context.support.ClassPathXmlApplicationContext;
     9 
    10 /**
    11  * 装配bean的三种方式
    12  */
    13 public class Lesson03 {
    14 
    15     @Test
    16     public void test2(){
    17         /**
    18          * 静态工厂
    19          */
    20         IUserService userservice2 = UserServiceFactory1.createUserService();
    21         userservice2.add();
    22     }
    23 }

    2. Spring的方式实现静态工厂

     beans3.xml

    1 <!-- 第二种方式:通过静态工厂方法-->
    2     <bean id="userService2" class="com.gyf.service.UserServiceFactory1" factory-method="createUserService"></bean>

    Lesson03.java

     1 package com.gyf.test;
     2 
     3 import com.gyf.service.IUserService;
     4 import com.gyf.service.UserServiceFactory1;
     5 
     6 import org.junit.Test;
     7 import org.springframework.context.ApplicationContext;
     8 import org.springframework.context.support.ClassPathXmlApplicationContext;
     9 
    10 /**
    11  * 装配bean的三种方式
    12  */
    13 public class Lesson03 {
    14     @Test
    15     public void test3(){
    16 
    17         /**
    18          * 装配bean的第二种方式:使用静态工厂方法实例化
    19          *
    20           */
    21         ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
    22         IUserService userService3 = (IUserService) context.getBean("userService2");
    23         userService3.add();
    24     }
    25 }

    三、实例化Bean的三种方式(三)—— 使用实例工厂方法实例化

    1. 使用java代码的实例工厂方法实例化bean

    UserServiceFactory2.java

    1 package com.gyf.service;
    2 
    3 public class UserServiceFactory2 {
    4     public IUserService createUserService(){
    5         return new UserServiceImpl();
    6     }
    7 }

    Lesson03.java

     1 package com.gyf.test;
     2 
     3 import com.gyf.service.IUserService;
     4 import com.gyf.service.UserServiceFactory1;
     5 import com.gyf.service.UserServiceFactory2;
     6 
     7 import org.junit.Test;
     8 import org.springframework.context.ApplicationContext;
     9 import org.springframework.context.support.ClassPathXmlApplicationContext;
    10 
    11 /**
    12  * 装配bean的三种方式
    13  */
    14 public class Lesson03 {
    15     
    16     @Test
    17     public void test4(){
    18         /**
    19          * 实例化工厂
    20          */
    21         UserServiceFactory2 factory = new UserServiceFactory2();
    22         IUserService userService4 = factory.createUserService();
    23         userService4.add();
    24     }
    25 }

    2. 使用 Spring框架的实例化工厂方法,实例化bean

    beans.xml

    1 <!--第三种方式:通过实例化工厂方法-->
    2     <!--创建实例factory2 bean-->
    3     <bean id="factory2" class="com.gyf.service.UserServiceFactory2"></bean>
    4     <!--创建实例userService3 bean-->
    5     <bean id="userService3" factory-bean="factory2" factory-method="createUserService"></bean>

    Lesson03.java

    1 @Test
    2     public void test5(){
    3         /**
    4         * 装配bean的第三种方式:使用实例化工厂方法实例化
    5         */
    6         ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
    7         IUserService userService5 = (IUserService) context.getBean("userService3");
    8         userService5.add();
    9     }
  • 相关阅读:
    每日日报2021.5.14
    每日日报2021.5.13
    每日日报2021.5.12
    每日日报2021.5.11 冲刺第十天
    《梦断代码》读后感3
    每日日报2021.5.10 冲刺第九天
    《梦断代码》读后感2
    每日日报2021 5/23
    每日日报2021 5/22
    每日日报2021 5/21
  • 原文地址:https://www.cnblogs.com/upyang/p/13562635.html
Copyright © 2011-2022 走看看