当前位置: 移动技术网 > IT编程>开发语言>Java > Spring实战之XML与JavaConfig的混合配置详解

Spring实战之XML与JavaConfig的混合配置详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

前言

之前提到了关于spring的显示配置方式有两种,一种是基于xml配置,一种是基于javaconfig的方式配置。对于这两种配置方式并不是互斥关系,相反,他们两能够相互融合,有效的搭配完成spring的bean注入。

这里分别介绍如何在javaconfig中引用xml配置的bean以及如何在xml配置中引用javaconfig。下面话不多说,来一起看看详细的介绍吧。

一、javaconfig中引用xml配置的bean

上篇我们看到配置类cdplayerconfig具体代码如下

@configuration
public class cdplayerconfig {
 
 @bean
 public compactdisc compactdisc() {
 return new sgtpeppers();
 }

 @bean
 public cdplayer cdplayer(compactdisc compactdisc) {
 return new cdplayer(compactdisc);
 }

}

在该配置类中注册了两个bean。假设让cdplayerconfig现在矫情一把,它认为自己内部注册的bean太多了,它有点管不过来了(确实有点矫情过了),它准备把compactdisc这个bean踢出去,让别人来管管。

让谁管呢,这时候我们又新建了另外一个配置类cdconfig

@configuration
public class cdconfig {
 @bean
 public compactdisc compactdisc() {
 return new sgtpeppers();
 }
}

这个配置类简洁到不行,只有一个compactdisc的bean。

现在有了这两个配置类,也集齐了两个bean,那么该如何把他们整合到一起呢,建立起它们的联系。这时候我们新建一个连接类soundsystemconfig

@configuration
@import(cdplayerconfig.class, cdconfig.class)
public class soundsystemconfig {

}

这样就解决了在一个配置类中要加载的bean过多的问题。

那么问题来了,如果这时候compactdisc接口的实现类blankdisc是声明在xml中,这两种显示声明又会如何结合呢。

public class blankdisc implements compactdisc {

 private string title;
 private string artist;
 private list<string> tracks;

 public blankdisc(string title, string artist, list<string> tracks) {
 this.title = title;
 this.artist = artist;
 this.tracks = tracks;
 }

 public void play() {
 system.out.println("playing " + title + " by " + artist);
 for (string track : tracks) {
  system.out.println("-track: " + track);
 }
 }

}

具体该类在xml中配置如下

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:c="http://www.springframework.org/schema/c"
 xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="compactdisc"
  class="soundsystem.blankdisc"
  c:_0="sgt. pepper's lonely hearts club band"
  c:_1="the beatles">
 <constructor-arg>
  <list>
  <value>sgt. pepper's lonely hearts club band</value>
  <value>with a little help from my friends</value>
  <value>lucy in the sky with diamonds</value>
  <value>getting better</value>
  <value>fixing a hole</value>
  <!-- ...other tracks omitted for brevity... -->
  </list>
 </constructor-arg>
 </bean>

</beans>

注意:这里的c:_0c:_1表示分别要传入到构造函数中的第一个参数和第二个参数的值,写法等价于

<bean id="compactdisc"
  class="soundsystem.blankdisc">
<constructor-arg value="sgt. pepper's lonely hearts club band"/>
<constructor-arg value="the beatles">
</bean>

或者

<bean id="compactdisc"
  class="soundsystem.blankdisc"
  c:_title="sgt. pepper's lonely hearts club band"
  c:_artist="the beatles"/>

现在blankdisc已经配置在xml中,那么spring如何去加载这个xml配置文件读取到这个配置信息并加载呢。这里我们可以通过一个注解@importresource来完成这个操作。假设上面关于blankdisc类的配置信息存放在cd-config.xml文件中,该文件在项目的根路径下。通过修改soundsystemconfig即可完成加载bean。

@configuration
@import(cdplayerconfig.class)
@importresource("classpath:cd-config.xml")
public class soundsystemconfig {

}

如上所示,一个是通过javaconfig配置的cdplayer,一个是通过xml声明的blankdisc,通过如上处理,实现了都能被spring容器加载。

二、xml配置中引用javaconfig

cdplayerconfig彻底的矫情了一把,xml配置文件表示不服,cdplayerconfig受不了两个bean,那我就能受得了?(好好好,朕特批xml矫情一次)

其实我们都知道,在xml中声明两个bean不是什么大问题,即便受不了,我们还有一个这种方案,xml中也可以使用import标签实现导入其他的xml配置文件并导入配置信息,好比这样的

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:c="http://www.springframework.org/schema/c"
 xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <import resource="cd-config.xml">

 <bean id="cdplayer"
  class="soundsystem.cdplayer"
  c:cd-ref="compactdisc" />
  
</beans>

注意:这里的c:cd-ref="compactdisc"等价于添加了属性<constructor-arg ref="compactdisc"

但是xml认为这样它还是受不了,它将<import resource="cd-config.xml">换成了<bean class="soundsystem.cdconfig" /> 。这下畅快多了,因为这样就实现了两种配置的结合了,一种是配置在javaconfig中的blankdisc,还有一种是在xml中配置的cdplayer。

测试

分别对于两种方式编写ut测试如下

importjavaconfigtest

@runwith(springjunit4classrunner.class)
@contextconfiguration("classpath:cdplayer-config.xml")
public class importjavaconfigtest {

 @rule
 public final standardoutputstreamlog log = new standardoutputstreamlog();

 @autowired
 private mediaplayer player;


 @test
 public void play() {
 player.play();
 assertequals(
  "playing sgt. pepper's lonely hearts club band by the beatles\r\n",
  log.getlog());
 }

}

importxmlconfigtest

@runwith(springjunit4classrunner.class)
@contextconfiguration(classes=soundsystemconfig.class)
public class importxmlconfigtest {

 @rule
 public final standardoutputstreamlog log = new standardoutputstreamlog();

 @autowired
 private mediaplayer player;


 @test
 public void play() {
 player.play();
 assertequals(
  "playing sgt. pepper's lonely hearts club band by the beatles\r\n" +
  "-track: sgt. pepper's lonely hearts club band\n\n" +
  "-track: with a little help from my friends\n\n" +
  "-track: lucy in the sky with diamonds\n\n" +
  "-track: getting better\n\n" +
  "-track: fixing a hole\n\n",
  log.getlog());
 }

}

测试结果


至此,我们领略了

       •javaconfig中引用xml配置的bean

       •xml配置中引用javaconfig

这两种显示配置bean如何融洽的配合起来。

总结

以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网