您好,登錄后才能下訂單哦!
公司的很多項目都陸陸續續引入了Spring Boot,通過對Spring Boot的接觸了解發現其真的是大大地簡化了開發、簡化了依賴配置,很多功能注解一下就可以實現,真的是太方便了。下面記錄了一個Spring Boot的入門程序實現,包括過濾器、servlet、定時器、全局異常處理、日志、Druid數據源的SQL監控配置。
1,pom.xml文件:
<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.zws</groupId> <artifactId>spring-boot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
可以看到這個文件內容并不多,但是卻引入了很多需要的依賴。這里引入的Spring Boot版本為1.5.10.RELEASE,其中spring-boot-maven-plugin插件是Spring Boot提供的打包用的插件。mybatis-spring-boot-starter為MyBatis集成Spring Boot的依賴。druid-spring-boot-starter為阿里開源數據源Druid集成Spring Boot的依賴,它會下載對應的druid jar包,注意這里并沒有直接引入Druid依賴,如果直接引入Druid依賴jar包,則druid的監控頁面無法顯示sql。
2,Spring Boot配置文件resources/application.properties:
server.port=8080 server.context-path=/app spring.mvc.throw-exception-if-no-handler-found=true spring.resources.add-mappings=false #tomcat訪問日志 server.tomcat.basedir=logs server.tomcat.accesslog.enabled=true server.tomcat.accesslog.directory=access server.tomcat.accesslog.pattern=%t %a "%r" %s %b (%D ms) datasource.druid.url=jdbc:mysql://localhost/db_boot?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false datasource.druid.username=root datasource.druid.password=root datasource.druid.driver-class-name=com.mysql.jdbc.Driver datasource.druid.initialSize=5 datasource.druid.minIdle=5 datasource.druid.maxActive=20 datasource.druid.maxWait=60000 datasource.druid.minEvictableIdleTimeMillis=300000 datasource.druid.validationQuery=select 'x' datasource.druid.testOnBorrow=false datasource.druid.testOnReturn=false datasource.druid.testWhileIdle=true datasource.druid.poolPreparedStatements=false datasource.druid.maxPoolPreparedStatementPerConnectionSize=100 datasource.druid.filters=stat,wall,log4j mybatis.config-location=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
application.properties文件為Spring Boot的總配置文件,Spring Boot默認會加載resources目錄下的application.properties文件。Spring Boot默認內嵌Tomcat作為web容器,server.port為端口,server.context-path為上下文路徑。datasource開頭的為druid數據源的配置。
3,MyBatis的配置文件resources/mybatis/mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="cacheEnabled" value="true" /> <setting name="lazyLoadingEnabled" value="true" /> <setting name="multipleResultSetsEnabled" value="true" /> <setting name="useColumnLabel" value="true" /> <setting name="useGeneratedKeys" value="false" /> <setting name="autoMappingBehavior" value="PARTIAL" /> <setting name="autoMappingUnknownColumnBehavior" value="WARNING" /> <setting name="defaultExecutorType" value="SIMPLE" /> <setting name="defaultStatementTimeout" value="25" /> <setting name="defaultFetchSize" value="100" /> <setting name="safeRowBoundsEnabled" value="false" /> <setting name="mapUnderscoreToCamelCase" value="false" /> <setting name="localCacheScope" value="SESSION" /> <setting name="jdbcTypeForNull" value="OTHER" /> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" /> </settings> <typeAliases> <package name="com.zws.be.user.bean" /> </typeAliases> </configuration>
此配置文件的路徑已經配置在了resources/application.properties文件內。
4,resources/mybatis/UserMapper.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.zws.be.user.mapper.UserMapper" > <resultMap id="UserMap" type="User"> <id column="id" property="id" jdbcType="BIGINT" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="pass" property="pass" jdbcType="VARCHAR" /> <result column="create_date" property="createDate" /> </resultMap> <select id="getAll" resultMap="UserMap"> select id, name, pass, create_date from user </select> <select id="getUserById" resultMap="UserMap" parameterType="long"> select id, name, pass, create_date from user where id = #{value} </select> </mapper>
5,Sping Boot啟動類Application.java:
package com.zws.be; import javax.sql.DataSource; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; import com.alibaba.druid.pool.DruidDataSource; @SpringBootApplication @MapperScan(basePackages = {"com.zws.be.*.mapper"}) @ServletComponentScan @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean("dataSource") @ConfigurationProperties("datasource.druid") public DataSource getDruidDataSource() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } }
這個是程序啟動的入口,里面有mian函數,運行此類即可啟動Spring Boot。注解@MapperScan(basePackages = {"com.zws.be.*.mapper"})指定了映射mapper文件的接口所在的包。@EnableScheduling注解用于開啟Spring的定時組件,下面會有例子。@ServletComponentScan注解搭配@WebServlet和@WebFilter注解使得定義servlet和filter時非常方便。
6,下面是定義一個簡單的restful接口所需的各類:
com.zws.be.user.bean.User:
package com.zws.be.user.bean; import java.util.Date; import org.apache.ibatis.type.Alias; @Alias("User") public class User { private Long id; private String name; private String pass; private Date createDate; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", pass=" + pass + ", createDate=" + createDate + "]"; } }
com.zws.be.user.mapper.UserMapper:
package com.zws.be.user.mapper; import java.util.List; import com.zws.be.user.bean.User; public interface UserMapper { List<User> getAll(); User getUserById(Long id); }
com.zws.be.user.service.UserService:
package com.zws.be.user.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.zws.be.user.bean.User; import com.zws.be.user.mapper.UserMapper; @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getUsers() { return userMapper.getAll(); } public User getUser(Long id) { return userMapper.getUserById(id); } }
com.zws.be.user.controller.UserController:
package com.zws.be.user.controller; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.zws.be.user.bean.User; import com.zws.be.user.service.UserService; @RestController @RequestMapping("/user") public class UserController { public Logger logger = Logger.getLogger(UserController.class); @Autowired private UserService userService; @RequestMapping(value="/{id}", method=RequestMethod.GET, produces = "application/json") public String hello(@PathVariable Long id) { User user = userService.getUser(id); return JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat); } }
7,配置Druid的sql監控servlet:
package com.zws.be.filter; import javax.servlet.annotation.WebServlet; import com.alibaba.druid.support.http.StatViewServlet; /** * DRUID的sql監控 * @author wensh.zhu * @2018-03-11 */ @WebServlet(name="monitorSrvlet", urlPatterns= {"/druid/*"}) public class MonitorServlet extends StatViewServlet { private static final long serialVersionUID = -2211104135110049275L; }
啟動后訪問http://localhost:8080/app/druid即可進入Druid的sql監控的監控頁面。
8,過濾器例子:
package com.zws.be.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import org.apache.log4j.Logger; @WebFilter(filterName="filterDemo", urlPatterns = {"/*"}) public class FilterDemo implements Filter{ private Logger logger = Logger.getLogger(FilterDemo.class); @Override public void destroy() {} @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { logger.info("FilterDemo is working..."); chain.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { } }
9,定時器例子:
package com.zws.be.timer; import org.apache.log4j.Logger; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class TimerDemo { private Logger logger = Logger.getLogger(getClass()); @Scheduled(cron = "0/30 * * * * *") public void timer() { logger.info("定時器例子"); } }
10,全局異常處理:
package com.zws.be.exception; import org.apache.log4j.Logger; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; /** * 全局異常處理 * @author wensh.zhu * @2018-03-11 */ @RestControllerAdvice public class ExceptionAdvice { private Logger logger = Logger.getLogger(getClass()); private String pattern = "抱歉,異常了:{0}"; @ExceptionHandler(value = {Exception.class}) @ResponseBody public String handleAllException(Exception e, HttpServletRequest req, HttpServletResponse resp) { logger.error("接口調用異常", e); if (e instanceof NoHandlerFoundException) { String reqURI = req.getRequestURI().toString(); return MessageFormat.format(pattern, "資源" + reqURI + "不存在!!!"); } return MessageFormat.format(pattern, e.getMessage()); } }
11,log4j配置文件resources/log4j.properties:
log4j.rootLogger=INFO, stdout, infoLog, errorLog log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.conversionPattern = %d{yyyy-MM-dd HH\:mm\:ss}|%p|%C|%M|%L|%m%n log4j.appender.infoLog=com.zws.be.log.MyRollingFileAppender log4j.appender.infoLog.File=logs/info/info.log log4j.appender.infoLog.Threshold = info log4j.appender.infoLog.layout=org.apache.log4j.PatternLayout log4j.appender.infoLog.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss}|%p|%C|%M|%L|%m%n log4j.appender.errorLog=com.zws.be.log.MyRollingFileAppender log4j.appender.errorLog.File=logs/error/error.log log4j.appender.errorLog.Threshold = error log4j.appender.errorLog.layout=org.apache.log4j.PatternLayout log4j.appender.errorLog.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss}|%p|%C|%M|%L|%m%n
自定義日志生成文件名稱:
package com.zws.be.log; import java.io.File; import java.io.IOException; import java.io.InterruptedIOException; import org.apache.log4j.Priority; import org.apache.log4j.RollingFileAppender; import org.apache.log4j.helpers.CountingQuietWriter; import org.apache.log4j.spi.LoggingEvent; public class MyRollingFileAppender extends RollingFileAppender{ private long nextRollover = 0L; public void rollOver() { File target, file; if (qw != null) { long size = ((CountingQuietWriter) qw).getCount(); nextRollover = size + maxFileSize; } boolean renameSuccessed = true; if (maxBackupIndex > 0) { file = new File(getRollingFileName(fileName, maxBackupIndex)); if (file.exists()) { renameSuccessed = file.delete(); } for (int i = maxBackupIndex - 1; i >= 1 && renameSuccessed; i --) { file = new File(getRollingFileName(fileName, i)); if (file.exists()) { target = new File(getRollingFileName(fileName, i + 1)); renameSuccessed = file.renameTo(target); } } if (renameSuccessed) { target = new File(getRollingFileName(fileName, 1)); closeFile(); renameSuccessed = file.renameTo(target); if (!renameSuccessed) { try { setFile(fileName, true, bufferedIO, bufferSize); } catch (IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } } } } } } private String getRollingFileName(String fileName, int index) { String fName = ""; String newIndex = index < 10 ? ("0" + index) : String.valueOf(index); if (index > 0) { fName = fileName.replace(".log", "") + "_" + newIndex + ".log"; } else { fName = fileName; } return fName; } @Override protected void subAppend(LoggingEvent event) { super.subAppend(event); if (fileName != null && qw != null) { long size = ((CountingQuietWriter) qw).getCount(); if (size >= maxFileSize && size >= nextRollover) { rollOver(); } } } @Override public boolean isAsSevereAsThreshold(Priority priority) { String level = priority.toString(); if ("ERROR".equals(level)) { return getThreshold().equals(priority); } return super.isAsSevereAsThreshold(priority); } }
12,Spring MVC請求參數以及返回信息自動日志的輔助類:
package com.zws.be.log; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.MessageFormat; import org.apache.log4j.Logger; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; /** * 用于請求參數、響應信息的自動日志。 * @author wensh.zhu * @2018-03-11 */ @ControllerAdvice(basePackages = {"com.zws.be"}) public class LogResponseBodyAdvice implements ResponseBodyAdvice<String>{ private String pattern = "\n---BEGIN---\nReqURI:{0}\nParams:{1}\nMethod:{2}\nReturn:{3}\n---END---"; private Logger logger = Logger.getLogger(LogResponseBodyAdvice.class); @Override public String beforeBodyWrite(String rtnMsg, MethodParameter param, MediaType arg2, Class<? extends HttpMessageConverter<?>> arg3, ServerHttpRequest req, ServerHttpResponse resp) { String clazName = param.getDeclaringClass().getName(); String mtdName = param.getMethod().getName(); String reqURI = req.getURI().toString(); String reqParams = getReqParams(req); String log = getLog(reqURI, reqParams, clazName + "." + mtdName, rtnMsg); logger.info(log); return rtnMsg; } @Override public boolean supports(MethodParameter arg0, Class<? extends HttpMessageConverter<?>> arg1) { return true; } private String getLog(String reqURI, String reqParams, String proMethod, String rtnMsg) { return MessageFormat.format(pattern, reqURI, reqParams, proMethod, rtnMsg); } private String getReqParams(ServerHttpRequest req) { StringBuilder body = new StringBuilder(); String line = null; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(req.getBody(), "UTF-8")); while ((line = reader.readLine()) != null) { body.append(line); } } catch (IOException e) { logger.error("I/O error ", e); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { logger.error("I/O error ", e); } } return body.toString(); } }
此類功能為將Spring MVC接受到請求的參數、處理請求對應的方法、請求返回內容自動打印到日志文件內,在接口出現問題的時候方便排查,日志輸出樣例:
---BEGIN--- ReqURI:http://localhost:8080/app/user/1 Params: Method:com.zws.be.user.controller.UserController.hello Return:{"createDate":"2018-03-04 15:44:25","id":1,"name":"zhangsan","pass":"zhangsan@123"} ---END---
13,運行Application,訪問http://localhost:8080/app/user/1,如下圖:
查看druid的監控:
項目目錄結構圖:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。