springboot学习
1、回顾Spring
1.1、IOC,DI,AOP
- IOC:控制反转,创建实例对象的控制权从代码转换到Spring容器,实际就是在xml中配置。
- DI:依赖注入,把代码向对象属性或实例对象注入属性值或域属性的控制权限转给Spring容器进行控制。
- AOP:面向切面编程,AOP可以对业务逻辑 的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高 了开发的效率。
1.2、xml配置ioc
<bean id="mybean" class="com.vivi._01_xml_config.MyBean"></bean>
2、SpringBoot
2.1、概念
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
2.2、注解配置ioc
2.2.1、方式1:
a.定义一个类
public class MyBean {
}
b.配置类,相当于是:applicationContext.xml
配置类中定义Bean,表示返回的对象交给Spring容器管理
@Configuration//Spring的配置标签,标记该类是Spring的配置类
public class ApplicationConfig {
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
return myBean;
}
}
c.测试
public class JavaConfigTest {
@Test
public void testMyBean() throws Exception{
//1.加载配置文件
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
//2.通过容器获取bean
MyBean bean = applicationContext.getBean(MyBean.class);
System.out.println(bean);
}
}
2.2.2、方式2:ComponentScan&ComponentScans自动扫描
a.定义一个类
public class MyBean {
}
b.配置类
@Configuration
@ComponentScan//开启扫描,相当于在xml中扫描
public class ApplicationConfig {
}
c.测试
public class JavaConfigTest {
@Test
public void testMyBean() throws Exception{
//1.加载配置文件
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
//2.通过容器获取bean
MyBean bean = applicationContext.getBean(MyBean.class);
System.out.println(bean);
}
}
ComponentScan是ioc组件自动扫描,相当于是 context:component-scan base-package=*
默认扫描当前包,及其子包 ;
ComponentScan.lazyInit :懒初始化
ComponentScan.excludeFilters :排除
注意:如果使用了自动扫描 ,那么配置类中就不要去配置@Bean
2.3、bean的细节
@Configuration
public class ApplicationConfig {
/*
* bean的id:方法名,一般用类名小写
*bean的name:一般不用bean的name。@Bean=(name="myBeanName")
*bean的初始,销毁方法,类中需要有此方法
* bean的scope: @Scope("prototype")
*bean的lazy-init : @Lazy,懒加载
* */
@Bean(initMethod = "init",destroyMethod = "destroy")
public MyBean myBean() {
MyBean myBean = new MyBean();
return myBean;
}
}
2.4、di依赖注入
a.定义两个类,MyBean和OtherBean
public class MyBean {
private String name;
private OtherBean otherBean;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public OtherBean getOtherBean() {
return otherBean;
}
public void setOtherBean(OtherBean otherBean) {
this.otherBean = otherBean;
}
}
public class OtherBean {
}
b.注解配置
@Configuration
public class ApplicationConfig {
/*需求
* 定义Mybean
* 从容器中拿到的Mybean有Otherbean
* 从容器中直接获取到otherbean
* 从容器中拿到的Otherbean要求和MyBean中拿到的Otherbean是同一个
* */
@Bean
public MyBean myBean(OtherBean otherBean ) {
MyBean myBean = new MyBean();
//这样可以通过MyBean获取到OtherBean,不能通过容器拿到
//myBean.setOtherBean(new OtherBean());
//第一种,调用方法,得到两个otherBean是一样的
//myBean.setOtherBean(otherBean());
//第二种,传参
myBean.setOtherBean(otherBean);
//第三种,autowired
//普通属性赋值
myBean.setName("普通属性");
return myBean;
}
@Bean
public OtherBean otherBean() {
return new OtherBean();
}
}
2.5、condition条件标签
@Configuration
public class ApplicationConfig {
/*
* 如果系统是window,定义bean,如果不是则不定义
* @Conditional条件标签
* 指定此方法需要满足某种条件,才会定义
* */
@Conditional(value = WindowsBeanCondition.class)
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
return myBean;
}
}
WindowsBeanCondition类实现了Condition接口
public class WindowsBeanCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
String osName = conditionContext.getEnvironment().getProperty("os.name");
System.out.println(osName);
if (osName.equals("Windows 10")){
return true;
}
return false;
}
}
2.6、import引入标签
2.6.1、引入bean或者配置类
@Configuration
@Import({OtherConfig.class,TempBean.class})
public class ApplicationConfig {
@Bean
public MyBean myBean(OtherBean otherBean) {
MyBean myBean = new MyBean();
myBean.setOtherBean(otherBean);
return myBean;
}
}
2.6.2、导入ImportSelector
@Configuration
@Import(MyImportSelector.class)
public class ApplicationConfig {
}
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{"com.vivi._08_import_selector.MyBean","com.vivi._08_import_selector.OtherBean"};
}
}
2.6.2、导入ImportBeanDefinitionRegistrar
@Configuration
@Import(value = MyImportBeanDefinitionRegistrar.class)
public class ApplicationConfig {
}
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
RootBeanDefinition myBeanDefinition = new RootBeanDefinition(MyBean.class);
RootBeanDefinition otherBeanDefinition = new RootBeanDefinition(OtherBean.class);
registry.registerBeanDefinition("myBean",myBeanDefinition);
registry.registerBeanDefinition("otherBean",otherBeanDefinition);
}
}