spring boot简易入门
2016-01-25 17:20
771 查看
工作需要搭建一个spring boot,通过百度看了下这个微框架的一些资料,本地简易的搭建了一个
1.eclips新建一个maven工程,不一定要web工程,新建后然后建立相应的代码控制器,service,启动的包名,如下
2.配置pom文件
3.启动应用,实现浏览器地址栏输入http://localhost:8080/loginUser,返回OK
启动应用实现:
控制器段代码
业务层代码
最后在src/main/resources目录下创建一个application.properties,并配置jsp的跳转路径,如果不配置会找不到页面路径,如果全是
在AppStart类中运行main
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.1.RELEASE)
2016-01-25 16:27:28.687 INFO 7668 --- [ main] com.linkon.ec.springboot.start.AppStart : Starting AppStart on lenovo-PC with PID 7668 (started by lenovo in D:\Program Files\workspace\springboot)
2016-01-25 16:27:28.701 INFO 7668 --- [ main] com.linkon.ec.springboot.start.AppStart : No active profile set, falling back to default profiles: default
2016-01-25 16:27:28.838 INFO 7668 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@77d0bef0: startup date [Mon Jan 25 16:27:28 CST 2016]; root of context hierarchy
2016-01-25 16:27:30.117 INFO 7668 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-01-25 16:27:31.342 INFO 7668 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-25 16:27:31.362 INFO 7668 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-01-25 16:27:31.363 INFO 7668 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-25 16:27:31.535 INFO 7668 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-01-25 16:27:31.536 INFO 7668 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2706 ms
2016-01-25 16:27:32.060 INFO 7668 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-25 16:27:32.065 INFO 7668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-25 16:27:32.065 INFO 7668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-25 16:27:32.065 INFO 7668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-25 16:27:32.066 INFO 7668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-01-25 16:27:32.342 INFO 7668 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@77d0bef0: startup date [Mon Jan 25 16:27:28 CST 2016]; root of context hierarchy
2016-01-25 16:27:32.448 INFO 7668 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[GET]}" onto public java.lang.String com.linkon.ec.springboot.controller.UserController.login()
2016-01-25 16:27:32.450 INFO 7668 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/loginUser],methods=[GET]}" onto public java.lang.String com.linkon.ec.springboot.controller.UserController.loginUser()
2016-01-25 16:27:32.456 INFO 7668 --- [ 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)
2016-01-25 16:27:32.457 INFO 7668 --- [ 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)
2016-01-25 16:27:32.512 INFO 7668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-25 16:27:32.512 INFO 7668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-25 16:27:32.608 INFO 7668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-25 16:27:32.876 INFO 7668 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-01-25 16:27:32.985 INFO 7668 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-25 16:27:32.992 INFO 7668 --- [ main] com.linkon.ec.springboot.start.AppStart : Started AppStart in 4.991 seconds (JVM running for 5.453)
2016-01-25 16:27:47.107 INFO 7668 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-25 16:27:47.108 INFO 7668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-25 16:27:47.127 INFO 7668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
关于日志文件配置
使用自定义的logback
1、在src/main/resources目录下创建logback.xml文件,内容如下:
2、在application.properties中指定logback.xml和日志生成的路径,如下:
生成的日志文件
1.eclips新建一个maven工程,不一定要web工程,新建后然后建立相应的代码控制器,service,启动的包名,如下
2.配置pom文件
<!-- 父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.1.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- 导入jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3.启动应用,实现浏览器地址栏输入http://localhost:8080/loginUser,返回OK
启动应用实现:
//@SpringbootApplication自动扫描注入配置的包路径下的bean @SpringBootApplication(scanBasePackages={"com.linkon.ec.springboot.controller,com.linkon.ec.springboot.service"}) public class AppStart extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(AppStart.class); } public static void main(String[] args) { //SpringApplication 程序入口 SpringApplication.run(AppStart.class, args); } }
控制器段代码
@Controller public class UserController { @Autowired UserService userService; @RequestMapping(value = "/login", method = RequestMethod.GET) public String login() { return "login"; } @RequestMapping(value = "/loginUser", method = RequestMethod.GET) @ResponseBody public String loginUser() { userService.loginUser(); return userService.loginUser(); } }
业务层代码
@Service public class UserService { public String loginUser() { System.out.println("logi...."); return "OK"; } }
最后在src/main/resources目录下创建一个application.properties,并配置jsp的跳转路径,如果不配置会找不到页面路径,如果全是
@ResponseBody注解的方法则不需要配置
</pre></div><div style="font-family:微软雅黑; font-size:14px; line-height:21px; widows:auto"><div style="background-color:inherit"><pre name="code" class="html">spring.mvc.view.prefix:/jsp/ spring.mvc.view.suffix:.jsp
在AppStart类中运行main
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.1.RELEASE)
2016-01-25 16:27:28.687 INFO 7668 --- [ main] com.linkon.ec.springboot.start.AppStart : Starting AppStart on lenovo-PC with PID 7668 (started by lenovo in D:\Program Files\workspace\springboot)
2016-01-25 16:27:28.701 INFO 7668 --- [ main] com.linkon.ec.springboot.start.AppStart : No active profile set, falling back to default profiles: default
2016-01-25 16:27:28.838 INFO 7668 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@77d0bef0: startup date [Mon Jan 25 16:27:28 CST 2016]; root of context hierarchy
2016-01-25 16:27:30.117 INFO 7668 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-01-25 16:27:31.342 INFO 7668 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-25 16:27:31.362 INFO 7668 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-01-25 16:27:31.363 INFO 7668 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-25 16:27:31.535 INFO 7668 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-01-25 16:27:31.536 INFO 7668 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2706 ms
2016-01-25 16:27:32.060 INFO 7668 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-25 16:27:32.065 INFO 7668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-25 16:27:32.065 INFO 7668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-25 16:27:32.065 INFO 7668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-25 16:27:32.066 INFO 7668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-01-25 16:27:32.342 INFO 7668 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@77d0bef0: startup date [Mon Jan 25 16:27:28 CST 2016]; root of context hierarchy
2016-01-25 16:27:32.448 INFO 7668 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[GET]}" onto public java.lang.String com.linkon.ec.springboot.controller.UserController.login()
2016-01-25 16:27:32.450 INFO 7668 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/loginUser],methods=[GET]}" onto public java.lang.String com.linkon.ec.springboot.controller.UserController.loginUser()
2016-01-25 16:27:32.456 INFO 7668 --- [ 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)
2016-01-25 16:27:32.457 INFO 7668 --- [ 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)
2016-01-25 16:27:32.512 INFO 7668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-25 16:27:32.512 INFO 7668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-25 16:27:32.608 INFO 7668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-25 16:27:32.876 INFO 7668 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-01-25 16:27:32.985 INFO 7668 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-25 16:27:32.992 INFO 7668 --- [ main] com.linkon.ec.springboot.start.AppStart : Started AppStart in 4.991 seconds (JVM running for 5.453)
2016-01-25 16:27:47.107 INFO 7668 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-25 16:27:47.108 INFO 7668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-25 16:27:47.127 INFO 7668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
关于日志文件配置
使用自定义的logback
1、在src/main/resources目录下创建logback.xml文件,内容如下:
<!-- Logback configuration. See http://logback.qos.ch/manual/index.html --> <configuration scan="true" scanPeriod="10 seconds"> <include resource="org/springframework/boot/logging/logback/base.xml" /> <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${LOG_PATH}/info.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/info-%d{yyyyMMdd}.log.%i </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>500MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>2</maxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n </Pattern> </layout> </appender> <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <File>${LOG_PATH}/error.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/error-%d{yyyyMMdd}.log.%i </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>500MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>2</maxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n </Pattern> </layout> </appender> <!-- hibernate日志输入 --> <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" /> <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="TRACE" /> <logger name="org.hibernate.SQL" level="DEBUG" /> <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" /> <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" /> <root level="INFO"> <appender-ref ref="INFO_FILE" /> <appender-ref ref="ERROR_FILE" /> </root> </configuration>
2、在application.properties中指定logback.xml和日志生成的路径,如下:
logging.config=classpath:logback.xml logging.path=/bootlog/log
生成的日志文件
相关文章推荐
- The Java™ Tutorials —— Generics:前言
- Struts2设置默认的action
- Spring起步--helloworld
- 关于 Java 数组的 12 个最佳方法
- 【Eclipse】--Eclipse简介和安装
- 摘抄java基础
- Bean在Spring容器中的生命周期
- java 获取前几天时间
- struts2标签学习笔记(一)
- 【spring-mvc】spring mvc中得/和/*的区别
- Java基础之----异常处理(1.7以上)
- Java基础第一节
- Struts2 访问web元素
- spring注解注入失败一个原因
- spring jdbc多条件查询(参数化传参,防止sql注入风险)
- spring 与jedis2.7 集合
- java实现cassandra的增删改查
- 破解MyEclipse2013注册码
- javaweb dev 入
- SpringMVC的各种参数绑定方式