目前我正在尝试将spring boot添加到现有项目中 这个项目是一个独立的jar,成功地使用了spring jpa和hibernate。但是因为应用程序必须能够接收和发送json文件,所以我认为spring boot是最好的选择。
因此,我将以下依赖项添加到pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
并设置一个RequestMapping作为测试
@Controller
public class RequestMappingController {
@RequestMapping("/")
@ResponseBody
public String myAction() {
return "Hello World !";
}
}
现在我遇到了问题,我的所有来源(this,this和this)都告诉我接下来要设置主要方法。
像
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
但我无法改变那种方法。我的架构使用一个主jar文件,它将组件和插件作为jar文件加载。主jar文件已下载,不由我编程。
main.jar (downloaded)
|- plugin1.jar (programmed by me)
|- plugin2.jar (programmed by me)
|- ...
我没有使用任何xml文件(pom.xml file of course) to configure spring. In my
plugin1.jar there is a JpaConfig Class with all the important spring enabling annotations (
@ EnableScheduling ,
@ EnableAsync , transactionmanagement, component scan,
@Configuration , jpa repository enabling, ...)
@ SpringBootConfiguration除外并且因为我的plugin1能够激活此Configuration类中所有当前实现的spring组件,也许我可以在那里设置spring boot?(有没有办法?或者其他方式使用第二个配置类?)
I recognices that there is also a annotation
这是我的问题:如何在不改变main(String[] args)
方法的情况下在现有项目结构中启用spring boot?
答案 0 :(得分:2)
将此添加到您的pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
编辑: POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
那就是你需要为数据库的连接信息提供一个连接器和一个application.properties。例如:
# database
spring.datasource.url= jdbc:mysql://****
spring.datasource.username=****
spring.datasource.password=****
#ddl generation
spring.jpa.hibernate.ddl-auto=update
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.testOnBorrow=true
spring.datasource.timeBetweenEvictionRunsMillis = 3600000
spring.datasource.validationQuery = SELECT 1
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.max-wait=10000
# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.max-active=50
# Show or not log for each sql query
spring.jpa.show-sql = true
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect