zoukankan      html  css  js  c++  java
  • Spring创建bean的三种方式

    1、空参构造方法

    2、静态工厂方法

    3、实例工厂

    bean.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">
    
        <!-- 依赖于空参构造函数,没有空参构造函数将不能创建 -->
        <!-- 配置service -->
        <bean id="AccountService" class="com.alphajuns.webstudy.service.impl.AccountServiceImpl"></bean>
        <!-- 配置dao -->
        <bean id="AccountDao" class="com.alphajuns.webstudy.dao.impl.AccountDaoImpl"></bean>
    
        <!-- 静态工厂方法创建对象,class指向工厂类,factory-method指向工厂类中的静态方法 -->
        <bean id="AccountService1" class="com.alphajuns.webstudy.util.StaticFactory" factory-method="createAccountService"></bean>
    
        <!-- 实例工厂方法创建对象 -->
        <bean id="InstanceFactory" class="com.alphajuns.webstudy.util.InstanceFactory"></bean>
        <!-- factory-bean指向实例工厂类,factory-method为实例工厂类的方法 -->
        <bean id="AccountService2" factory-bean="InstanceFactory" factory-method="createAccountService"></bean>
    </beans>

    AccountServiceImpl.java

    静态工厂

    实例工厂

    测试

  • 相关阅读:
    洛谷P2146 [NOI2015]软件包管理器
    洛谷P3038 [USACO11DEC]牧草种植Grass Planting
    洛谷P2831 愤怒的小鸟
    洛谷P1084 疫情控制
    洛谷P3258 [JLOI]2014松鼠的新家
    洛谷P1084 运输计划
    洛谷P2051 [AHOI2009]中国象棋
    洛谷P1438 无聊的数列
    洛谷P1312 Mayan游戏
    luogu P1038 神经网络
  • 原文地址:https://www.cnblogs.com/alphajuns/p/13261301.html
Copyright © 2011-2022 走看看