一.SpringBoot介绍

1.SpringBoot基本概念

Spring Boot是其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

二.Spring的注解配置

1.spring IOC的使用

1.1xml形式

public class Myben(){}
  <bean id="myBean" class="cn.itsource._01_xml_bean.MyBean"></bean>

1.2 通过注解方式

/*** Spring的配置类 相当于是:applicationContext.xml* @Configuration :Spring的配置标签,标记改类是Spring的配置类*/
@Configuration
public class ApplicationConfig {/*** @Bean : Spring的bean的定义标签 ,标记方法返回的对象交给Spring容器管理*/@Beanpublic MyBean myBean(){return new MyBean();}}

1.3ComponentScan&ComponentScans自动扫描

@Component
public class MyBean {}
/**** @ComponentScan会扫描 @Component @Controller @Service 注解的实体类  value默认不写,* 表示扫描本包下面的所有有该类注解的实体类**/@Configuration
@ComponentScan(value = {"cn.itsource._03_bean_componentscan"},//排除资源excludeFilters = {@ComponentScan.Filter(//通过注解排除type = FilterType.ANNOTATION ,//排除有@Controller注解的类value = Controller.class)})
public class ApplicationConfig {}
2.bean的详情
  • bean的id: 方法名
  • bean的name:可以通过 @Bean标签的name属性指定
  • 生命周期方法:initMethod , destroyMethod
  • 单利多利: @Scope(“prototype”)
  • 懒初始化: @Lazy
 @Bean(name="" , initMethod ="" ,destroyMethod="" )//@Scope("prototype")//@Lazypublic MyBean myBean(){return new MyBean();}
3.依赖注入
  /*** @Bean : Spring的bean的定义标签 ,标记方法返回的对象交给Spring容器管理*  bean的id:  方法名 myBean*  bean的name:可以通过 @Bean标签的name属性指定*  生命周期方法:initMethod , destroyMethod*  单利多利: @Scope("prototype")*/@Bean(initMethod = "init" , destroyMethod = "destroy")public MyBean myBean(OtherBean otherBean){MyBean myBean = new MyBean();myBean.setName("猪儿虫");//myBean.setOtherBean(otherBean());myBean.setOtherBean(otherBean);return myBean ;}@Bean//@Lazypublic OtherBean otherBean(){return new OtherBean();}
4.条件Conditional

Conditional注解帖在bean的定义方法上来判断,如果不满足条件就不会定义bean

1.在Bean的定义方法帖@Conditional

 @Bean@Conditional(value = MyCondition.class)public MyBean windowsMyBean(){return new MyBean("windowMyBean");}@Bean@Conditional(value = LinuxCondition.class)public MyBean linuxMyBean(){return new MyBean("linuxMyBean");}
public class MyCondition implements Condition {/*** 匹配方法,返回值决定是否满足条件*/public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {//获取系统环境String systemName = context.getEnvironment().getProperty("os.name");if("Windows 10".equals(systemName)){return true;}return false;}
}
5.import

1.直接导入Bean或者配置类

@Configuration
@Import(ApplicationOtherConfig.class)  //导入其他的配置类
public class ApplicationConfig

2.导入ImportSelector

public class MyImportSelector implements ImportSelector {//选择导入,该方法返回我们需要导入的类的全限定名public String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{"cn.itsource._07_import_selector.MyBean","cn.itsource._07_import_selector.OtherBean"};}
}
@Configuration
@Import(MyImportSelector.class)    //导入选择器
public class ApplicationConfig
6.FactoryBean

1.定义FactoryBean

public class MyBeanFactoryBean implements FactoryBean<MyBean> {public MyBean getObject() throws Exception {return new MyBean();}public Class<?> getObjectType() {return MyBean.class;}public boolean isSingleton() {return true;}
}

2.配置 MyBeanFactoryBean的bean定义

@Configuration
public class ApplicationConfig {@Beanpublic MyBeanFactoryBean myBean(){return new MyBeanFactoryBean();}
}

3.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes ={ ApplicationConfig.class})
public class AnnoTest {@Autowiredprivate MyBean myBean;@Testpublic void test(){System.out.println(myBean);

三.SpringBoot入门HelloWorld

1.pom文件

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version></parent>
...
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

2.启动类

@SpringBootApplication
public class ApplicationConfig {public static void main(String[] args) {SpringApplication.run(ApplicationConfig.class);}
}

3.编写Controller

@Controller
public class Example {@RequestMapping("/")@ResponseBodyString home() {return "Hello World!";}
}

spring中一些重要注解的理解

  • spring-boot-starter-parent :SpringBoot的父工程,帮我们管理了很多的基础jar包

  • spring-boot-starter-web :SpringBoot和SpringMvc整合的jar包,并且导入了日志,tomcat,等等相关的jar包

  • RestController : Controller+ResponseBody

  • @EnableAutoConfiguration : 开启自动配置功能

  • SpringApplication.run : 启动SpringBoot应用

  • jar :SpringBoot应用默认打jar包

  • SpringBootApplication:包括三个标签组成

    @SpringBootConfiguration - @Configuration : Spring的配置标签

    @EnableAutoConfiguration :开启自动配置

    @ComponentScan :组件自动扫描

spring底层中pom的一些理解
dependencyManagement> 该标签下的jar包,默认是不能被子项目直接使用的 , 他只有声明的功能 , 如果只项目想用这里标签里面的jar包 ,需要显示的写出来 ,而版本号使用父工程的。达到版本号统一管理的效果dependencies> 这个标签下面的jar包默认会被子项目直接继承直接使用

spring+springboot认识相关推荐

  1. Java项目:宠物医院预约挂号系统(java+JSP+Spring+SpringBoot+MyBatis+html+layui+maven+Mysql)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 功能包括: 用户分为宠物,医生,管理员,宠物主人可进行注册选择医生挂号,选择日期,选择号源,医生可进行宠物接诊,管理员可对宠物 ...

  2. Java项目:教务管理系统(java+JSP+Spring+SpringBoot+layui+maven)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 功能包括: 三角色教师 管理员,学生教务管理系统,包括院系管理,课题综合管理,信息管理,以及差旅管理,学生选题等等. 二.项目 ...

  3. 近100个Spring/SpringBoot常用注解汇总!

    作者 | Guide 来源 | JavaGuide(微信公众号) 毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我 ...

  4. 接近8000字的Spring/SpringBoot常用注解总结!安排!

    文章目录 0.前言 1. `@SpringBootApplication` 2. Spring Bean 相关 2.1. `@Autowired` 2.2. `Component`,`@Reposit ...

  5. 基于javaweb+mysql的教务管理系统(java+JSP+Spring+SpringBoot+layui+maven)

    一.项目简述 功能包括: 三角色教师 管理员,学生教务管理系统,包括院系管理,课题综合管理,信息管理,以及差旅管理,学生选题等等. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + ...

  6. 基于javaweb+mysql的教务管理系统(java+jsp+spring+springboot+layui)

    基于javaweb+mysql的教务管理系统(java+jsp+spring+springboot+layui) 运行环境 Java≥8.MySQL≥5.7 开发工具 eclipse/idea/mye ...

  7. Spring/SpringBoot常用注解总结!

    0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot ...

  8. spring springboot springcloud常用注解

    @SpringBootApplication 组合注解,用在启动类上,源码: @Retention(RetentionPolicy.RUNTIME) @SpringBootConfiguration ...

  9. 学习笔记之-Activiti7工作流引擎,概述,环境搭建,类关系图,使用Activiti BPMN visualizer,流程变量,组任务 网关,Activiti整合Spring SpringBoot

    本篇学习笔记是观看黑马程序员Activiti7视频而得 Activiti7 一.工作流介绍 1.1 概念 工作流(Workflow),就是通过计算机对业务流程自动化执行管理.它主要解决的是" ...

  10. spring springboot websocket 不能注入( @Autowired ) service bean 报 null 错误

    spring 或 springboot 的 websocket 里面使用 @Autowired 注入 service 或 bean 时,报空指针异常,service 为 null(并不是不能被注入). ...

最新文章

  1. Zabbix 中文乱码解决
  2. [MySQL FAQ]系列 -- 为何授权不对
  3. 图像处理与识别的算法中若有非线性变换,那么请一定注意归一化的处理会影响结果
  4. (旧)子数涵数·Flash——遮罩动画
  5. hession调用json解析异常 com.caucho.hessian.io.HessianProtocolException: expected integer at 0x74 java.util
  6. Madagascar的自定义浮点型函数--对数函数
  7. 100_1小记ressons analysis
  8. 李洪强iOS经典面试题30-一个区分度很大的面试题
  9. 生产级mysql双写_生产级Mysql物理全量备份-Xtrabackup
  10. 易辅客栈 从零学辅助_如何从零启动辅助项目
  11. 空间四点定位原理及应用
  12. c# winform 制作统计图
  13. WindowsCluster 由于在更新安全DNS区域时访问被拒绝,群集网络资源无法注册一个或多个关联的DNS名称
  14. R 中 facet_wrap() 和 facet_grid() 的区别
  15. Retrofit实现App更新
  16. 将物理机迁移到ESXI上
  17. 柬埔寨招聘中文计算机,柬埔寨ll中文老师1000美金+招聘机会来啦,快来围观!!!...
  18. 书单素材怎么找?教你寻找方法
  19. Linux Ubuntu常用软件
  20. 图书信息管理系统(二)

热门文章

  1. 蓝桥杯 PREV-43 拉马车(试题解析)
  2. numpy PIL tensor之间的相互转换
  3. 下一代iPhone什么样
  4. Adobe国际认证证书介绍
  5. 开拓者探地雷达BS-M
  6. 爬虫工程师想拿更高薪,这点不要忽略
  7. LiteOS学习---开发环境初识
  8. hangye5:09年做行业网址导航不如做行业网站联盟
  9. 计算机网络实验35步骤,计算机网络模拟器实验报告(1).(35页)-原创力文档
  10. 瑞萨单片机RL78-时钟