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

    静态工厂

    实例工厂

    测试

  • 相关阅读:
    python 全栈开发大纲
    2018/6/22 晚
    python——小知识
    变量与常量
    1.2数据结构-抽象数据类型的表示和实现
    1.1数据结构-基本概念和术语
    人机交互实践04-定位作业
    人机交互实践04-图像浮动至右边
    人机交互实践03-课堂作业2
    人机交互实践03-链接到的网页
  • 原文地址:https://www.cnblogs.com/alphajuns/p/13261301.html
Copyright © 2011-2022 走看看