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

    静态工厂

    实例工厂

    测试

  • 相关阅读:
    Intellij IDEA13 创建多模块Maven项目
    oracle锁
    oracle rac负载均衡
    awk命令
    政务外网、政务专网、政务内网和互联网
    图片切换实现选中-未选中效果
    生成带logo 的二维码
    控制input为number时样式
    移动端适配的解决方法?
    input-checkbox选中及非选中样式设置
  • 原文地址:https://www.cnblogs.com/alphajuns/p/13261301.html
Copyright © 2011-2022 走看看