SpringBoot搭建笔记以及常见问题

13 篇文章 0 订阅
3 篇文章 0 订阅

一、spring、springMvc、springBoot和springCloud的联系与区别

https://blog.csdn.net/alan_liuyue/article/details/80656687

Spring Boot实现了自动配置,降低了项目搭建的复杂度。

众所周知Spring框架需要进行大量的配置,Spring Boot引入自动配置的概念,让项目设置变得很容易。Spring Boot本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。也就是说,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具。同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box),大部分的Spring Boot应用都只需要非常少量的配置代码,开发者能够更加专注于业务逻辑。

Spring Boot只是承载者,辅助你简化项目搭建过程的。如果承载的是WEB项目,使用Spring MVC作为MVC框架,那么工作流程和你上面描述的是完全一样的,因为这部分工作是Spring MVC做的而不是Spring Boot。

对使用者来说,换用Spring Boot以后,项目初始化方法变了,配置文件变了,另外就是不需要单独安装Tomcat这类容器服务器了,maven打出jar包直接跑起来就是个网站,但你最核心的业务逻辑实现与业务流程实现没有任何变化。

所以,用最简练的语言概括就是:

Spring 是一个“引擎”;

Spring MVC 是基于Spring的一个 MVC 框架 ;

Spring Boot 是基于Spring4的条件注册的一套快速开发整合包。

二、、常用注解:

通过使用@MapperScan可以指定要扫描的Mapper类的包的路径(注意不是xml映射文件的路径而是dao层接口的路径)

mybatis可以用xml进行数据操作,也可以在dao层用注解的方式,也可以采取xml和dao层接口组合使用的方法。显然 ,后者更加简单。

同时,使用@MapperScan注解多个包

@SpringBootApplication  

@MapperScan({"com.kfit.demo","com.kfit.user"})  

public class App {  

    public static void main(String[] args) {  

       SpringApplication.run(App.class, args);  

    }  

3、@RestController

@RestController的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!相当于@ResponseBody+@Controller的组合

如果controller层中需要返回页面,那么就不能用@RestController了

4、springboot访问静态资源

方式①

首先在application.properties中添加如下配置

 

spring.mvc.view.prefix=classpath:/templates/

spring.mvc.view.suffix=.html

然后pom.xml中引入

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

备注:不知道为啥一开是可以结果过了一会之后报如下错误,有待解决。更加奇怪的是过一会再运行又好了

方式②

controller模式

习惯上,我们还是多使用/index方式,而不是index.html方式。

为此还是需要controller。

application.properties中添加

spring.mvc.view.prefix=/

spring.mvc.view.suffix=.html

 

controller当然也是需要的,和之前一样:

 

@GetMapping("/index")

public String index(){

return "home"; //当浏览器输入/index时,会返回 /static/home.html的页面

}

参考链接https://blog.csdn.net/u012882134/article/details/77230900

https://www.jianshu.com/p/198497e08e00

三、SpringBoot中替代web.xml解决办法

SpringBoot中省掉了web.xml这样简化了项目同时也减少了项目搭建时的复杂性。网上有springboot加载web.xml的方法。但是个人感觉既然是被去掉的东西,再加上未免太复杂了。

SpringBoot中有相关的实现方法去实现web.xml中的配置功能。

以下介绍如何在springboot中添加自定义的配置

JavaEE中web.xml配置:

<filter>

<filter-name>OauthFilter</filter-name>

<filter-class>com.sense.sso.oauth.WebFilter</filter-class>

<init-param>

<param-name>serverAddress</param-name>

<param-value>http://localhost:8080/IAM</param-value>

</init-param>

<init-param>

<param-name>clientId</param-name>

<param-value>APP001</param-value>

</init-param>

<init-param>

<param-name>clientSecret</param-name>

<param-value>wpprkkmsi093jns2sdf4452sdf</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>OauthFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

SpringBoot实现方法

说明:上面web.xml中引用的WebFilter类就是项目启动的时候需要加载的类,也就是你自定义需要实现的方法要写在这里面

WebFilter.java

public class WebFilter implements Filter {

@Override

public void init(FilterConfig filterConfig) throws ServletException {

 

}

 

@Override

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)

throws IOException, ServletException {

System.out.println("自定义实现类的方法");

}

 

@Override

public void destroy() {

 

}

}

回顾@Configuration的使用方式。添加@Configuration注解的类中会存在0个或者多个@Bean。被@Bean注解过的方法会在项目启动的时候跟随调用

@Configuration//用来定义 DispatcherServlet 应用上下文中的 bean、

@EnableWebMvc

@ComponentScan

public class WebConfig extends WebMvcConfigurerAdapter {

/**

*

* 集成统一认证Bean,WebFilter类是统一认证提供的jar包中的工具类,该类实现的功能与web.xml文件中配置的Filter相同。在项目启动时会自动加载

* 该Bean执行统一认证功能。FilterRegistrationBean的详细使用可参考spring的API

*/

@Bean

public FilterRegistrationBean SenseSsoOauthWebFilterFilterRegistration() {

FilterRegistrationBean registration = new FilterRegistrationBean();

registration.setFilter(new WebFilter());//创建上面的自定义的WebFilter对象

registration.addUrlPatterns("/*");

registration.addInitParameter("serverAddress", "http://localhost:8080/IAM");//相当于web.xml中的<param-name>、<param-value>。可以添加n个

registration.addInitParameter("clientId", "APP001");

registration.addInitParameter("clientSecret", "wpprkkmsi093jns2sdf4452sdf");

registration.setName("WebFilter");//该Filter的名字,自己随便定义

registration.setOrder(1);//启动时候的优先级

return registration;

}

@Override

public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

configurer.enable();

}

 

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

super.addResourceHandlers(registry);

}

}

当使用嵌入式的Servlet容器(Tomcate、Jetty等)时我们通过将Servlet、Filter和Listener声明为SpringBean而达到注册效果;或者注册FilterRegistrationBean、ServletListenerRegistrationBean、ServletRegistrationBean的Bean。

以上是实现的web.xml中Filter标签的对应功能,可以通过FilterRegistrationBean类去实现,详细的方法可以参考spring的官方API

四、常见问题

1、将springboot自带tomcate改成外部tomcate方法(这是一个特别多次一举的方法,自己在弄的时候还纳闷为啥自带tomcate还要改成外部的呢)

①首先在启动方法中添加如下代码,只有这样才能在外部tomcate启动的时候加载该项目

 

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplication){

return springApplication.sources(NewCityIoTV3App.class);

}

 

②pom.xml文件修改

<!-- tomcat的支持. -->

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<!-- provided必须添加 -->

<scope>provided</scope>

</dependency>

如下截图,修改生成方式,改成war生成方式

 

2、启动springboot项目内部Tomcate

在打好war的项目文件夹路径下启动cmd输入:java -jar xxx.war

即可启动项目是不是so easy!!!(如何打war、jar包参考上面的详细介绍)

备注:springboot中不建议使用jsp页面做前端的开发,使用thymeleaf模板开发前端html页面是很好的选择,因为jsp在集成到内服servlet的时候会出问题,也就是带有jsp页面的springboot不能打成jar包。

3、springboot集成第三方jar包

①pom.xml中添加本地jar包注入的代码

<dependency>

<groupId>com</groupId><!-- 自己定义 -->

<artifactId>api</artifactId><!-- 自己定义 -->

<version>3.2.2</version><!-- 自己定义 -->

<scope>system</scope><!-- system固定参数 -->

<systemPath>${project.basedir}/libs/api.jar</systemPath><!--${project.basedir}是固定写法,表示项目的根路径,此处是引入第三方jar包的路径 -->

</dependency>

②同时要在build标签中添加配置,在指定位置添加红色加粗部分代码

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration> <includeSystemScope>true</includeSystemScope>

</configuration>

</plugin>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

</plugins>

</build>

在pom配置文件中添加完成以上代码之后再运行clean package命令就能生成成功相应的项目的jar或者war包了

4、jsp中${pageContext.request.contextPath }失效问题

首先:${pageContext.request.contextPath }获得的是项目的根路径,在jsp中添加isELIgnored="false"这样${pageContext.request.contextPath }中的{和}才不会被转译成转义符

 

{、}转义符如下图

参考链接:

https://q.cnblogs.com/q/78956/

https://www.cnblogs.com/zhangminghui/p/4122821.html

https://bbs.csdn.net/topics/310090728

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪头的彩虹糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值