spring老项目转springboot项目详细步骤

2019-12-03 16:41:51

参考地址 spring老项目转springboot项目 笔记

引入jar包

先不删除老的jar包

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 

复制代码

  <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>
        <!-- log4j -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

            <!-- 这个是剔除掉自带的 tomcat部署的-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>

        </dependency>
        <!-- tomcat容器部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--<scope>compile</scope>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 支持 @ConfigurationProperties 注解 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

复制代码

spring boot打包的插件

复制代码

  <build>
        <finalName>MIS</finalName>
        <!-- maven 打包不全(xml,properties文件没打进包)解决方案-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <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>
                <version>1.5.9.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

复制代码

 

创建springboot主类

复制代码

package com.guige;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"com.guige.oim.v2.*"})public class Application extends SpringBootServletInitializer {

    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        return application.sources(Application.class);
    }    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

复制代码

 

创建application.properties文件

复制代码

#默认前缀
server.contextPath=/#数据源
spring.datasource.name=adminDataSource
spring.datasource.driverClassName = oracle.jdbc.OracleDriver
spring.datasource.url = spring.datasource.username = spring.datasource.password =

复制代码

 

1.先把web.xml去掉

创建core包

web.xml里的servlet转成bean形式

filter也一样

创建WebConfig配置类

复制代码

package com.guige.core.conf;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.guige.core.ext.i18n.filter.LocaleFilter;
import com.guige.mis.filter.JspFilter;
import com.guige.mis.listener.ContextListener;
import com.guige.mis.listener.OnlineUserListener;
import com.guige.oim.filter.CORSFilter;
import com.guige.oim.servlet.*;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;/**
 * 对应web.xml
 *
 * @author songaw
 * @date 2018/7/17 9:10 */@Configurationpublic class WebConfig {    //----------------------Filter    //跨域过滤器    @Bean    public FilterRegistrationBean corsRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new CORSFilter());
        filterRegistrationBean.addUrlPatterns("/*");        return filterRegistrationBean;
    }    //字符集utf-8    @Bean    public FilterRegistrationBean characterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        filterRegistrationBean.setFilter(characterEncodingFilter);
        filterRegistrationBean.addUrlPatterns("/*");        //filterRegistrationBean.addInitParameter("encoding", "UTF-8");       // filterRegistrationBean.addInitParameter("ForceEncoding", "true");
        return filterRegistrationBean;
    }    //对jsp进行权限日志过滤    @Bean    public FilterRegistrationBean jspFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        JspFilter jspFilter = new JspFilter();
        filterRegistrationBean.setFilter(jspFilter);
        filterRegistrationBean.addUrlPatterns("*.do");        return filterRegistrationBean;
    }    /**
     * 如果session中没有设置locale串或者locale串不合法,默认采用request的locale,
     * @return     */
    @Bean    public FilterRegistrationBean localeRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new LocaleFilter());
        filterRegistrationBean.addUrlPatterns("*.do");        return filterRegistrationBean;
    }    //阿里druid界面设置    @Bean    public FilterRegistrationBean webStatFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");        return filterRegistrationBean;
    }    //--------------------------------Listener
    @Bean    public ServletListenerRegistrationBean<OnlineUserListener> onlineUserListenerRegistrationBean(){
        ServletListenerRegistrationBean<OnlineUserListener>
                sessionListener = new ServletListenerRegistrationBean<OnlineUserListener>(new OnlineUserListener());        return sessionListener;

    }
    @Bean    public ServletListenerRegistrationBean<ContextListener> contextListenerRegistrationBean(){
        ServletListenerRegistrationBean<ContextListener>
                sessionListener = new ServletListenerRegistrationBean<ContextListener>(new ContextListener());        return sessionListener;

    }//    --------------------------------------Servlet
    @Bean    public ServletRegistrationBean dispatcher() {

        ServletRegistrationBean reg = new ServletRegistrationBean();
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        dispatcherServlet.setContextConfigLocation("classpath:config/core/spring-mvc.xml," +                " classpath:config/core/dao/dao_authority.xml," +                " classpath:config/core/dao/dao_manager.xml," +                " classpath:config/core/dao/dao_oim.xml," +                " classpath:config/core/dao/dao_oim_v2.xml," +                " classpath:config/core/dao/dao_orgacus.xml," +                " classpath:config/core/service/service_authority.xml," +                " classpath:config/core/service/service_manager.xml," +                " classpath:config/core/service/service_oim.xml," +                " classpath:config/core/service/service_oim_v2.xml," +                " classpath:config/core/service/service_orgacus.xml," +                " classpath:config/client/i18n.xml," +                " classpath:config/client/http.xml," +                " classpath:config/client/action/action_main.xml," +                " classpath:config/client/action/action_authority.xml," +                " classpath:config/client/action/action_manager.xml," +                " classpath:config/client/action/action_oim.xml," +                " classpath:config/client/action/action_oim_v2.xml," +                " classpath:config/client/action/action_orgacus.xml");
        reg.setServlet(dispatcherServlet);
        reg.addUrlMappings("*.do");
        reg.setLoadOnStartup(30);        return reg;
    }  
    //阿里druid界面设置    @Bean    public ServletRegistrationBean statViewServlet() {

        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "111");
        reg.addInitParameter("loginPassword", "111");        return reg;
    }

}

复制代码

 

 

2.spring mvc文件

里面的bean 转成声明试bean

复制代码

package com.guige.core.conf;

import com.alibaba.druid.support.http.WebStatFilter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.guige.oim.filter.CORSFilter;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.resource.ContentVersionStrategy;
import org.springframework.web.servlet.resource.VersionResourceResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter {    /**
     * 添加拦截器
     * @param registry     */
    @Override    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleChangeInterceptor());    //    registry.addInterceptor(webInterceptor());    }    /**
     *
     * @param converters     */
    @Override    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {        //用来忽略json对象传入,反序列化时候属性名称不正确导致序列化失败的bug
        MappingJackson2HttpMessageConverter jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
        List<MediaType> supportedMediaTypes =new ArrayList<>();//        避免IE执行AJAX时,返回JSON出现下载文件        supportedMediaTypes.add(MediaType.TEXT_HTML);
        jacksonMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        ObjectMapper mapper = jacksonMessageConverter.getObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        converters.add(jacksonMessageConverter);
    }

    @Override    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(this.getViewResolver());        //MappingJsonView jsonView = new MappingJsonView();    //    registry.enableContentNegotiation(jsonView);    }

    @Bean    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setOrder(1);        return resolver;
    }
    @Override    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/").setViewName("forward:/login.html");
    }
    @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        VersionResourceResolver versionResourceResolver = new VersionResourceResolver();
        versionResourceResolver.addVersionStrategy(new ContentVersionStrategy(), "/**");
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/")
                .resourceChain(true)
                .addResolver(versionResourceResolver);
    }

    @Bean    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setSessionTimeout(14400, TimeUnit.SECONDS);
        ErrorPage ep1 = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");        return factory;
    }

}

复制代码

 

3. spring.xml转配置文件


  • 2020-03-14 16:35:00

    nuxt.js部署全过程(ubuntu+nginx+node+pm2)

    系统的话本篇是Ubuntu 16.04.6 ,centos也行,大同小异都是Linux。不过如果你是初学者,最好和我使用一样的,因为因为发行版本不同而导致的差异可能导致运行某些东西失败,找问题要找好久。windows server不推荐了,企业用的多,小服务器跑windows server比较费劲。

  • 2020-03-14 23:15:25

    icomoon使用详细介绍

    此篇博文讲述如何利用icomoon导入图标,从而把自己想要的都通过icomoon方式进行,大家都知道,网站以及移动端,用图标还是尽量选择这种。因为直接用image有些图标会失真,从而也是前端开发之中,需求去掌握的一项,很简单的就几个步骤。

  • 2020-03-14 23:39:59

    vuetify和@nuxt/vuetify icon 之我见

    vuetify中v-icon,貌似默认支持 Material Design Icons, Material Icons, Font Awesome 4 and Font Awesome 5, 我自己单独引入了vuetify 用哪一个图标都没有问题。但是用了@nuxt/vuetify只能用mdi-home这样的。不知道因为啥。肯定是封装后,封装成一个了。 但是我修改vuetify的设置,哪一个图标也都能用。哎,不过多研究了。

  • 2020-03-16 15:57:53

    nuxtjs中单独引入Message组件的问题

    // 引入elementUIimport { Message } from 'element-ui';//由于Message组件并没有install 方法供Vue来操作的,是直接返回的,因此按照官方文档单独引入的方法是//会报错的,需要给 Message 添加 install 方法Message.install = function (Vue, options) {Vue.prototype.$message = Message}Vue.use(Message )//消息提示

  • 2020-03-16 16:03:20

    css的var()函数

     随着sass,less预编译的流行,css也随即推出了变量定义var函数。var()函数,就如同sass和less等预编译软件一样,可以定义变量并且进行对应的使用。

  • 2020-03-16 16:52:05

    对icomoon的误解,以及最快速的使用

    此时需要注意顶部第一个选项,Quick Usage,一定要打开,Enable Quick Usage,谁让咱英语不好呢,这个时候会出现一个css连接,直接引用就好了,就可以随意使用图标了,引入这一个css就能实现我们的功能,省区引入太多文件的烦恼,你可以在浏览器打开这个css,可以看到里面把我们所用的文件整成base64了。所以挺好用的。

  • 2020-03-17 09:47:05

    video标签视频不自动播放的问题

    添加 muted 属性,就可以通过地址栏进入网页的时候自动播放了,手机端还是有的有限制的,比如iphone浏览器,就不行,苹果手机为了保护用户的流量和用户的意愿,是禁止自动播放的,必须有手动触发。