使用 Java 构建路径将 Spring 项目添加到 Spring Boot
Posted
技术标签:
【中文标题】使用 Java 构建路径将 Spring 项目添加到 Spring Boot【英文标题】:Add Spring Project to Spring Boot using Java Built Path 【发布时间】:2018-06-24 01:16:04 【问题描述】:我有一个连接MongoDB的项目,配置如下:
@Configuration
@ComponentScan(basePackages = "ups.mongo")
@EnableMongoRepositories( "ups.mongo.repository" )
public class SpringMongoConfig
@Bean
public MongoDbFactory mongoDbFactory() throws Exception
return new SimpleMongoDbFactory(new MongoClient("127.0.0.1"), "ReconInput");
@Bean
public MongoTemplate mongoTemplate() throws Exception
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
另一个项目是基于 Spring boot 构建的,配置了 Spring 安全性:
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter
@Autowired
private AccessDeniedHandler accessDeniedHandler;
@Autowired
private AuthenticateSuccessHandler successHandler;
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder()
return new BCryptPasswordEncoder();
@Override
protected void configure(HttpSecurity http) throws Exception
http.csrf().disable()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/admin/**").hasAuthority("Admin")
.antMatchers("/common/**").hasAnyAuthority("Admin","User")
.antMatchers("/home").hasAnyAuthority("Admin","User")
.and()
.formLogin()
.loginPage("/login")
.successHandler(successHandler).permitAll()
.and()
.logout().permitAll()
.and()
.exceptionHandling()
.accessDeniedHandler(accessDeniedHandler);
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
Spring Boot pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Pros</name>
<description>Demo</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Mongo 项目 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ups.mongodb</groupId>
<artifactId>MongoConnection-Final</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MongoConnection-Final</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>$spring.version</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>$spring.version</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
</project>
如果我在没有从 Spring Boot App 到 Mongo Connection Project 的情况下一一运行,它可以正常工作。相反,它会抛出一个错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“springSecurityConfig”的 bean 时出错:不满意 通过方法“setContentNegotationStrategy”表达的依赖关系 参数0;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名称为 'mvcContentNegotiationManager' 的 bean 类路径资源 [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [org.springframework.web.accept.ContentNegotiationManager]:工厂 方法“mvcContentNegotiationManager”抛出异常;嵌套的 例外是 java.lang.AbstractMethodError
谁能给我一些建议或经验?
非常感谢!
【问题讨论】:
你是如何从 Spring Boot 应用程序中引用 Mongo Connection 项目的?我有一个使用直接弹簧应用程序的具有默认安全性的弹簧启动应用程序。我在 spring boot 应用程序的依赖项部分列出了 spring 应用程序。没有更多细节我很难说。 【参考方案1】:我发现问题来自Spring boot和Spring之间的版本冲突。
在 Spring boot 中,spring 的版本是自动设置的。例如这里,我使用的是 Spring Boot 版本 1.5.9.RELEASE。那么spring版本将是4.3.13。
但在 Mongo 项目中,我设置了 spring 版本 5.0.1.RELEASE。然后问题发生了。
[解决方案] 要将 Spring Boot 引用到另一个 Spring 项目,只需确保 Spring 版本兼容(相同版本)即可。
【讨论】:
以上是关于使用 Java 构建路径将 Spring 项目添加到 Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章
ivy或ivyDE能否在Eclipse中自动将相关jar添加到JAVA构建路径库中?
Visual Studio Code,Java 扩展,如何将 jar 添加到类路径
如何在不面对 java.lang.ClassNotFoundException 的情况下将 JAR 库添加到 WAR 项目?类路径 vs 构建路径 vs /WEB-INF/lib