Sprigmvc项目转为springboot的方法
程序员文章站
2022-07-12 08:18:34
是否有老掉牙的springmvc项目,想转成springboot项目,看这个文章就对了。
说明
如果你的项目连maven项目都不是,请自行转为maven项目,在按照本教...
是否有老掉牙的springmvc项目,想转成springboot项目,看这个文章就对了。
说明
如果你的项目连maven项目都不是,请自行转为maven项目,在按照本教程进行。
本教程适用于spring+springmvc+mybatis+shiro的maven项目。
1.修改pom文件依赖
删除之前的spring依赖,添加springboot依赖
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.5.9.release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</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.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-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> </dependencies>
添加springboot构建插件
<plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <configuration> <source>1.7</source> <target>1.7</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>
2.添加application启动文件
注意,如果application在controller,service,dao的上一层包里,无需配置 @componentscan ,
否则,需要指明要扫描的包。
@springbootapplication //@componentscan({"com.cms.controller","com.cms.service","com.cms.dao"}) public class applicationextends 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); } }
3.添加springboot配置文件
在resources下面添加application.properties文件
添加基本配置 #默认前缀 server.contextpath=/ # 指定环境 spring.profiles.active=local # jsp配置 spring.mvc.view.prefix=/web-inf/jsp/ spring.mvc.view.suffix=.jsp #log配置文件 logging.config=classpath:logback-cms.xml #log路径 logging.path=/users/mac/work-tommy/cms-springboot/logs/ #数据源 spring.datasource.name=admindatasource spring.datasource.driverclassname = com.mysql.jdbc.driver spring.datasource.url = jdbc:mysql://localhost:3306/mycms?useunicode=true&autoreconnect=true&characterencoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456
4.使用@configuration注入配置
注入mybatis配置,分页插件请自主选择
@configuration @mapperscan(basepackages = "com.kuwo.dao",sqlsessiontemplateref = "adminsqlsessiontemplate") public class admindatasourceconfig{ @bean(name = "admindatasource") @configurationproperties(prefix = "spring.datasource") @primary public datasource admindatasource(){ return datasourcebuilder.create().build(); } @bean(name = "adminsqlsessionfactory") @primary public sqlsessionfactory adminsqlsessionfactory(@qualifier("admindatasource")datasource datasource)throws exception { sqlsessionfactorybean bean = new sqlsessionfactorybean(); bean.setdatasource(datasource); //分页插件 // pagehelper pagehelper = new pagehelper(); pageplugin pageplugin = new pageplugin(); // properties props = new properties(); // props.setproperty("reasonable", "true"); // props.setproperty("supportmethodsarguments", "true"); // props.setproperty("returnpageinfo", "check"); // props.setproperty("params", "count=countsql"); // pagehelper.setproperties(props); //添加插件 bean.setplugins(new interceptor[]{pageplugin}); // 添加mybatis配置文件 bean.setconfiglocation(new defaultresourceloader().getresource("classpath:mybatis/mybatis-config.xml")); // 添加mybatis映射文件 bean.setmapperlocations(new pathmatchingresourcepatternresolver().getresources("classpath:mybatis/system/*.xml")); return bean.getobject(); } @bean(name = "admintransactionmanager") @primary public datasourcetransactionmanager admintransactionmanager(@qualifier("admindatasource")datasource datasource){ return new datasourcetransactionmanager(datasource); } @bean(name = "adminsqlsessiontemplate") @primary public sqlsessiontemplate adminsqlsessiontemplate(@qualifier("adminsqlsessionfactory")sqlsessionfactory sqlsessionfactory)throws exception { return new sqlsessiontemplate(sqlsessionfactory); } }
添加interceptor配置,注意addinterceptor的顺序,不要搞乱了
@configuration public class interceptorconfigurationextends webmvcconfigureradapter{ @override public void addinterceptors(interceptorregistry registry){ registry.addinterceptor(new loginhandlerinterceptor()); } }
添加shiro配置文件
注意:本来使用redis做session缓存,但是和shiro集成发现一个问题,user对象存储以后,从shiro中获取后,无法进行类型转换,所以暂时放弃了redis做session缓存。
@configuration public class shiroconfiguration{ @value("${spring.redis.host}") private string host; @value("${spring.redis.port}") private int port; @value("${spring.redis.timeout}") private int timeout; @bean public static lifecyclebeanpostprocessor getlifecyclebeanpostprocessor(){ return new lifecyclebeanpostprocessor(); } /** * shirofilterfactorybean 处理拦截资源文件问题。 * 注意:单独一个shirofilterfactorybean配置是或报错的,因为在 * 初始化shirofilterfactorybean的时候需要注入:securitymanager * filter chain定义说明 1、一个url可以配置多个filter,使用逗号分隔 2、当设置多个过滤器时,全部验证通过,才视为通过 3、部分过滤器可指定参数,如perms,roles * */ @bean public shirofilterfactorybean shirofilter(securitymanager securitymanager){ system.out.println("shiroconfiguration.shirfilter()"); shirofilterfactorybean shirofilterfactorybean = new shirofilterfactorybean(); // 必须设置 securitymanager shirofilterfactorybean.setsecuritymanager(securitymanager); // 如果不设置默认会自动寻找web工程根目录下的"/login.jsp"页面 shirofilterfactorybean.setloginurl("/login_tologin"); // 登录成功后要跳转的链接 shirofilterfactorybean.setsuccessurl("/userspage"); //未授权界面; shirofilterfactorybean.setunauthorizedurl("/403"); //拦截器. map<string,string> filterchaindefinitionmap = new linkedhashmap<>(); //配置退出 过滤器,其中的具体的退出代码shiro已经替我们实现了 filterchaindefinitionmap.put("/logout", "logout"); filterchaindefinitionmap.put("/login_tologin", "anon"); filterchaindefinitionmap.put("/login_login", "anon"); filterchaindefinitionmap.put("/static/login/**","anon"); filterchaindefinitionmap.put("/static/js/**","anon"); filterchaindefinitionmap.put("/uploadfiles/uploadimgs/**","anon"); filterchaindefinitionmap.put("/code.do","anon"); filterchaindefinitionmap.put("/font-awesome/**","anon"); //<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterchaindefinitionmap.put("/**", "authc"); shirofilterfactorybean.setfilterchaindefinitionmap(filterchaindefinitionmap); return shirofilterfactorybean; } @bean public securitymanager securitymanager(){ defaultwebsecuritymanager securitymanager = new defaultwebsecuritymanager(); //设置realm. securitymanager.setrealm(myshirorealm()); // 自定义缓存实现 使用redis //securitymanager.setcachemanager(cachemanager()); // 自定义session管理 使用redis securitymanager.setsessionmanager(sessionmanager()); return securitymanager; } @bean public shirorealm myshirorealm(){ shirorealm myshirorealm = new shirorealm(); // myshirorealm.setcredentialsmatcher(hashedcredentialsmatcher()); return myshirorealm; } } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securitymanager * @return */ @bean public authorizationattributesourceadvisor authorizationattributesourceadvisor(securitymanager securitymanager){ authorizationattributesourceadvisor authorizationattributesourceadvisor = new authorizationattributesourceadvisor(); authorizationattributesourceadvisor.setsecuritymanager(securitymanager); return authorizationattributesourceadvisor; } /** * 配置shiro redismanager * 使用的是shiro-redis开源插件 * @return */ public redismanager redismanager(){ redismanager redismanager = new redismanager(); redismanager.sethost(host); redismanager.setport(port); redismanager.setexpire(1800); redismanager.settimeout(timeout); // redismanager.setpassword(password); return redismanager; } /** * cachemanager 缓存 redis实现 * 使用的是shiro-redis开源插件 * @return */ public rediscachemanager cachemanager(){ rediscachemanager rediscachemanager = new rediscachemanager(); rediscachemanager.setredismanager(redismanager()); return rediscachemanager; } /** * redissessiondao shiro sessiondao层的实现 通过redis * 使用的是shiro-redis开源插件 */ @bean public redissessiondao redissessiondao(){ redissessiondao redissessiondao = new redissessiondao(); redissessiondao.setredismanager(redismanager()); return redissessiondao; } @bean public defaultwebsessionmanager sessionmanager(){ defaultwebsessionmanager sessionmanager = new defaultwebsessionmanager(); // sessionmanager.setsessiondao(redissessiondao()); return sessionmanager; } }
总结
搞了一天时间把项目转成springboot,查阅各种资料,希望这篇文章能够为你带来帮助。也希望大家多多支持。
上一篇: 葱的功效,你知道吗
下一篇: 实例学习如何在ASP中调用DLL
推荐阅读
-
springboot+mybatis日志显示SQL的最简单方法
-
0成本躺着月入万元的方法(2018最新风口小程序项目)
-
Vue2.0学习系列之项目上线的方法步骤(图文)
-
在vue项目中,将juery设置为全局变量的方法
-
Vant的安装和配合引入Vue.js项目里的方法步骤
-
vue-cli项目无法用本机IP访问的解决方法
-
sql2005 数据库转为sql2000数据库的方法(数据导出导入)
-
linux系统下部署项目访问报404错误的解决方法
-
vue-cli创建的项目,配置多页面的实现方法
-
SpringBoot项目使用RedisTemplate遇到key值\xac\xed\x00\x05t\x00的坑