spring-boot简单认识

1.Spring Boot

IOC

IOC: **Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。**在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。

DI

DI—Dependency Injection,即“依赖注入”**:**组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。依赖注入的目的并非为软件系统带来更多功能,而是为了提升组件重用的频率,并为系统搭建一个灵活、可扩展的平台。

AOP

1.简单ApplicationConfig 配置类

打注解

@Configuration :Spring的配置标签,打在类上,该类可以被识别为Spring的配置类

类中配置 MyBean

@Bean : Spring的Bean定义标签,打在方法上,方法返回的实例可以返回给spring容器

@Bean
public MyBean myBean(){
	MyBean myBean = new MyBean();
	return myBean;
}

2.扫描注解ApplicationConfig配置

1.MyBean打注解

@Component : 定义Bean 该类标签需要被扫描

2.配置类中开启IOC自动扫描(若没有限制扫描的包路径,默认扫描当前包,及其子包,basePackages指定扫描的路径)

@ComponentScan(basePackages = “包的路径…”) :开启扫描

3.Bean的详解

/**
*id:方法的名字
*name:通过@Bean.name属性指定,一般不管
*lazy-init:通过标签指定 @Lazy
*scope:标签指定@Scope-----单例还是多例
*init:@Bean.initMethod属性
*destroy:@Bean.destroyMethod属性
*初始化和销毁的方法写在MyBean类中
*/
@Bean(name = "xxx" , initMethod = "init" ,......)
@Lazy
@Scope
public MyBean myBean(){
	MyBean myBean = new MyBean();
	return myBean;
}

注意测试类

@ContextConfiguration(classes = ApplicationConfig.class)

就可以自动注入容器或者直接注入Bean

MyBean中的属性注入

1.MyBean中有对应的普通字段和对象

@Bean
public MyBean myBean(OtherBean otherBean){
	MyBean myBean = new MyBean();
    //普通字段直接set
    myBean.setUsername("蔡徐坤");
    //设置OtherBean的值
    /**
    * 依赖注入DI的方法
    * 1.调方法--容器中有直接取来用,没有则新创建
    * myBean.setOtherBean(otherBean());
    * 2.传参数
    * 3.容器中自动注入
    */
    myBean.setOtherBean(otherBean);
	return myBean;
}

@Bean
public OtherBean otherBean(){
    return new OtherBean();
}

Condition(了解)

根据不同的情况获取不同的Bean

1.ApplicationConfig

//@Conditional这个注解也可以打在类上面
@Bean
@Conditional(value = BeanCreateCondition.class)
public MyBean windowsBean(){
	MyBean myBean = new MyBean();
    myBean.setSystm("windows");
	return myBean;
}

@Bean
public MyBean linuxBean(){
	MyBean myBean = new MyBean();
    myBean.setSystm("linux");
	return myBean;
}

2.自己写一个类实现 Condition接口

public class BeanCreateCondition implements Condition{
    //条件匹配的方法
    public boolean matches(ConditionContext contex,...................){
        //获取操作系统名字
    }
}

@import:类的导入

导入配置文件,在主配置文件上打注解

1.导入其他配置类

@import(ApplicationOtherConfig.class)

2.导入普通的类,这样既不用扫描也不用在配置文件中配置就可以自动注入

@Import({ApplicationOtherConfig.class,TempBean.class})

3.导入ImportSelector 选择器

自定义一个类实现ImportSelector,然后在配置文件中导入MyImportSelector

public class MyImportSelector implements ImportSelector {
	
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"cn.dsq.import_selector.MyBean" ,
                "cn.dsq.import_selector.OtherBean"};
    }
}

4.导入ImportBeanDefinitionRegistrar 注册器,配置类中导入MyImportBeanDefinitionRegistrar

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    //BeanDefinitionRegistry 注册bean
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        registry.registerBeanDefinition("myBean",new RootBeanDefinition(MyBean.class));
        registry.registerBeanDefinition("otherBean",new RootBeanDefinition(OtherBean.class));
    }
}

FactoryBean

自己写一个类实现FactoryBean并传入对应的对象

public class MyBeanFactoryBean implements FactoryBean<MyBean> {
    //返回实例对象
    public MyBean getObject() throws Exception {
        return new MyBean();
    }
    //返回bean的类型
    public Class<?> getObjectType() {
        return MyBean.class;
    }
    //是否单例模式
    public boolean isSingleton() {
        return true;
    }
}

然后配置文件中直接配置这个工厂

@Bean
    public MyBeanFactoryBean myBean(){
        return new MyBeanFactoryBean();
    }

Bean的生命周期

1.Bean+InitMethod+DestoryMethod
@Bean(InitMethod="" , DestoryMethod="")
2.InitializingBean, DisposableBean
public class MyBean implements InitializingBean, DisposableBean {
    public MyBean(){
        System.out.println("创建.............");
    }
    public void destroy() throws Exception {
        System.out.println("destroy方法...........");
    }
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化方法...........");
    }
}
3.PostConstruct+PreDestroy
@Component
public class MyBean {

    @PostConstruct
    public void init(){
        System.out.println("init.....");
    }
    @PreDestroy
    public void destory(){
        System.out.println("destory.....");
    }
}

4.BeanPostProcessor后置处理器

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化之前.....:"+bean);
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化之后.....:"+bean);
        return bean;
    }
}

自动配置

1)SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration

2)@EnableAutoConfiguration 作用:

- 利用EnableAutoConfigurationImportSelector给容器中导入一些组件?

- 可以查看selectImports()方法的内容;

- List configurations = getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置
在这里插入图片描述将 类路径下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中;
在这里插入图片描述每一个这样的 xxxAutoConfiguration类都是容器中的一个组件,都加入到容器中;用他们来做自动配置;

每一个自动配置类进行自动配置功能;

3)以HttpEncodingAutoConfiguration(Http编码自动配置)为例解释自动配置原理;

@Configuration //表示这是一个配置类,以前编写的配置文件一样,也可以给容器中添加组件

@EnableConfigurationProperties(HttpEncodingProperties.class) //启动指定类的ConfigurationProperties功能;将配置文件中对应的值和HttpEncodingProperties绑定起来;并把HttpEncodingProperties加入到ioc容器中

@ConditionalOnWebApplication //Spring底层@Conditional注解(Spring注解版),根据不同的条件,如果满足指定的条件,整个配置类里面的配置就会生效; 判断当前应用是否是web应用,如果是,当前配置类生效

@ConditionalOnClass(CharacterEncodingFilter.class) //判断当前项目有没有这个类CharacterEncodingFilter;SpringMVC中进行乱码解决的过滤器;

@ConditionalOnProperty(prefix = “spring.http.encoding”, value = “enabled”, matchIfMissing = true) //判断配置文件中是否存在某个配置 spring.http.encoding.enabled;如果不存在,判断也是成立的

//即使我们配置文件中不配置pring.http.encoding.enabled=true,也是默认生效的;

public class HttpEncodingAutoConfiguration {
 	//他已经和SpringBoot的配置文件映射了
 	private final HttpEncodingProperties properties;
  //只有一个有参构造器的情况下,参数的值就会从容器中拿
 	public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
		this.properties = properties;
	}
}
    
      @Bean   //给容器中添加一个组件,这个组件的某些值需要从properties中获取
	@ConditionalOnMissingBean(CharacterEncodingFilter.class) //判断容器没有这个组件?
	public CharacterEncodingFilter characterEncodingFilter() {
		CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
		filter.setEncoding(this.properties.getCharset().name());
		filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
		filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
		return filter;
	}

根据当前不同的条件判断,决定这个配置类是否生效?

一但这个配置类生效;这个配置类就会给容器中添加各种组件;这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的;

4)、所有在配置文件中能配置的属性都是在xxxxProperties类中封装者‘;配置文件能配置什么就可以参照某个功能对应的这个属性类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值