SpringBoot学习二:application.porperties中的配置不生效导致改端口后不生效原因,无法加载resource下的资源文件

application.porperties中的配置不生效导致改端口后不生效原因,无法加载resource下的资源文件


springboot项目新建后,application.porperties文件放置路径为/springboot01/src/main/resources

但更改端口后

server.port=8081
server.context-path=/springboot01

hello=\u6211\u7231\u4F60

jdbc.url=jdbc://mysql:localhost11
jdbc.username=root
jdbc.password=sa
jdbc.driver=com.mysql.driver

启动仍是8080端口

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.6.RELEASE)

2018-05-12 13:50:12.959  INFO 7604 --- [           main] com.sm.Springboot01Application           : Starting Springboot01Application on py2014-PC with PID 7604 (D:\note\springboot01\target\classes started by py2014 in D:\note\springboot01)
2018-05-12 13:50:12.964  INFO 7604 --- [           main] com.sm.Springboot01Application           : No active profile set, falling back to default profiles: default
2018-05-12 13:50:13.152  INFO 7604 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7a13f4c: startup date [Sat May 12 13:50:13 CST 2018]; root of context hierarchy
2018-05-12 13:50:15.669  INFO 7604 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8081 (http)
2018-05-12 13:50:15.689  INFO 7604 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-05-12 13:50:15.691  INFO 7604 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.16
2018-05-12 13:50:15.912  INFO 7604 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/springboot01]  : Initializing Spring embedded WebApplicationContext
2018-05-12 13:50:15.913  INFO 7604 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2766 ms
2018-05-12 13:50:16.298  INFO 7604 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-05-12 13:50:16.306  INFO 7604 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-12 13:50:16.306  INFO 7604 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-12 13:50:16.307  INFO 7604 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-12 13:50:16.307  INFO 7604 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-12 13:50:17.154  INFO 7604 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7a13f4c: startup date [Sat May 12 13:50:13 CST 2018]; root of context hierarchy
2018-05-12 13:50:17.301  INFO 7604 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/showJdbc]}" onto public java.lang.String com.sm.controller.HelloController.showJdbc()
2018-05-12 13:50:17.302  INFO 7604 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.sm.controller.HelloController.say()
2018-05-12 13:50:17.309  INFO 7604 --- [           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)
2018-05-12 13:50:17.309  INFO 7604 --- [           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)
2018-05-12 13:50:17.382  INFO 7604 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-12 13:50:17.383  INFO 7604 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-12 13:50:17.484  INFO 7604 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-12 13:50:18.146  INFO 7604 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-05-12 13:50:18.308  INFO 7604 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-05-12 13:50:18.389  INFO 7604 --- [           main] com.sm.Springboot01Application           : Started Springboot01Application in 6.046 seconds (JVM running for 7.371)
2018-05-12 13:50:18.498  INFO 7604 --- [nio-8081-exec-2] o.a.c.c.C.[.[localhost].[/springboot01]  : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-05-12 13:50:18.499  INFO 7604 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-05-12 13:50:18.533  INFO 7604 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 34 ms


经过查各种资料,问题得以解决,解决方案为:

第一种办法:

把/springboot01/src/main/resources路径下的application.porperties放置根目录下,即可成功启动。


第二种办法:

Java Build Path ->Source->Excluded置为None


第三种办法

重写WebMvcConfigurerAdapter的addResourceHandlers方法

package com.sm.app;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
	 @Override
	    public void addResourceHandlers(ResourceHandlerRegistry registry) {
	        registry.addResourceHandler("/**").addResourceLocations("classpath:/");
	        super.addResourceHandlers(registry);
	    }
}


三种方法,最终我是用第二种方法解决的,搜了好多资料,最终整理到这些


希望能帮助到大家

猜你喜欢

转载自blog.csdn.net/cuipeng1019/article/details/80291136