服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring中BeanFactory与FactoryBean接口的区别详解

Spring中BeanFactory与FactoryBean接口的区别详解

2021-07-19 09:18ゞ .邓澎波 Java教程

这篇文章主要给大家介绍了关于Spring中BeanFactory与FactoryBean接口的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

前言

spring框架中的beanfactory接口和factorybean接口因为名称相似,老是容易搞混淆,而且也是面试过程中经常会碰到的一个问题。所以本文就专门给大家整理出来。

一、beanfactory接口

beanfactory接口是spring容器的核心接口,负责:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。

Spring中BeanFactory与FactoryBean接口的区别详解

spring为我们提供了许多易用的beanfactory实现,xmlbeanfactory就是常用的一个,该实现将以xml方式描述组成应用的对象及对象间的依赖关系。xmlbeanfactory类将持有此xml配置元数据,并用它来构建一个完全可配置的系统或应用。

?
1
2
3
beanfactory bf = new xmlbeanfactory(new classpathresource("applicationcontext.xml"));
object bean = bf.getbean(iuserservice.class);
system.out.println(bean);

接口中定义的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public interface beanfactory {
 
 string factory_bean_prefix = "&";
 
 object getbean(string name) throws beansexception;
 
 <t> t getbean(string name, class<t> requiredtype) throws beansexception;
 
 object getbean(string name, object... args) throws beansexception;
 
 <t> t getbean(class<t> requiredtype) throws beansexception;
 
 <t> t getbean(class<t> requiredtype, object... args) throws beansexception;
 
 boolean containsbean(string name);
 
 boolean issingleton(string name) throws nosuchbeandefinitionexception;
 
 boolean isprototype(string name) throws nosuchbeandefinitionexception;
 
 boolean istypematch(string name, resolvabletype typetomatch) throws nosuchbeandefinitionexception;
 
 boolean istypematch(string name, class<?> typetomatch) throws nosuchbeandefinitionexception;
 
 class<?> gettype(string name) throws nosuchbeandefinitionexception;
 
 string[] getaliases(string name);
}

二、factorybean接口

beanfactory接口是spring的核心接口。功能非常复杂,这个时候如果我们想要编写一些比较复杂点儿的逻辑就会触及到其他一些不必要的接口,不好实现。这时候使用factorybean就比较方便了。factorybean以bean结尾是个bean对象,不是工厂。接口中定义的方法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public interface factorybean<t> {
 
 /**
  * 返回对象的实例
  */
 t getobject() throws exception;
 
 /**
  * 返回对象的类型
  */
 class<?> getobjecttype();
 
 /**
  * 是否是单例
  */
 boolean issingleton();
}

1.简单实现

接口和实现类

?
1
2
3
4
public interface iuserservice {
 
 public void dosome();
}
?
1
2
3
4
5
6
7
8
9
10
public class userserviceimpl implements iuserservice {
 public userserviceimpl(){
  system.out.println("--被实例化了--");
 }
 
 @override
 public void dosome() {
  system.out.println("userserviceimpl 。。。 被执行了");
 }
}

自定义factorybean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class myfactorybean implements factorybean<iuserservice>{
 
 @override
 public iuserservice getobject() throws exception {
  system.out.println("--iuserservice实例化之前---");
  iuserservice service = new userserviceimpl();
  system.out.println("--iuserservice实例化后---");
  return service;
 }
 
 @override
 public class<?> getobjecttype() {
  return iuserservice.class;
 }
 
 @override
 public boolean issingleton() {
  return true;
 }
}

配置文件

?
1
2
3
4
5
6
7
<?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">
 <bean id="myfactorybean" class="com.dpb.factorybean.myfactorybean"/>
</beans>

测试--通过类型获取

?
1
2
3
4
5
6
@test
public void test1() {
 beanfactory bf = new xmlbeanfactory(new classpathresource("applicationcontext.xml"));
 object bean = bf.getbean(iuserservice.class);
 system.out.println(bean);
}

输出结果

--iuserservice实例化之前---
--被实例化了--
--iuserservice实例化后---
com.dpb.service.userserviceimpl@5315b42e

测试--通过id获取

?
1
2
3
4
5
6
@test
public void test1() {
 beanfactory bf = new xmlbeanfactory(new classpathresource("applicationcontext.xml"));
 object bean = bf.getbean("myfactorybean");
 system.out.println(bean);
}

输出结果

--iuserservice实例化之前---
--被实例化了--
--iuserservice实例化后---
com.dpb.service.userserviceimpl@783e6358

如果想要获取factorybean对象 id前加 &就可以

?
1
2
3
4
5
6
@test
public void test1() {
 beanfactory bf = new xmlbeanfactory(new classpathresource("applicationcontext.xml"));
 object bean = bf.getbean("&myfactorybean");
 system.out.println(bean);
}

输出结果

com.dpb.factorybean.myfactorybean@3b81a1bc

2.增强实现

通过factorybean创建一个代理类来增强目标类,我们来看下效果

接口和实现类

?
1
2
3
4
public interface iuserservice {
 
 public void dosome();
}
?
1
2
3
4
5
6
7
8
9
10
public class userserviceimpl implements iuserservice {
 public userserviceimpl(){
  system.out.println("--被实例化了--");
 }
 
 @override
 public void dosome() {
  system.out.println("userserviceimpl 。。。 被执行了");
 }
}

自定义factorybean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class myfactorybean implements factorybean,initializingbean,disposablebean{
 
 private object proxyobject;
 
 private object target;
 
 private string interfacename;
 
 @override
 public object getobject() throws exception {
  
  return proxyobject;
 }
 
 @override
 public class<?> getobjecttype() {
  return proxyobject.getclass()==null?object.class:proxyobject.getclass();
 }
 
 @override
 public boolean issingleton() {
  return true;
 }
 
 /**
  * myfactorybean 对象销毁的回调方法
  * @throws exception
  */
 @override
 public void destroy() throws exception {
  system.out.println("destroy ....");
  
 }
 
 /**
  * myfactorybean 对象实例化的方法
  */
 @override
 public void afterpropertiesset() throws exception {
  system.out.println("---afterpropertiesset---");
  proxyobject = proxy.newproxyinstance(
     this.getclass().getclassloader()
     , new class[]{class.forname(interfacename)}
     , new invocationhandler() {
      
      @override
      public object invoke(object proxy, method method, object[] args) throws throwable {
       system.out.println("----代理方法执行开始----");
       object obj = method.invoke(target, args);
       system.out.println("----代理方法执行结束----");
       return obj;
      }
     });
 }
 
 public string getinterfacename() {
  return interfacename;
 }
 
 public void setinterfacename(string interfacename) {
  this.interfacename = interfacename;
 }
 
 public object gettarget() {
  return target;
 }
 
 public void settarget(object target) {
  this.target = target;
 }
}

配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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">
 <!-- 注册目标对象 -->
 <bean class="com.dpb.service.userserviceimpl" id="userserviceimpl"/>
 <!-- 注册factorybean对象 -->
 <bean id="myfactorybean" class="com.dpb.factorybean.myfactorybean">
  <property name="interfacename" value="com.dpb.service.iuserservice"/>
   <property name="target" ref="userserviceimpl"/>
 </bean>
</beans>

测试

?
1
2
3
4
5
6
7
8
@test
public void test1() {
 applicationcontext ac = new classpathxmlapplicationcontext("applicationcontext.xml");
 iuserservice bean = (iuserservice) ac.getbean("myfactorybean");
 system.out.println("****************");
 bean.dosome();
 system.out.println();
}

输出结果:

--被实例化了--
---afterpropertiesset---
****************
----代理方法执行开始----
userserviceimpl 。。。 被执行了
----代理方法执行结束----

小结:通过输出结果我们可以看到通过factorybean接口我们也可以实现beanfactory中某些接口提供的功能,而且会更加的灵活一些。

3.factorybean的实际使用案例

spring在整合mybatis框架的时候提供的sqlsessionfactorybean就是factorybean的很好的实现。

?
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 整合mybatis -->
<bean class="org.mybatis.spring.sqlsessionfactorybean"
id="sqlsessionfactorybean" >
 <!-- 关联数据源 -->
 <property name="datasource" ref="datasource"/>
 <!-- 关联mybatis的配置文件 -->
 <property name="configlocation" value="classpath:mybatis-cfg.xml"/>
 <!-- 添加别名设置 -->
 <property name="typealiasespackage" value="com.sxt.model"/>
 <!-- 映射文件和接口文件不在同一个目录下的时候,需要单独指定映射文件的路径 -->
 <property name="mapperlocations" value="classpath:mapper/*.xml"/>
</bean>

spring会调用sqlsessionfactorybean这个实现了factorybean的工厂bean 同时加载datasource,mapper文件的路径,对sqlsessionfactory进行初始化。

Spring中BeanFactory与FactoryBean接口的区别详解

源代码比较多就不一一贴出来。到了这儿可以自行跟踪下源代码。

核心方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@override
 public void afterpropertiesset() throws exception {
 // 省略
 this.sqlsessionfactory = buildsqlsessionfactory();
 }
 
 protected sqlsessionfactory buildsqlsessionfactory() throws ioexception {
 
 configuration configuration;
 
 xmlconfigbuilder xmlconfigbuilder = null;
 // 省略
 return this.sqlsessionfactorybuilder.build(configuration);
 }
/**
 * {@inheritdoc}
 */
 @override
 public sqlsessionfactory getobject() throws exception {
 if (this.sqlsessionfactory == null) {
  afterpropertiesset();
 }
 
 return this.sqlsessionfactory;
 }
 
 /**
 * {@inheritdoc}
 */
 @override
 public class<? extends sqlsessionfactory> getobjecttype() {
 return this.sqlsessionfactory == null ? sqlsessionfactory.class : this.sqlsessionfactory.getclass();
 }
 
 /**
 * {@inheritdoc}
 */
 @override
 public boolean issingleton() {
 return true;
 }

maven坐标:

?
1
2
3
4
5
<dependency>
 <groupid>org.mybatis</groupid>
 <artifactid>mybatis-spring</artifactid>
 <version>1.3.2</version>
</dependency>

三、总结

  • beanfactory是spring中ioc容器最核心的接口,遵循了ioc容器中所需的基本接口。例如我们很常见的:applicationcontext,xmlbeanfactory 等等都使用了beanfactory这个接口。
  • factorybean是工厂类接口,当你只是想简单的去构造bean,不希望实现原有大量的方法。它是一个bean,不过这个bean能够做为工厂去创建bean,同时还能修饰对象的生成。
  • factorybean比beanfactory在生产bean的时候灵活,还能修饰对象,带有工厂模式和装饰模式的意思在里面,不过它的存在还是以bean的形式存在。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。

原文链接:https://www.cnblogs.com/dengpengbo/p/10493782.html

延伸 · 阅读

精彩推荐