Java - Spring boot - (初嗅)
发布日期:2021-06-30 19:50:48
浏览次数:2
分类:技术文章
本文共 9061 字,大约阅读时间需要 30 分钟。
基础概念
1. 依赖注入(DI)
ApplicationContext Spring IOC 容器负责创建Bean和维护Bean之间的依赖关系
2. 控制反转(IOC)
通过容器来实现对象组件的装配和管理,是通过依赖注入实现的
3. 注解配置
3.1 声明
- @Component
- @Service
- @Repository
- @Controller
3.2 注入
- @Autowired
- @Inject
- @Resource
4. Java 配置
4.1 概念
Spring 4.x 推荐使用Java 配置
- @Configuration,相当与Spring配置的xml文件
- @Bean,当前方法返回值为一个Bean
4.2 原则
- 全局配置使用Java 配置
- 业务Bean使用注解配置
4.3. AOP
- @Aspect
- @After, @Before, @Around
Spring 常用配置
1. @Scope 新建Bean 的实例
2. @Value Spring EL,注入资源
3. 事件
- 自定义事件,ApplicationEvent
- 事件监听器, ApplicationListener
- 容器发布事件
4. @Profile,实例化不同的Bean
Spring 高级
1. 多线程
使用ThreadPoolTaskExecutor 实现一个基于线程池的TaskExecutor,然后@EnableAsync开启异步支持并通过Bean中的@Async注解来声明一个异步任务
2. 计划任务
- @EnableScheduling 开启对计划任务的支持
- @Scheduled, 声明计划任务
3. 条件注解,@Conditional,根据满足某一个特定条件创建一个特定的Bean
4. 组合注解与元注解
- 元注解,最原来的注解
- 组合注解,多个元注解注解后的注解,应用到Bean
Spring Boot 基础概念
- 独立运行Spring 项目,只需要java -jar xxx.jar
- 内嵌Servlet 容器, 无需使用war包部署
- 使用starter 简化maven 配置
- 提供基于http, ssh, telnet 对项目进行监控
- 无代码生成和xml配置
Hello Spring Boot !
1. pom.xml
4.0.0 peerslee yierling 0.0.1-SNAPSHOT jar yierling http://maven.apache.org org.springframework.boot spring-boot-starter-parent 1.3.3.RELEASE 1.8 UTF-8 org.springframework.boot spring-boot-starter-web junit junit org.springframework.boot spring-boot-maven-plugin
2. App.Java
package peerslee.yierling;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Hello world! * */@RestController@SpringBootApplication //Spring Boot 项目和核心注解,开启自动配置public class App { @RequestMapping("/") String index() { return "Hello Spring Boot!"; } public static void main( String[] args ) throws Exception { SpringApplication.run(App.class, args); }}
3. Run
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.3.RELEASE)2017-06-04 15:28:13.006 INFO 7312 --- [ main] peerslee.yierling.App : Starting App on WIN10-703302300 with PID 7312 (D:\code\eclipse\yierling\target\classes started by Administrator in D:\code\eclipse\yierling)2017-06-04 15:28:13.008 INFO 7312 --- [ main] peerslee.yierling.App : No active profile set, falling back to default profiles: default2017-06-04 15:28:13.052 INFO 7312 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@df27fae: startup date [Sun Jun 04 15:28:13 CST 2017]; root of context hierarchy2017-06-04 15:28:13.575 INFO 7312 --- [ 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]]2017-06-04 15:28:14.039 INFO 7312 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2017-06-04 15:28:14.091 INFO 7312 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat2017-06-04 15:28:14.092 INFO 7312 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.322017-06-04 15:28:14.191 INFO 7312 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2017-06-04 15:28:14.191 INFO 7312 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1142 ms2017-06-04 15:28:14.445 INFO 7312 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2017-06-04 15:28:14.451 INFO 7312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2017-06-04 15:28:14.452 INFO 7312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2017-06-04 15:28:14.452 INFO 7312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2017-06-04 15:28:14.452 INFO 7312 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2017-06-04 15:28:14.672 INFO 7312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@df27fae: startup date [Sun Jun 04 15:28:13 CST 2017]; root of context hierarchy2017-06-04 15:28:14.722 INFO 7312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String peerslee.yierling.App.index()2017-06-04 15:28:14.725 INFO 7312 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2017-06-04 15:28:14.725 INFO 7312 --- [ 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)2017-06-04 15:28:14.746 INFO 7312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-06-04 15:28:14.746 INFO 7312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-06-04 15:28:14.778 INFO 7312 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-06-04 15:28:14.861 INFO 7312 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2017-06-04 15:28:14.929 INFO 7312 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2017-06-04 15:28:14.934 INFO 7312 --- [ main] peerslee.yierling.App : Started App in 2.134 seconds (JVM running for 2.432)2017-06-04 15:28:36.706 INFO 7312 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'2017-06-04 15:28:36.707 INFO 7312 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2017-06-04 15:28:36.721 INFO 7312 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 14 ms
4. 测试
http://localhost:8080/
问题
1. 使用maven 更新项目
Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix. yierling line 1 Maven Configuration Problem
2. localhost:8080拒绝访问解决方案(win10系统)
- 打开“我的电脑”,选择左上角的“计算机”中的“卸载或更改程序”。
- 点击“启用或关闭Windows功能”。
- 选中“Internet Information Service”
转载地址:https://lipenglin.blog.csdn.net/article/details/72851705 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月11日 11时37分50秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python一键获取微信推送封面图
2019-04-30
油猴脚本:微信推送浏览功能拓展
2019-04-30
JavaScript DOM对象操作详解
2019-04-30
JavaScript 表单操作与MD5加密
2019-04-30
JAVA学习笔记4 - 循环与分支结构
2019-04-30
JAVA学习笔记6 - 数组
2019-04-30
JAVA学习笔记8 - Stream 和 File I/O
2019-04-30
JAVA学习笔记9 - 异常
2019-04-30
JAVA学习笔记10 - 继承
2019-04-30
JAVA学习笔记11 - 接口interface
2019-04-30
JAVA学习笔记12 - 包package
2019-04-30
Android 开发学习笔记 00 - Getting Started
2019-04-30
【学习笔记】Android Activity
2019-04-30
【学习笔记】Android Fragments
2019-04-30
Android使用Retrofit_00_Getting Started
2019-04-30
Android使用Retrofit_01_OAuth2 + GitHub
2019-04-30
Django + REST学习笔记
2019-04-30