• Spring三种实例化Bean的方式


    待实例化的Bean

    package org.zln.module.test2.service.impl;
    
    import org.apache.log4j.Logger;
    import org.zln.module.test2.service.PersonService;
    
    /**
     * Created by coolkid on 2015/6/6 0006.
     */
    public class PersonServiceBean implements PersonService {
        private Logger logger = Logger.getLogger(PersonServiceBean.class);
        @Override
        public void save(){
            logger.debug("我是save()方法");
        }
    
        /**
         * 静态工厂方法
         * @return 实例
         */
        public static PersonServiceBean getPersonServiceBean(){
            return new PersonServiceBean();
        }
    
        /**
         * 静态工厂方法
         * @return 实例
         */
        public PersonServiceBean getPersonServiceFactory(){
            return new PersonServiceBean();
        }
    }

    配置文件

    <?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-2.5.xsd">
    
        <!--默认构造方法实例化-->
        <bean id="personService" class="org.zln.module.test2.service.impl.PersonServiceBean"/>
        <!--静态工厂方法实例化-->
        <bean id="personService2" class="org.zln.module.test2.service.impl.PersonServiceBean" factory-method="getPersonServiceBean"/>
        <!--实例工厂方法-->
        <bean id="personServiceFactory" class="org.zln.module.test2.service.impl.PersonServiceBean"/>
        <bean id="personService3" factory-bean="personServiceFactory" factory-method="getPersonServiceFactory"/>
    
    </beans>
  • 相关阅读:
    React项目(垃圾分类后台管理)
    React 学习笔记
    xml学习
    .NET Core部署Windows服务
    .NET 生成PDF流
    无线数传电台是什么
    电流电压采集模块如何进行数据传输的
    Spring5快乐教程(十)AspectJ注解实现AOP
    Spring5快乐教程(九)注解编程详解
    Spring5快乐教程(八)注解编程详解
  • 原文地址:https://www.cnblogs.com/sherrykid/p/4573910.html
Copyright © 2020-2023  润新知