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

西安建站平台苏州seo关键词优化方法

西安建站平台,苏州seo关键词优化方法,青岛房产网二手房,南京企业网站设计建设Spring为Bean提供了多种实例化方式,通常包括4种方式。(也就是说在Spring中为Bean对象的创建准备了多种方案,目的是:更加灵活) 第一种:通过构造方法实例化第二种:通过简单工厂模式实例化第三种&…

Spring为Bean提供了多种实例化方式,通常包括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 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"><!--Spring提供的实例化方式,第一种:在spring配置文件中直接配置类全路径,Spring会自动调用该类的无参数构造方法来实例化Bean--><bean id="springBean" class="com.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 id="star" class="com.powernode.spring6.bean.StarFactory" factory-method="get"/>

编写测试程序

@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 id="gunFactory" class="com.powernode.spring6.bean.GunFactory"/>
<!--factory-bean属性告诉Spring调用哪个对象。factory-method告诉Spring调用该对象的哪个方法。-->
<bean id="gun" factory-bean="gunFactory" factory-method="get"/>

编写测试程序

@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 FactoryBean<Person> {// PersonFactoryBean也是一个Bean,这个Bean比较特殊,叫做工厂Bean// 通过工厂Bean可以获取普通的Bean@Overridepublic 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 id="person" class="com.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 FactoryBean<Date> {// 定义属性接收日期字符串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配置文件中配置

<!--通过工厂Bean:DateFactoryBean来返回普通Bean:java.util.Date-->
<bean id="dateFactoryBean" class="com.powernode.spring6.bean.DateFactoryBean"><constructor-arg index="0" value="1999-10-11"/>
</bean>
<bean id="student" class="com.powernode.spring6.bean.Student"><property name="birth" ref="dateFactoryBean"/>
</bean>

测试:

@Test
public void testDate(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");Student student = applicationContext.getBean("student", Student.class);System.out.println(student);
}
http://www.hengruixuexiao.com/news/5645.html

相关文章:

  • 揭阳网站制作专业网站建设
  • 购物网站建设需求模板seo关键词排名工具
  • 做网站选什么系统深圳网络公司推广公司
  • 做网站(信科网络)网络营销平台推广方案
  • 可以做外链视频的网站关键词分类
  • 中国邮政做特产得网站嵌入式培训机构哪家好
  • 做调查报告的网站平台推广
  • 怎么做招聘网站的数据分析百度用户服务中心人工电话
  • 网站建设中 html5个人在线网站推广
  • 万网网站制作搜索图片识别出处百度识图
  • 有做国际网站生意吗seo排名优化是什么意思
  • 提供网站建设运营公司资质最好用的免费建站平台
  • 响应式网站企业厦门网站建设
  • 宝坻网站建设网站查询器
  • 做黑彩网站域名seo站长工具
  • 用网站ip做代理免费seo免费培训
  • 做公司网站,哪个程序用的多云南seo简单整站优化
  • 网站建设 博客抖音热门搜索关键词
  • 网站版块下载网络营销网站建设案例
  • 现在还有企业做网站吗上海牛巨微seo
  • 沈阳做企业网站站长seo综合查询工具
  • 口碑好的常州做网站推广平台有哪些?
  • 企业做网站需要准备什么资料全媒体运营师
  • 广告公司怎么宣传自己无锡百度seo优化
  • 青海网站开发重庆森林经典台词独白
  • 网站开发和网络工程师seo咨询服务价格
  • 创网络科技有限公司seo快速整站上排名教程
  • 重庆市住建局官方网站事件营销的案例有哪些
  • 佛山建设企业网站百度指数怎么下载
  • 湘潭做网站 去磐石网络网站模板哪家好