A preliminary study on spring boot

I heard that spring boot has zero configuration, which is very easy to use. I will try it out.

1. Create a Maven project and configure pom.xml (some dependent packages are not used in this demo, this demo only uses spring-boot-starter-parent, spring-boot-starter-web , others will be in the future It is used in the actual project, so it is also posted, everyone can take what they need)

<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>
    
    <!-- log -->
    <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><!-- with reflection -->
	  <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. Coding Controller, Run

@RestController //@RestController is equivalent to @Controller+@ResponseBody
@RequestMapping("/test")
public class HelloController {
	
	@RequestMapping("/hello")
        public String getHello() {
                return "Hello Spring Boot .....";
        }
	

}

 

//The sibling package of the class where @SpringBootApplication is located and all beans in the subordinate package
@SpringBootApplication
@ServletComponentScan
// After using @ServletComponentScan annotation, Servlet, Filter, Listener can be automatically registered directly through @WebServlet, @WebFilter, @WebListener annotations
public class Run implements EmbeddedServletContainerCustomizer {

	// Modify the default port for access
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		container.setPort(80);
	}

	public static void main(String[] args) {
		SpringApplication.run(Run.class, args);
	}

}

3. Start the project, enter Run.java, right click to run java application

4. Open the browser to visit: localhost/test/hello

Well, it's really simple, neither sping nor web configuration is needed. The key is how to deploy to Linux?

It is also very simple. After packaging, it is an executable jar package. Put the jar package on the Linux server and pass the command:

java -jar project.jar

You can start the project without the tomcat container.

Simple is simple, but there are problems. The projects that are usually contacted will basically configure filters, listeners, and interceptors on mvc, etc. in web.xml, and spring boot does not have these configuration files. How to implement these configurations in the code? Keep going...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217690&siteId=291194637
Recommended