spring+springboot认识
一.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认识相关推荐
- Java项目:宠物医院预约挂号系统(java+JSP+Spring+SpringBoot+MyBatis+html+layui+maven+Mysql)
源码获取:博客首页 "资源" 里下载! 一.项目简述 功能包括: 用户分为宠物,医生,管理员,宠物主人可进行注册选择医生挂号,选择日期,选择号源,医生可进行宠物接诊,管理员可对宠物 ...
- Java项目:教务管理系统(java+JSP+Spring+SpringBoot+layui+maven)
源码获取:博客首页 "资源" 里下载! 一.项目简述 功能包括: 三角色教师 管理员,学生教务管理系统,包括院系管理,课题综合管理,信息管理,以及差旅管理,学生选题等等. 二.项目 ...
- 近100个Spring/SpringBoot常用注解汇总!
作者 | Guide 来源 | JavaGuide(微信公众号) 毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我 ...
- 接近8000字的Spring/SpringBoot常用注解总结!安排!
文章目录 0.前言 1. `@SpringBootApplication` 2. Spring Bean 相关 2.1. `@Autowired` 2.2. `Component`,`@Reposit ...
- 基于javaweb+mysql的教务管理系统(java+JSP+Spring+SpringBoot+layui+maven)
一.项目简述 功能包括: 三角色教师 管理员,学生教务管理系统,包括院系管理,课题综合管理,信息管理,以及差旅管理,学生选题等等. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + ...
- 基于javaweb+mysql的教务管理系统(java+jsp+spring+springboot+layui)
基于javaweb+mysql的教务管理系统(java+jsp+spring+springboot+layui) 运行环境 Java≥8.MySQL≥5.7 开发工具 eclipse/idea/mye ...
- Spring/SpringBoot常用注解总结!
0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot ...
- spring springboot springcloud常用注解
@SpringBootApplication 组合注解,用在启动类上,源码: @Retention(RetentionPolicy.RUNTIME) @SpringBootConfiguration ...
- 学习笔记之-Activiti7工作流引擎,概述,环境搭建,类关系图,使用Activiti BPMN visualizer,流程变量,组任务 网关,Activiti整合Spring SpringBoot
本篇学习笔记是观看黑马程序员Activiti7视频而得 Activiti7 一.工作流介绍 1.1 概念 工作流(Workflow),就是通过计算机对业务流程自动化执行管理.它主要解决的是" ...
- spring springboot websocket 不能注入( @Autowired ) service bean 报 null 错误
spring 或 springboot 的 websocket 里面使用 @Autowired 注入 service 或 bean 时,报空指针异常,service 为 null(并不是不能被注入). ...
最新文章
- Zabbix 中文乱码解决
- [MySQL FAQ]系列 -- 为何授权不对
- 图像处理与识别的算法中若有非线性变换,那么请一定注意归一化的处理会影响结果
- (旧)子数涵数·Flash——遮罩动画
- hession调用json解析异常 com.caucho.hessian.io.HessianProtocolException: expected integer at 0x74 java.util
- Madagascar的自定义浮点型函数--对数函数
- 100_1小记ressons analysis
- 李洪强iOS经典面试题30-一个区分度很大的面试题
- 生产级mysql双写_生产级Mysql物理全量备份-Xtrabackup
- 易辅客栈 从零学辅助_如何从零启动辅助项目
- 空间四点定位原理及应用
- c# winform 制作统计图
- WindowsCluster 由于在更新安全DNS区域时访问被拒绝,群集网络资源无法注册一个或多个关联的DNS名称
- R 中 facet_wrap() 和 facet_grid() 的区别
- Retrofit实现App更新
- 将物理机迁移到ESXI上
- 柬埔寨招聘中文计算机,柬埔寨ll中文老师1000美金+招聘机会来啦,快来围观!!!...
- 书单素材怎么找?教你寻找方法
- Linux Ubuntu常用软件
- 图书信息管理系统(二)