在使用Spring Boot的过程中,很多人发现官方给的例子中,Maven配置Parent为
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent>
你会发现很多以来在parent当中,这给很多企业内部有自身的parent带来了不便.
不过没关系,spring 官方给出了相关的解决办法.
参考:using-boot-maven-without-a-parent
也就是在你相关的工程中,在pom文件添加:
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
这段,中版本号请根据最新的修改,
type是pom,scope为import。
这样你就相当于能使用这个pom中的依赖了,也就是利用maven的依赖管理引入其他的依赖管理进行解决的。– ps:maven还是很强大的。
这个时候你的parent还是按照你所想的那么去写就行了.
不过少部分人会遇到这样的情况.
项目启动报如下错误:
Caused by: java.lang.IllegalAccessException: Class org.springframework.core.io.support.SpringFactoriesLoader can not access a member of class org.springframework.boot.autoconfigure.condition.OnClassCondition with modifiers "" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102) ~[na:1.8.0_60] at java.lang.Class.newInstance(Class.java:436) ~[na:1.8.0_60] at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:135) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] ... 28 common frames omitted
Caused by: java.lang.IllegalArgumentException: Cannot instantiate factory class: org.springframework.boot.autoconfigure.AutoConfigurationImportFilter at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:138) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.core.io.support.SpringFactoriesLoader.loadFactories(SpringFactoriesLoader.java:91) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getAutoConfigurationImportFilters(AutoConfigurationImportSelector.java:279) ~[spring-boot-autoconfigure-1.5.1.RELEASE.jar:1.5.1.RELEASE] at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.filter(AutoConfigurationImportSelector.java:250) ~[spring-boot-autoconfigure-1.5.1.RELEASE.jar:1.5.1.RELEASE] at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:101) ~[spring-boot-autoconfigure-1.5.1.RELEASE.jar:1.5.1.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:453) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] ... 23 common frames omitted
这个主要是因为你自身的parent中或者当前的pom中spring的版本出现了差异。
修改相关的版本号即可解决问题.一定要一样.