当前位置: 首页 > news >正文

杭州旅游团购网站建设dw做网站的流程

杭州旅游团购网站建设,dw做网站的流程,思帽西宁网站建设,在哪买网站链接目录 一#xff1a;Bean的实例化方式 1. 通过构造方法实例化 2. 通过简单工厂模式实例化 3. 通过factory-bean实例化 4. 通过FactoryBean接口实例化 5. BeanFactory和FactoryBean的区别#xff08;面试题#xff09; 6. 使用FactoryBean注入自定义Date 一#xff1a…目录 一Bean的实例化方式 1. 通过构造方法实例化 2. 通过简单工厂模式实例化 3. 通过factory-bean实例化 4. 通过FactoryBean接口实例化 5. BeanFactory和FactoryBean的区别面试题 6. 使用FactoryBean注入自定义Date 一Bean的实例化方式 Spring为Bean提供了多种实例化方式通常包括4种方式。也就是说在Spring中为Bean对象的创建准备了多种方案目的是更加灵活 第一种通过构造方法实例化 第二种通过简单工厂模式实例化 第三种通过factory-bean实例化工厂方法模式 第四种通过FactoryBean接口实例化 1. 通过构造方法实例化 我们之前一直使用的就是这种方式默认情况下会调用Bean的无参数构造方法这里在复习一遍 SpringBean类 package com.bjpowernode.spring.bean;public class SpringBean {public SpringBean() {System.out.println(SpringBean的无参数构造方法执行了);} }spring.xml配置 第一种在spring配置文件中直接配置类全路径Spring会自动调用该类的无参数构造方法来实例化Bean ?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提供的实例化方式第一种--bean idsb classcom.bjpowernode.spring.bean.SpringBean/ /beans BeanInstantiationTest测试类 package com.bjpowernode.spring.test;import com.bjpowernode.spring.bean.SpringBean; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanInstantiationTest {Testpublic void tesInstantiation1(){ClassPathXmlApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);SpringBean sb applicationContext.getBean(sb, SpringBean.class);System.out.println(sb);} }执行结果成功调用无参数构造方法实例化对象 2. 通过简单工厂模式实例化 简单工厂模式又叫做静态工厂方法模式因为工厂类中有一个静态方法 第一步定义一个Bean package com.bjpowernode.spring.bean;public class Vip {public Vip() {System.out.println(我是一个Vip);} }第二步编写简单工厂模式当中的工厂类 package com.bjpowernode.spring.bean;public class VipFactory {// 里面有一个静态方法public static Vip get(){// 实际上对象的创建还是我们程序员自己完成的return new Vip();} }第三步在Spring配置文件中指定创建该Bean的方法 第二种通过简单工厂模式。 需要在Spring配置文件中告诉Spring框架调用哪个类的哪个方法获取Bean ①class属性指定的是工厂类的全限定类名 ②factory-method属性指定的是工厂类当中的静态方法也就是告诉Spring框架调用这个方法可以获取Bean ?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提供的实例化方式第二种--bean idvipBean classcom.bjpowernode.spring.bean.VipFactory factory-methodget/ /beans 第四步编写测试程序 package com.bjpowernode.spring.test;import com.bjpowernode.spring.bean.SpringBean; import com.bjpowernode.spring.bean.Vip; import com.bjpowernode.spring.bean.VipFactory; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanInstantiationTest {Testpublic void tesInstantiation2(){ClassPathXmlApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);Object vipBean applicationContext.getBean(vipBean, Vip.class);System.out.println(vipBean);} }执行结果通过简单工厂模式也能实例化对象 3. 通过factory-bean实例化 本质上是通过工厂方法模式进行实例化对象 注简单工厂模式和工厂方法模式的区别 ①简单工厂模式是所有的产品对应一个工厂类使用的是静态方法 ②工厂方法模式是一个产品对应一个工厂类使用的是实例方法 第一步定义一个Bean package com.bjpowernode.spring.bean; // 工厂方法模式当中的具体产品角色 public class Gun {public Gun() {System.out.println(Gun的无参数构造方法执行);} }第二步定义具体工厂类工厂类中定义实例方法 package com.bjpowernode.spring.bean; // 工厂方法模式当中的具体工厂角色 public class GunFactory {// 实例方法public Gun get(){// 还是我们自己new的对象return new Gun();}}第三步在Spring配置文件中指定factory-bean以及factory-method 第三种通过工厂方法模式。 通过 factory-bean属性 factory-method属性来共同完成。告诉Spring框架调用哪个对象因为是实例方法需要创建对象的哪个方法来获取Bean。 ①factory-bean属性用来告诉Spring调用那个对象 ②factory-method属性用来告诉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提供的实例化方式第三种--bean idgunBean classcom.bjpowernode.spring.bean.GunFactory/bean idgun factory-beangunBean factory-methodget/ /beans 第四步编写测试程序 package com.bjpowernode.spring.test;import com.bjpowernode.spring.bean.Gun; import com.bjpowernode.spring.bean.SpringBean; import com.bjpowernode.spring.bean.Vip; import com.bjpowernode.spring.bean.VipFactory; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanInstantiationTest {Testpublic void tesInstantiation3(){ClassPathXmlApplicationContext 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.bjpowernode.spring.bean;public class Person {public Person() {System.out.println(Person的无参数构造方法执行了);} }第二步编写一个类实现FactoryBean接口重写里面的方法 PersonFactory也是一个Bean只不过这个Bean比较特殊叫做工厂Bean。通过工厂Bean这个特殊的Bean可以获取一个普通的Bean package com.bjpowernode.spring.bean;import org.springframework.beans.factory.FactoryBean;public class PersonFactory implements FactoryBeanPerson {Overridepublic Person getObject() throws Exception {// 对象的创建也是自己new的return new Person();}Overridepublic Class? getObjectType() {return null;}Overridepublic boolean isSingleton() {// 这个方法是默认存在的true表示单例false表示原型return true;} }第三步在Spring配置文件中配置FactoryBean 第四种通过FactoryBean接口来实现这种方式实际上就是第三种方式的简化 ①由于你编写的类实现了FactoryBean接口所以这个类是一个特殊的类不需要你再手动指定factory-bean、factory-method。 ②通过一个特殊的Bean工厂Bean来返回一个普通的Bean Person对象。即通过FactoryBean这个工厂Bean主要是想对普通Bean进行加工处理 ?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提供的实例化方式第四种--bean idperson classcom.bjpowernode.spring.bean.PersonFactory / /beans 第四步编写测试程序 package com.bjpowernode.spring.test;import com.bjpowernode.spring.bean.*; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanInstantiationTest {Testpublic void tesInstantiation4(){ClassPathXmlApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);Person person applicationContext.getBean(person, Person.class);System.out.println(person);} }执行结果通过FactoryBean接口实例化 注FactoryBean在Spring中是一个接口被称为“工厂Bean”。“工厂Bean”是一种特殊的Bean所有的“工厂Bean”都是用来协助Spring框架来创建其他Bean对象的 5. BeanFactory和FactoryBean的区别面试题 1BeanFactory是一个工厂 BeanFactory是Spring IoC容器的顶级对象BeanFactory被翻译为“Bean工厂”在Spring的IoC容器中“Bean工厂”负责创建Bean对象 2FactoryBean是一个Bean FactoryBean是一个Bean是一个能够辅助Spring实例化其它Bean对象的一个Bean 在Spring中Bean可以分为两类 第一类普通Bean第二类工厂Bean工厂Bean也是一种Bean只不过这种Bean比较特殊它可以辅助Spring实例化其它Bean对象6. 使用FactoryBean注入自定义Date ①前面我们说过java.util.Date在Spring中被当做简单类型简单类型在注入的时候可以直接使用value属性或value标签来完成。 ②但是之前我们已经测试过了对于Date类型来说采用value属性或value标签赋值的时候对日期字符串的格式要求非常严格必须是这种格式的Mon Oct 10 14:30:26 CST 2022其他格式是不会被识别的 ③当然我们也可以当成非简单类型处理使用ref属性来处理但是却有一个弊端获取的都是当前的时间并不能自己指定时间 注下面我们就使用FactoryBean来完成这个骚操作 Student类 package com.bjpowernode.spring.bean;import java.util.Date;public class Student {// 每个学生都有出生日期private Date birth;Overridepublic String toString() {return Student{ birth birth };}public void setBirth(Date birth) {this.birth birth;}}编写DateFactory实现FactoryBean接口 package com.bjpowernode.spring.bean;import org.springframework.beans.factory.FactoryBean;import java.text.SimpleDateFormat; import java.util.Date;public class DateFactory implements FactoryBeanDate {// 定义一个日期属性用来处理传过来的日期字符串private String date;// 通过构造方法给日期字符串属性赋值public DateFactory(String date) {this.date date;}Overridepublic Date getObject() throws Exception {// 处理SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);return sdf.parse(this.date);}Overridepublic Class? getObjectType() {return null;} }编写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!--通过这个类的构造方法把字符串转换成Date--bean iddate classcom.bjpowernode.spring.bean.DateFactoryconstructor-arg namedate value1999-01-14//bean!--把上面的Date通过上面的类使用ref属性引进来--bean idstudentBean classcom.bjpowernode.spring.bean.Studentproperty namebirth refdate//bean /beans 编写测试程序 package com.bjpowernode.spring.test;import com.bjpowernode.spring.bean.*; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanInstantiationTest {Testpublic void testDate(){ClassPathXmlApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);Student studentBean applicationContext.getBean(studentBean, Student.class);System.out.println(studentBean);} }执行结果
http://www.hkea.cn/news/14384644/

相关文章:

  • seo站长常用工具公司网站SEO优化哪个做得好
  • 私人网站开发公司制作app开发制作
  • wordpress如何开发手机扬中网站优化哪家好
  • 山海关城乡建设局网站建设网站目标
  • 建设项目环境影响网站搜索引擎优化举例说明
  • 湛江手机网站建设公司游戏 网站 模板
  • 百度搜索网站图片国外小型网站
  • 响应式网站是指自适应吗互联网行业怎么赚钱
  • 项目推广方案怎么写重庆seo整站优化
  • 外贸网站建设青岛百安居装修口碑怎么样
  • 联想官方服务网站推广网站的网址和网鱼相匹配
  • 通付盾 建设公司网站国外网站建设嫣语赋
  • 网站是不是每年都要续费企业网站排名优化方案
  • 经营网站建设做论坛网站4g空间够不够用
  • 物流网站开发实训视频做网站
  • 做片头网站响应式网站建设外文文献
  • 最简单的网站怎么做中国移动手机支付网站
  • 国外优秀建筑设计网站服装网站设计欣赏
  • 靖安县城乡规划建设局网站html网页模板网站模板下载
  • 西充县建设路小学网站哪个网站卖自己做的手工艺品
  • 公司网站建设多少费用兴田德润在哪里网站建设又叫什么
  • 手机存储wordpress海淀区seo搜索优化
  • 网站主页特效欣赏17做网站广州
  • 如何做网站建设方案网站备案查询 美橙
  • 手机高端网站开发商场设计平面图
  • 网站样版风格排版网站制作网站建站
  • 哪个网站专门做灵异文网络营销的定义与特点
  • 天津网站建设定做内蒙古建设厅安全资料网站
  • 微网站appxz域名网站
  • 做网站销售怎么找客户免费网站后台管理系统模板下载