1, 热部署:
有jrebel的话, 不用了, 不如jre好用
原理: 使用两个classLoad, 一个加载不改变的jar, 另一个加载可更改的jar, 发生改变后, 舍弃可更改的jar重新restart classloader, 由于加载的类少, 所以更快重启(5s以内)
1), 使用 spring-boot:run运行, 但会存在进程未结束
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
<dependencies>
<!--springloaded hot deploy -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
然后可以使用spring-boot:run来进行项目运行, 既可以实现热部署了
2), 如果使用run as java.. 需要将spring-loader-1.2.4.RELEASE.jar下载下来,放到项目的lib目录中,然后把IDEA的run参数里VM参数设置为:
-javaagent:.libspringloaded-1.2.4.RELEASE.jar -noverify
3), 新添加的方法没法进行热部署, 所以: 不使用上面的方式, 使用如下方式: 需要eclipse开启 Build Automatically(自动编译)
添加依赖:
<dependency>
<groupId>org.springframework.boot</>
<artifactId>spring-boot-devtools</>
<optional>true</>
<scope>true</>
</>
然后添加spring-boot-plugin插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</>
<artifactId>spring-boot-maven-plugin</>
<configuration>
<!-- 如果没有这个, devTools不起作用-->
<fork>true</>
</>
</></>
修改配置文件, 也会生效, 这个比jre好用
新建class也会生效
页面的修改也会生效, 但需要在applicaton.properties中设置 spring.thymeleaf.cache=false 来实现
需要eclipse开启build automatically, 如果设置springApplication.setRegisterShutdownHook(false) 自动重启不起作用
2, 更改端口号:
在application.properties中:
server.port=9090
既可以完成修改了
3, 配置ContextPath
嗯, 在application.properties中修改
server.context-path=/spring-boot
就可以通过 http://ip:port/spring-boot来访问项目了
######################################################## ###EMBEDDED SERVER CONFIGURATION (ServerProperties) ######################################################## #server.port=8080 #server.address= # bind to a specific NIC #server.session-timeout= # session timeout in seconds #the context path, defaults to '/' #server.context-path=/spring-boot #server.servlet-path= # the servlet path, defaults to '/' #server.tomcat.access-log-pattern= # log pattern of the access log #server.tomcat.access-log-enabled=false # is access logging enabled #server.tomcat.protocol-header=x-forwarded-proto # ssl forward headers #server.tomcat.remote-ip-header=x-forwarded-for #server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp) #server.tomcat.background-processor-delay=30; # in seconds #server.tomcat.max-threads = 0 # number of threads in protocol handler #server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding
4, 锁定jdk版本
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
5, 解决springboot的乱码问题
<build>
<plugins>
<!-- main方法运行需要 -->
<!-- 加入热部署插件, spring-boot:run可用 -->
<!-- java可用, 需要下载jar包放在lib下, 然后修改vm参数 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
</configuration>
<dependencies>
<!--springloaded hot deploy -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
6, 打成jar包直接运行
pom.xml中加入:
<!-- 锁定jdk版本 -->
<properties>
<!-- 指定启动类(main方法的位置) -->
<start-class>com.wenbronk.profiles.App</start-class>
<java.version>1.8</java.version>
<!-- 构建编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
既可以通过
mvn clean package -Dmaven.skip.test=true
打成jar包, 然后通过命令
java -jar xxx.jar
运行, 否则会报错找不到主类之类的...
7, 打成war包运行,
pom.xml需要添加:
<!-- 为了构建一个即是可执行的,又能部署到一个外部容器的war文件,你需要标记内嵌容器依赖为"provided" --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
App类需要:
package com.wenbronk.profile; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App extends SpringBootServletInitializer { /** * war使用 */ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(App.class); } /** * jar使用 * @param args * @throws Exception * @time 2017年5月15日 */ public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } }
或者不在App上继承, 然后同一目录下 新建一个类:
package com.wenbronk; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; /** * war包使用 * @author wenbronk * @Date 下午3:12:13 */ public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(MyOneProjecApplication.class); } }