SpringBoot实践-导入jar包中的配置文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012668018/article/details/79031060

事件起因:开发的项目需要依赖一个本地jar包,无法通过maven直接下载;而这个jar包中含有配置文件,在配置文件中定义了spring的bean,而我的项目需要用到这个bean。

开发工具:IDEA

JDK版本:1.7

使用框架:SpringBoot、SpringMVC

解决过程:

1、首先需要在项目中引入这个本地的jar包,而且还要保持maven的pom.xml文件的一致与整洁。maven提供了一种方案来引入本地jar包,而不是从仓库下载。当然,也可以自己建立一个maven私服,将jar包上传到私服仓库,然后共享给大家,但是有点麻烦。现在来描述第一种思路。

(1)在项目的根目录下新建一个libs目录,跟src目录平级;然后将jar复制到libs目录。

(2)在pom.xml文件中添加依赖,如下:

<dependency>
			<groupId>com.credit</groupId>
			<artifactId>xx</artifactId>
			<version>1.0</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/libs/xx-1.0.0.jar</systemPath>
</dependency>

因为是本地jar包,所以坐标(groupId、artifactId和version)可以随便写,最重要的是后面两个属性scope和systemPath,scope的值必须是system,表明是外部jar包(scope更多介绍参见这里);而systemPath则指明了jar包在文件系统的绝对路径。那么这个jar放在那个位置比较方便呢?显然,jar的开发环境和发布环境不是同一台机器,绝对路径不一定相同。所以最简洁安全的办法就是直接把这个jar包存放在项目中,然后利用项目的相对路径——不管到时项目本身部署在哪个文件系统的哪个位置,只要项目的根目录确定了,jar包的位置也就确定了。这就是开头要在项目的根目录下新建一个libs目录的原因,而systemPath的值使用一个变量${project.basedir},它代表的就是项目根目录。

(3)添加依赖之后,还需要添加几个参数以便在打包的时候能将本地的这几个jar包包括进去,不然运行的时候会报错,说找不到这个jar包,我也是后来发现的:

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<includeSystemScope>true</includeSystemScope>
				</configuration>
			</plugin>
		</plugins>
</build>
核心就是这个includeSystemScope参数。

2、有了jar之后,就有了xml文件,现在需要在代码中引用定义在xml中的bean,如下:

private static IEventPublisher eventPublisher;

@Autowired
    public void setEventPublisher(IEventPublisher eventPublisher) {
        EmailUtil.eventPublisher = eventPublisher;
    }
顺便提一下,我这边需要将这个bean引用设置为static的,这是spring框架的其中一种注入静态变量的方式,更多方式参见 这里

然而,编译器却提示我“Could not autowire. No beans of 'IEventPublisher' type found”。那么显然了,xml文件没有被加载,或者无法读取到。于是我就想尽各种办法让spring去读取这个位于jar包中的xml:

(1)使用@ImportResource注解,然而并没有什么卵用。况且还有一个问题,这个xml的路径怎么去写?这样:

@ImportResource(value = "classpath*:xx.xml")
那是不行滴。

(2)为什么不行?是不是因为maven打包的时候没有把资源文件加进去?于是我按照往上众多教程开始鼓捣pom.xml文件:

<build>
    <plugins>
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <configuration>
             <compilerArguments>
               <extdirs>${project.basedir}/libs</extdirs>
             </compilerArguments>
           </configuration>
        </plugin>
    </plugins>
</build>
仍旧不行。根据我的判断, @ImportResource注解只能导入项目根目录下的配置文件,但是对jar包中的文件是无能为力的。

(3)于是我想到了springboot是鼓励去xml化的,所以只能废弃掉原来的xml,而在项目内部以springBoot的方式重建这个xml的内容:

@Configuration
public class Config {
  @Bean(name = "emailEventPublisher")
    public IEventPublisher eventPublisher() {
        EmailEventPublisher eventPublisher = new EmailEventPublisher();
        eventPublisher.setJmsTemplate(jmsTemplate());
        eventPublisher.setDestination(activeMQQueue());
        return eventPublisher;
    }
}
就这样,搞定了。看着简单,可是费了我两天时间。。看来还需要继续深化对maven、spring、springboot的研究。










猜你喜欢

转载自blog.csdn.net/u012668018/article/details/79031060