spring boot应用

来源:互联网 发布:ar9271 linux驱动 编辑:程序博客网 时间:2024/05/20 11:19

听说spring boot零配置,挺好用,我也来试用下。

1.创建Maven项目,配置pom.xml(部分依赖包在此demo中没有用到,此demo只用到spring-boot-starter-parent、spring-boot-starter-web,其他的会在往后的实际项目中用到,所以也贴了出来,大家各取所需)

<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>tboot</artifactId>    <version>0.0.1-SNAPSHOT</version>        <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.3.7.RELEASE</version><!-- 1.5.7.RELEASE -->    </parent>        <dependencies>      <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-web</artifactId>      </dependency>            <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-freemarker</artifactId>      </dependency>            <!-- 日志 -->      <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter</artifactId>          <exclusions>              <exclusion>                  <groupId>org.springframework.boot</groupId>                  <artifactId>spring-boot-starter-logging</artifactId>              </exclusion>          </exclusions>       </dependency>       <dependency>           <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-log4j</artifactId>       </dependency>              <dependency>          <groupId>mysql</groupId>          <artifactId>mysql-connector-java</artifactId>       </dependency>       <dependency>          <groupId>org.mybatis.spring.boot</groupId>          <artifactId>mybatis-spring-boot-starter</artifactId>          <version>1.0.0</version>      </dependency>      <dependency>            <groupId>com.zaxxer</groupId>            <artifactId>HikariCP</artifactId>        </dependency>            <dependency>          <groupId>redis.clients</groupId>          <artifactId>jedis</artifactId>          <version>2.8.1</version>      </dependency>            <dependency>          <groupId>javax.servlet.jsp</groupId>          <artifactId>jsp-api</artifactId>          <version>2.2</version>          <!-- <scope>provided</scope> -->      </dependency>      <dependency>          <groupId>javax.servlet</groupId>          <artifactId>jstl</artifactId>      </dependency>            <dependency>          <groupId>org.apache.commons</groupId>          <artifactId>commons-lang3</artifactId>          <version>3.1</version>      </dependency>      <dependency><!-- 带反射机制 -->        <groupId>commons-beanutils</groupId>        <artifactId>commons-beanutils-core</artifactId>        <version>1.8.3</version>      </dependency>      <dependency>          <groupId>commons-io</groupId>          <artifactId>commons-io</artifactId>          <version>2.4</version>      </dependency>      <dependency>          <groupId>commons-fileupload</groupId>          <artifactId>commons-fileupload</artifactId>          <version>1.3.1</version>      </dependency>            <dependency>          <groupId>com.google.code.gson</groupId>          <artifactId>gson</artifactId>          <version>2.5</version>      </dependency>    </dependencies>        <build>      <finalName>tboot</finalName>      <plugins>          <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-compiler-plugin</artifactId>              <configuration>                  <source>1.7</source>                  <target>1.7</target>                  <encoding>UTF-8</encoding>              </configuration>          </plugin>          <!-- <plugin>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-maven-plugin</artifactId>              <configuration>                  <fork>true</fork>                  <mainClass>${start-class}</mainClass>              </configuration>                <executions>                  <execution>                    <goals>                      <goal>repackage</goal>                    </goals>                  </execution>              </executions>          </plugin>          -->                    <plugin>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-maven-plugin</artifactId>             <configuration>                <layout>ZIP</layout>                <minimizeJar>true</minimizeJar>             </configuration>          </plugin>                </plugins>    </build>  </project>
2.编码Controller、Run

@RestController //@RestController相当于@Controller+@ResponseBody  @RequestMapping("/test")  public class HelloController {            @RequestMapping("/hello")          public String getHello() {                  return "Hello Spring Boot .....";          }        }
//@SpringBootApplication所在类的同级包以及下级包里的所有bean  @SpringBootApplication  @ServletComponentScan  // 使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册  public class Run implements EmbeddedServletContainerCustomizer {        // 修改访问的默认端口      @Override      public void customize(ConfigurableEmbeddedServletContainer container) {          container.setPort(80);      }        public static void main(String[] args) {          SpringApplication.run(Run.class, args);     }    }

3.启动项目,进入Run.java,右键运行java application

4.打开浏览器访问:localhost/test/hello

好吧,真的很简单,sping和web的配置都不用。关键是部署到Linux要怎么办?

也很简单,打包后是一个可执行的jar包,将这jar包放到Linux服务器上,通过命令:

java -jar project.jar

即可启动项目,无需tomcat容器。

简单归简单,但也有问题。平常接触的项目基本都会在web.xml配置过滤器,监听器,在mvc上配置拦截器等等,而spring boot没有这些配置文件,那这些配置,在代码怎样实现?继续深入……


原创粉丝点击