使用maven 快速搭建springboot项目

maven pom.xml配置如下:
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE

junit
junit
test
org.springframework.boot
spring-boot-starter-web
com.lsm.app.GatewayApplication
org.springframework.boot
spring-boot-maven-plugin



后面这两项可省略,一样可以运行 spring-milestone http://repo.spring.io/libs-milestone false spring-milestone http://repo.spring.io/libs-milestone false
添加程序主入口 :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages="com.lsm.app")
@EnableAutoConfiguration
public class GatewayApplication {
public static void main(String[] args){
SpringApplication.run(GatewayApplication.class, args);
}
}
在eclipse中运行可以像运行应用程序一样运行此类。

添加springboot热部署:
org.springframework.boot
spring-boot-devtools




打包项目:

然后run运行看输出日志



在开发调试完成之后,可以将应用打成JAR包的形式,在Eclipse中可以直接使用Maven插件的package命令,最终会形成一个可运行的JAR包。我们使用java –jar命令就可以运行这个JAR包了。所呈现出的效果与在调试期是一样的。现在看一下这个JAR包解压后的目录结构:
这个JAR包与传统JAR包的不同之处在于里面有一个名为lib的目录,在这个目录中包含了这个简单应用所依赖的其他JAR包,其中也包含内置的嵌入式Tomcat,正是使用它,才能发布服务和访问Web资源。除了我们编写的源码所编译形成的CLASS以外,在org目录下还有许多Spring所提供的CLASS,正是依赖这些CLASS,才能够加载位于lib目录下JAR中的类。这样的加载机制与在OSGi bundle中声明Bundle-Classpath很类似,不过在OSGi中会由容器来负责加载指定路径下的类。这大致阐述了这样一个JAR包能够发布服务的原因。
如果我们想要使用HTML、JSP等Web资源的话,在Controller中直接返回对应的视图就可以了。
如果我们想要将这个JAR包转换成可以在Servlet容器中部署的WAR的话,就不能依赖于Application的main函数了,而是要以类似于web.xml文件配置的方式来启动Spring应用上下文,此时我们需要声明这样一个类:
public class HelloWebXml extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); }}
这个类的作用与在web.xml中配置负责初始化Spring应用上下文的监听器作用类似,只不过在这里不需要编写额外的XML文件了。
如果要将最终的打包形式改为WAR的话,还需要对pom.xml文件进行修改,除了需要将packaging的值修改为war以外,还需要对依赖进行适当的配置(这一部分在Spring Boot的样例和文档中均未提及,提醒大家注意):
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat
在这里需要移除对嵌入式Tomcat的依赖,这样打出的WAR包中,在lib目录下才不会包含Tomcat相关的JAR包,否则将会出现启动错误。另外,在移除对Tomcat的依赖后,为了保证编译正确,还需要添加对servlet-api的依赖,因此添加如下的配置:
org.apache.tomcat tomcat-servlet-api 7.0.42 provided
在这里将scope属性设置为provided,这样在最终形成的WAR中不会包含这个JAR包,因为Tomcat或Jetty等服务器在运行时将会提供相关的API类。此时,执行mvn package命令就会得到一个WAR文件,我们可以直接将其放到Tomcat下运行(需要7.0.42版本以上)。



添加测试类:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.lsm.app.service.TestService;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(class=StartApplication.class) //这里使用springBootTest
public class ApplicationTest {
@Autowired
private TestService testService;
@Test
public void test1() {
testService.test();
}
}


maven中

org.springframework.boot
spring-boot-starter-test


spring boot 添加jsp支持

pom.xml
javax.servlet
javax.servlet-api
provided
javax.servlet
jstl
provided
org.springframework.boot
spring-boot-starter-tomcat
provided
org.apache.tomcat.embed
tomcat-embed-jasper
provided

application.proterties
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp



你可能感兴趣的:(java,spring-boot)