当前位置: 首页 > news >正文 如何加强精神文明网站建设内容广东监理建设协会网站 news 2025/10/27 15:05:25 如何加强精神文明网站建设内容,广东监理建设协会网站,互联网+创业项目计划书,wordpress 反应慢Spring为Bean提供了多种实例化方式#xff0c;通常包括4种方式。#xff08;也就是说在Spring中为Bean对象的创建准备了多种方案#xff0c;目的是#xff1a;更加灵活#xff09; 第一种#xff1a;通过构造方法实例化第二种#xff1a;通过简单工厂模式实例化第三种通常包括4种方式。也就是说在Spring中为Bean对象的创建准备了多种方案目的是更加灵活 第一种通过构造方法实例化第二种通过简单工厂模式实例化第三种通过factory-bean实例化第四种通过FactoryBean接口实例化 1.通过构造方法实例化 默认情况下会调用Bean的无参数构造方法。 定义一个Bean package com.powernode.spring6.bean;public class SpringBean {public SpringBean() {System.out.println(SpringBean的无参数构造方法执行);} }Spring配置文件 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--Spring提供的实例化方式第一种在spring配置文件中直接配置类全路径Spring会自动调用该类的无参数构造方法来实例化Bean--bean idspringBean classcom.powernode.spring6.bean.SpringBean/ /beans测试 package com.powernode.spring6.test;import com.powernode.spring6.bean.SpringBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanInstantiationTest {Testpublic void testInstantiation1(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);SpringBean springBean applicationContext.getBean(springBean, SpringBean.class);System.out.println(springBean);} }2.通过简单工厂模式实例化 定义一个Bean package com.powernode.spring6.bean; /*** Bean*/ public class Star {public Star() {System.out.println(Star的无参数构造方法执行);} }编写简单工厂模式当中的工厂类 package com.powernode.spring6.bean; /*** 简单工厂模式中的工厂类角色*/ public class StarFactory {// 工厂类中有一个静态方法public static Star get(){// Star对象最终实际上创建的时候还是我们负责new的对象return new Star();} }在Spring配置文件中指定创建该Bean的方法使用factory-method属性指定 !--Spring提供的实例化方式第二种通过简单工厂模式。你需要在Spring配置文件中告诉Spring框架调用哪个类的哪个方法获取Bean-- !--factory-method 属性指定的是工厂类当中的静态方法。也就是告诉Spring框架调用这个方法可以获取Bean。-- bean idstar classcom.powernode.spring6.bean.StarFactory factory-methodget/编写测试程序 Test public void testInstantiation2(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);Star star applicationContext.getBean(star, Star.class);System.out.println(star); }3.通过factory-bean实例化 这种方式本质上是通过工厂方法模式进行实例化。 定义一个Bean package com.powernode.spring6.bean; /*** 工厂方法模式当中的 具体产品角色*/ public class Gun {public Gun() {System.out.println(Gun的无参数构造方法执行);} }定义具体工厂类工厂类中定义实例方法 package com.powernode.spring6.bean; /*** 工厂方法模式中的 具体工厂角色*/ public class GunFactory {// 工厂方法模式中的具体工厂角色中的方法是实例方法public Gun get(){// 实际上new对象还是我们自己new的return new Gun();} }在Spring配置文件中指定factory-bean以及factory-method !--Spring提供的实例化方式第三种通过工厂方法模式。通过 factory-bean属性 factory-method属性来共同完成。-- !--告诉Spring框架调用哪个对象的哪个方法来获取Bean。-- bean idgunFactory classcom.powernode.spring6.bean.GunFactory/ !--factory-bean属性告诉Spring调用哪个对象。factory-method告诉Spring调用该对象的哪个方法。-- bean idgun factory-beangunFactory factory-methodget/编写测试程序 Test public void testInstantiation3(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);Gun gun applicationContext.getBean(gun, Gun.class);System.out.println(gun); }4.通过FactoryBean接口实例化 以上的第三种方式中factory-bean是我们自定义的factory-method也是我们自己定义的。 在Spring中当你编写的类直接实现FactoryBean接口之后factory-bean不需要指定了factory-method也不需要指定了。 factory-bean会自动指向实现FactoryBean接口的类factory-method会自动指向getObject()方法。 定义一个Bean package com.powernode.spring6.bean; /*** 普通的Bean*/ public class Person {public Person() {System.out.println(Person的无参数构造方法执行);} }编写一个类实现FactoryBean接口 package com.powernode.spring6.bean;import org.springframework.beans.factory.FactoryBean;public class PersonFactoryBean implements FactoryBeanPerson {// PersonFactoryBean也是一个Bean这个Bean比较特殊叫做工厂Bean// 通过工厂Bean可以获取普通的BeanOverridepublic Person getObject() throws Exception {// 最终这个Bean的创建还是我们自己new的return new Person();}Overridepublic Class? getObjectType() {return null;}/*** 这个方法在接口中有默认实现* 默认返回true表示单例的* 返回false表示多例的* return*/Overridepublic boolean isSingleton() {return FactoryBean.super.isSingleton();} }在Spring配置文件中配置FactoryBean !--Spring提供的实例化方式第四种通过FactoryBean接口来实现。-- !--这种方式实际上就是第三种方式的简化。-- !--由于你编写的类实现了FactoryBean接口所以这个类是一个特殊的类不需要你再手动指定factory-bean、factory-method-- !--通过一个特殊的Bean工厂Bean。来返回一个普通的Bean Person对象。-- !--通过FactoryBean这个工厂Bean主要是想对普通Bean进行加工处理。-- bean idperson classcom.powernode.spring6.bean.PersonFactoryBean/测试程序 Test public void testInstantiation4(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);Person person applicationContext.getBean(person, Person.class);System.out.println(person); }FactoryBean在Spring中是一个接口。被称为“工厂Bean”。“工厂Bean”是一种特殊的Bean。所有的“工厂Bean”都是用来协助Spring框架来创建其他Bean对象的。 5.BeanFactory和FactoryBean的区别 1 BeanFactory Spring IoC容器的顶级父接口BeanFactory被翻译为“Bean工厂”在Spring的IoC容器中“Bean工厂”负责创建Bean对象。 BeanFactory是工厂。 2 FactoryBean FactoryBean它是一个Bean是一个能够辅助Spring实例化其它Bean对象的一个Bean。 在Spring中Bean可以分为两类 第一类普通Bean第二类工厂Bean记住工厂Bean也是一种Bean只不过这种Bean比较特殊它可以辅助Spring实例化其它Bean对象。 6 注入自定义Date java.util.Date在Spring中被当做简单类型简单类型在注入的时候可以直接使用value属性或value标签来完成。但对于Date类型来说采用value属性或value标签赋值的时候对日期字符串的格式要求非常严格必须是这种格式的Mon Oct 10 14:30:26 CST 2022。其他格式是不会被识别的。 package com.powernode.spring6.bean;import java.util.Date;/*** 普通的Bean*/ public class Student {// java.util.Date 在Spring当中被当做简单类型注入日期字符串格式有要求// java.util.Date 在Spring当中也可以被当做非简单类型private Date birth;public void setBirth(Date birth) {this.birth birth;}Overridepublic String toString() {return Student{ birth birth };} }编写DateFactoryBean类实现FactoryBean接口 package com.powernode.spring6.bean;import org.springframework.beans.factory.FactoryBean;import java.text.SimpleDateFormat; import java.util.Date;public class DateFactoryBean implements FactoryBeanDate {// 定义属性接收日期字符串private String strDate;// 通过构造方法给日期字符串属性赋值public DateFactoryBean(String strDate) {this.strDate strDate;}Overridepublic Date getObject() throws Exception {SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);Date date sdf.parse(strDate);return date;}Overridepublic Class? getObjectType() {return null;} }在Spring配置文件中配置 !--通过工厂BeanDateFactoryBean来返回普通Bean:java.util.Date-- bean iddateFactoryBean classcom.powernode.spring6.bean.DateFactoryBeanconstructor-arg index0 value1999-10-11/ /bean bean idstudent classcom.powernode.spring6.bean.Studentproperty namebirth refdateFactoryBean/ /bean测试 Test public void testDate(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);Student student applicationContext.getBean(student, Student.class);System.out.println(student); } 文章转载自: http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.langlaitech.cn.gov.cn.langlaitech.cn http://www.morning.ygkq.cn.gov.cn.ygkq.cn http://www.morning.qbfkz.cn.gov.cn.qbfkz.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.zpfqh.cn.gov.cn.zpfqh.cn http://www.morning.nspzy.cn.gov.cn.nspzy.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.nslwj.cn.gov.cn.nslwj.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.xfmwk.cn.gov.cn.xfmwk.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.wlqll.cn.gov.cn.wlqll.cn http://www.morning.kbdjn.cn.gov.cn.kbdjn.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.zrjzc.cn.gov.cn.zrjzc.cn http://www.morning.qymqh.cn.gov.cn.qymqh.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.ygbq.cn.gov.cn.ygbq.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.brps.cn.gov.cn.brps.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.qxltp.cn.gov.cn.qxltp.cn 查看全文 http://www.tj-hxxt.cn/news/254132.html 相关文章: 做网站能挣钱不成都企业做网站 企业官网型网站模板下载朋友圈产品推广文案 网站建设自学多长时间洛阳百事通文化传播有限公司 在郑州网站推广网站服务费怎么做分录 常用外贸网站网站建设 考试题目 标志在线设计网站温州网站制作哪家好 怎么建设一个论坛网站安装wordpress到搭建服务器 有网站想修改里面的内容怎么做wordpress时间文件夹 泰州网站建设服务公司开发一个app软件能赚钱吗 怎么做个网站演示seo 网站title 石家庄网站seo模板和网站的区别 网站建设哪家wordpress 密码访问 网站 带数据网站备案报价 如何在网站添加代码网络公司网站建设服务 网站开发人员的 生活nginx 网站建设 未来对网站建设的需求做导师一般去什么网站找素材 搭建邮箱注册网站网站做接口怎么做 免费行情网站排名新乡做网站的公司 企业做电商网站有哪些内容小规模公司需要交哪些税 一般建站需要多少钱win10 做网站服务器吗 宁波公司建设网站网站名词 微信朋友圈广告在哪里做烟台优化网站公司哪家好 免费网站安全检测境外网站 备案 电影网站html模板江苏省建设集团有限公司网站 辽宁省网站制作网站建设需要的文案 花瓣官网设计网站网站建设报价比较 上海设计师网站有哪些wordpress设计主题 莱阳市规划建设局网站响应式网站弊端 印度网站域名还有什么网站可以做面包车拉货 德国服务器网站阿里云 wordpress 权限设置