SpringBoot处理事务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pang_da_xing/article/details/77200647

在上一篇中,讲诉了本人搭建SpringBoot环境的心酸历程,今日兴致又起好奇SpringBoot如何处理事务的。由于上一次已经玩过一遍这一次搭建过程就顺心很多。

需要源码的可以点击链接

源码:SpringBoot-Mybatis处理事务

数据表 user.sql id设置的是自增

/*
Navicat MySQL Data Transfer

Source Server         : localhost_3306
Source Server Version : 60011
Source Host           : localhost:3306
Source Database       : cloudclass

Target Server Type    : MYSQL
Target Server Version : 60011
File Encoding         : 65001

Date: 2017-08-15 20:46:35
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `userName` varchar(20) NOT NULL,
  `userPwd` varchar(20) NOT NULL,
  `date` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------

INSERT INTO `user` VALUES ('9', 'pangdaxing', '123', '2016-11-30 21:59:00');

项目结构

这里写图片描述

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SpringBoot-Mybatis</groupId>
    <artifactId>SpringBoot-Mybatis</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot-Mybatis Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <!-- <start-class>com.springBoot.main.Application</start-class> -->
        <start-class>com.beenoisy.springboot.way.BeenoisySpringBootWayApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.8</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <!-- Add Spring repositories -->
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>  

Application.java:这里需要注意的是,Application.java是启动类,并且只会扫描同级的包和子包,所以此处将其放在com.main下,便于扫描com.main包和com.main.*下的其他类

package com.main;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement   //处理事务注解
public class Application {
    public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
    }
}

User

package com.main.bean;

import org.springframework.stereotype.Component;

public class User {
    private Integer id;
    private String userName;
    private String userPwd;
    private String date;

    public User() {
        super();
    }

    public User(String userName, String userPwd, String date) {
        super();
        this.userName = userName;
        this.userPwd = userPwd;
        this.date = date;
    }

    public User(Integer id, String userName, String userPwd, String date) {
        super();
        this.id = id;
        this.userName = userName;
        this.userPwd = userPwd;
        this.date = date;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", date=" + date + "]";
    }

}

Controller.java

package com.main.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.main.bean.User;
import com.main.mapper.UserMapper;
import com.main.service.UserService;

@RestController
@EnableAutoConfiguration
public class Controller {
        @RequestMapping("/")
        public String FirstPage(){
            return "welcome to SpringBoot-Mybatis!!!";
        }

        @RequestMapping("/hello/{myName}")  
        String index(@PathVariable String myName) {  
            return "Hello "+myName+"!!!";  
        }  

        @Autowired
        UserService userService;

        @RequestMapping("/find")
        public @ResponseBody User selectUser(Integer id){
            User user = userService.findUserById(id);
            return user;
        }

        @RequestMapping("/insert")
        public String insertUserRollBack(){

            User user = new User(9,"pangdaxing","123","2017-08-15 13:59:00");
            int insertUser = userService.insertUser(user);

            return insertUser>0 ? "success":"false";
        }

        @RequestMapping("/insertUserNoRollBack")
        public String insertUserNoRollBack(){

            User user = new User("zhangsan","123","2017-08-15 13:59:00");
            int insertUser = userService.insertUserNoRollBack(user);

            return insertUser>0 ? "success":"false";
        }
}

UserMapper.java

package com.main.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.main.bean.User;


@Mapper
public interface UserMapper {

    @Select("select * from user where id = #{id}")
    User findUserById(@Param("id") Integer id);

    @Select("select * from user where userName = #{userName}")
    User findUserByUserName(String userName);

    @Insert("insert into user (userName, userPwd, date) values( #{userName} ,  #{userPwd},  #{date})")
    public int insertUserRollBack(User user);

    @Insert("insert into user (userName, userPwd, date) values( #{userName} ,  #{userPwd},  #{date})")
    public int insertUserNoRollBack(User user);



}

UserService.java

package com.main.service;

import org.apache.ibatis.annotations.Param;

import com.main.bean.User;

public interface UserService {

    User findUserById(Integer id);

    public int insertUser(User user);

    public int insertUserNoRollBack(User user);

}

UserServiceImpl.java

package com.main.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.main.bean.User;
import com.main.mapper.UserMapper;
import com.main.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

     @Transactional(rollbackFor = {IllegalArgumentException.class})
    @Override
    public int insertUser(User user) {
         User findUserByUserName = userMapper.findUserByUserName(user.getUserName());
         int insertUser = 0;
         insertUser = userMapper.insertUserRollBack(user);
         if (findUserByUserName != null) {
                throw new IllegalArgumentException(user.getUserName()+"已存在,数据将回滚");
            } 
        return insertUser;
    }

     @Transactional(noRollbackFor = {IllegalArgumentException.class})
    @Override
    public int insertUserNoRollBack(User user) {
         User findUserByUserName = userMapper.findUserByUserName(user.getUserName());
         int insertUser = 0;
         insertUser = userMapper.insertUserNoRollBack(user);
         if (findUserByUserName != null) {
                throw new IllegalArgumentException(user.getUserName()+" 已存在,数据将不会回滚");
            }
        return insertUser;
    }

    @Transactional(noRollbackFor = {IllegalArgumentException.class})
    @Override
    public User findUserById(Integer id) {
        User user = this.userMapper.findUserById(id);
        if (user == null) {
            throw new IllegalArgumentException(" 查询结果为空,数据将不会回滚");
        }
        return user;
    }

}

application.properties

spring.datasource.url=jdbc:mysql:///cloudclass
spring.datasource.username=root
spring.datasource.password=529529
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#这里设置的是服务器访问端口,默认是8080
#spring.datasource.sport=6666  

运行效果
第一次执行插入操作 http://127.0.0.1:8080/insert
这里写图片描述

第二次执行插入操作 http://127.0.0.1:8080/insert :控制台抛出异常显示重复插入同一条数据,执行数据回滚操作
这里写图片描述
这里写图片描述
第一次访问:http://127.0.0.1:8080/insertUserNoRollBack
这里写图片描述
第二次访问:http://127.0.0.1:8080/insertUserNoRollBack
这里写图片描述

这里写图片描述

再来看看数据库有什么变化:由数据库我们可以看出,执行重复执行http://127.0.0.1:8080/insert操作,数据会回滚不会重复插入数据;执行http://127.0.0.1:8080/insertUserNoRollBack尽管一样会抛出异常但是不会进行数据回滚,所以会导致数据的重复插入。

这里写图片描述

经常会遇到以下错误,显示端口占用,直接重启就好


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.4.3.BUILD-SNAPSHOT)

2017-08-15 21:29:30.803  INFO 5196 --- [           main] com.main.Application                     : Starting Application on DESKTOP-MQSHI7G with PID 5196 (C:\EclipseWorkPlace\SpringBoot-Mybatis\target\classes started by 胖大星 in C:\EclipseWorkPlace\SpringBoot-Mybatis)
2017-08-15 21:29:30.806  INFO 5196 --- [           main] com.main.Application                     : No active profile set, falling back to default profiles: default
2017-08-15 21:29:30.947  INFO 5196 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1725dc0f: startup date [Tue Aug 15 21:29:30 CST 2017]; root of context hierarchy
2017-08-15 21:29:32.971  INFO 5196 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$5506b1eb] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-08-15 21:29:33.782  INFO 5196 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-15 21:29:33.796  INFO 5196 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-08-15 21:29:33.797  INFO 5196 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-08-15 21:29:33.934  INFO 5196 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-08-15 21:29:33.934  INFO 5196 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2992 ms
2017-08-15 21:29:34.201  INFO 5196 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-15 21:29:34.207  INFO 5196 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-15 21:29:34.208  INFO 5196 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-15 21:29:34.208  INFO 5196 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-15 21:29:34.208  INFO 5196 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-08-15 21:29:35.005  INFO 5196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1725dc0f: startup date [Tue Aug 15 21:29:30 CST 2017]; root of context hierarchy
2017-08-15 21:29:35.101  INFO 5196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/{myName}]}" onto java.lang.String com.main.controller.Controller.index(java.lang.String)
2017-08-15 21:29:35.102  INFO 5196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.main.controller.Controller.FirstPage()
2017-08-15 21:29:35.102  INFO 5196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/find]}" onto public com.main.bean.User com.main.controller.Controller.selectUser(java.lang.Integer)
2017-08-15 21:29:35.102  INFO 5196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/insert]}" onto public java.lang.String com.main.controller.Controller.insertUserRollBack()
2017-08-15 21:29:35.103  INFO 5196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/insertUserNoRollBack]}" onto public java.lang.String com.main.controller.Controller.insertUserNoRollBack()
2017-08-15 21:29:35.114  INFO 5196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-15 21:29:35.115  INFO 5196 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-15 21:29:35.160  INFO 5196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-15 21:29:35.161  INFO 5196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-15 21:29:35.226  INFO 5196 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-15 21:29:35.524  INFO 5196 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-08-15 21:29:35.566 ERROR 5196 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Failed to start end point associated with ProtocolHandler [http-nio-8080]

java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_101]
    at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_101]
    at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_101]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_101]
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_101]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:228) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:874) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:590) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:969) [tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) [tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:233) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:178) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:297) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:145) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) [spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at com.main.Application.main(Application.java:13) [classes/:na]

2017-08-15 21:29:35.568 ERROR 5196 --- [           main] o.apache.catalina.core.StandardService   : Failed to start connector [Connector[HTTP/1.1-8080]]

org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8080]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:233) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:178) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:297) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:145) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) [spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.BUILD-SNAPSHOT.jar:1.4.3.BUILD-SNAPSHOT]
    at com.main.Application.main(Application.java:13) [classes/:na]
Caused by: org.apache.catalina.LifecycleException: service.getName(): "Tomcat";  Protocol handler start failed
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:976) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    ... 13 common frames omitted
Caused by: java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_101]
    at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_101]
    at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_101]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_101]
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_101]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:228) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:874) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:590) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:969) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
    ... 14 common frames omitted

2017-08-15 21:29:35.581  INFO 5196 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-08-15 21:29:35.673  INFO 5196 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-15 21:29:35.677 ERROR 5196 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.

2017-08-15 21:29:35.679  INFO 5196 --- [           main] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1725dc0f: startup date [Tue Aug 15 21:29:30 CST 2017]; root of context hierarchy
2017-08-15 21:29:35.680  INFO 5196 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

如果遇到:Content is not allowed in prolog.错误。那么很有可能是由于数据库的jar包版本不匹配,需要更换jar包

猜你喜欢

转载自blog.csdn.net/pang_da_xing/article/details/77200647
今日推荐