我将 spring 靴集成到我现有的 spring 项目中时遇到了问题 . 我有树问题,但在我谈到这些之前,让我解释一下我目前的架构:
Current project
我有两个项目 . 第一个被称为 myProject.toolkit
并且只有一个主要的类,我实现了一个无尽的do-while循环 . 此循环创建 ProcessBuilder
以启动新进程 . 当此过程终止时,它会检查是否需要此出口 . 如果是这样,它就会终止 . 如果没有,它会再次启动相同的过程(不是很漂亮,但我仍在学习,也许我将来会有更好的想法) .
第二个项目名为 myProject.dependencies
,这是将从循环内的工具包启动的程序 . 这也是真正的项目,因为工具包只有监控它的目的 .
这两个项目都是使用aspectJ编译为独立jar文件的maven项目 .myProject.dependencies
有sprint boot,hibernate和jpa . 我的MySQL数据库一切正常 . 我可以创建服务,它将使用存储库来持久化实体 .
为此,我创建了一个名为 JpaConfig
的类,其中包含所有必需的配置,因为xml文件不起作用 . 它看起来像这样缩短:
@EnableScheduling
@EnableAsync
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@ComponentScan(basePackages = {"myProject"})
@Configuration
@EnableJpaRepositories
public class JpaConfig {
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver(){
return new InstrumentationLoadTimeWeaver();
}
@Bean
public DataSource dataSource(){
// creates, configures and returnes a "BoneCPDataSource" Object
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
// creates, configures and returnes a "LocalContainerEntityManagerFactoryBean" Object
}
@Bean
public JpaTransactionManager transactionManager(){
// creates, configures and returnes a "JpaTransactionManager" Object
}
}
在我的 myProject.dependencies
项目的主要课程中,为了让 spring 工作,我称之为一条魔术线:
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JpaConfig.class);
使用这一行,配置被加载,我可以使用hibernate,jpa和spring及其所有功能 .
integrating spring boot
现在我想将spring boot添加到项目中,因为我需要一个REST API . 而且因为我读到 spring 靴可以在没有tomcat或glassfish的情况下运行,也可以在 jar 里而不是战争中运行,我认为这对我来说是完美的架构 .
所以我读了一些文章以便验证这一点,并且我发现spring boot带来了一个自己的集成tomcat,我试图在我现有的项目中实现它 .
我将以下依赖项添加到我的 pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.4.1.RELEASE</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
并且因为我无法设置父pom(我已经有一个父设置并且maven中不可能有多个父设备),我也将它添加到我的pom而不理解它,但因为这些行也在 org.springframework.boot:spring-boot-starter-parent:1.4.1.RELEASE
并且每个人都告诉我依靠这个配置,我添加了它:
<pluginManagement>
<plugins>
<!-- Apply more sensible defaults for user projects -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${start-class}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<mainClass>${start-class}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<delimiters>
<delimiter>${resource.delimiter}</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.1.11</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
</configuration>
</plugin>
<!-- Support our own plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
<!-- Support shade packaging (if the user does not want to use our plugin) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
<configuration>
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${start-class}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
- 我需要所有这些插件吗?并且没有遗传的pom里面的
pluginManagement
是不必要的?我需要什么?
在完成 pom.xml
之后,我修改了主类并向其添加了第二行:
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JpaConfig.class);
ApplicationContext ctx = SpringApplication.run(SpringBootApp.class, args);
-
现在有两种情况 . 它可以开始,是的,但我不确定这是否有用 . 我不想两次配置 . 那么如何修改我的
JpaConfig
类以便配置 spring 启动 . 继续:如何在主类中修改我的两行以便只进行一次配置?也许使用SpringApplication.run
并给它JpaConfig
类?但构造函数不允许这样做 . -
当我用所有修改开始这个当前项目时,独立地可能是hibernate和/或jpa和/或spring注释被搜索并且bild两次,我得到这个错误:
org.springframework.beans.factory.BeanDefinitionStoreException:无法处理配置类[myProject.dependency.spring.javaOnly.springBoot.SpringBootApp]的导入候选项;嵌套异常是java.lang.IllegalArgumentException:在META-INF / spring.factories中找不到自动配置类 . 如果您使用的是自定义包装,请确保该文件正确无误 . // ...引起:java.lang.IllegalArgumentException:在META-INF / spring.factories中找不到自动配置类 . 如果您使用的是自定义包装,请确保该文件正确无误 .
我不太清楚这是告诉我的,因为我在 META-INF
中创建了 spring.factories
,当我编译它时, META-INF
文件夹中甚至还有两个 spring.factories
. 我自己创建的那个和编译器创建的那个 . 无论我在 META-INF
文件夹中是否有两个或只有一个 spring.factories
,错误仍然是相同的 .
但也许这个问题会在其他两个问题得到解决时解决 . 非常感谢你为这篇长篇文章和我可能愚蠢的问题所花的时间和耐心 . 但是我希望你明白我还在学习并尽力改进,这些问题对我来说几天都无法解决 . 所以感谢任何提示或/和帮助 .
1 回答
我的建议将现有的 spring 项目整合到 spring 启动项目中,作为使用基于 spring 的完整基础架构构建的 spring 启动,尤其是自动配置 .
和
标准 spring 启动项目只需要
spring-boot-maven-plugin
插件 - doc这里不需要
AnnotationConfigApplicationContext
,SpringApplication
类提供了一种方便的方法来引导将从main()
方法启动的Spring应用程序 . 在许多情况下,您可以委派静态SpringApplication.run
方法ApplicationContext ctx = SpringApplication.run(SpringBootApp.class,args);
Spring boot建议您在其他类之上的根包中找到主应用程序类(
SpringBootApp
) . Spring引导将在此处应用带有默认根包的@ComponentScan
,因此它将默认加载JpaConfig
,此处不需要@ComponentScan(basePackages = {"myProject"})
,但这取决于项目中的包定义 .META-INF/spring.factories
用于注册所有自动配置类,如果您需要自定义auto-configurtion类,则可以删除此文件或将此文件清空为空 .好走 .