栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot框架知识点整理(一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot框架知识点整理(一)

1.springboot的特点 1.1 依赖管理
  1. 配置文件里见到很多 spring-boot-starter-*: *就某种场景
  2. 只要引入starter,这个场景的所有常规需要的依赖都会自动引入

官方地址:

https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter

  1. 见到 *-spring-boot-starter: 就是第三方为提供的简化开发的场景启动器。
  2. 默认版本号查看方式:pom配置点击—>spring-boot-starter-parent —> spring-boot-dependencies
  3. 修改默认的版本依赖

        5.1.43
    
1.2 自动装配
  1. 自动配好了tomcat依赖:

      org.springframework.boot
      spring-boot-starter-tomcat
      2.3.4.RELEASE
      compile

  1. 自动配好SpringMVC常用功能;
  2. 自动配好了web常见的功能:如字符串编码的问题。
  3. 无需包扫描配置:主程序所在包及其下面的所有子包都会被扫描进来。想要改变扫描路径
    ■ @SpringBootApplication(scanbasePackages=“com.test”)
    ■ 或者@ComponentScan 指定扫描路径
  4. 按需加载自动配置项: 根据需求引入spring-boot-starter-*,自动开启相关配置
2.容器功能 2.1、组件功能

1、@Configuration+@Bean
基本使用:

@Configuration(proxyBeanMethods = false)
public class BootConfig {

    @Bean("beanColor")//给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
    public Color beanColor(){
        return new Color("李沁","迪丽热巴");
    }

    @Bean
    public Red beanRed(){
        return new Red();
    }
}

2、@import
2.1. @import的基本使用

import com.example.springboottest.entity.Color;
import com.example.springboottest.entity.Red;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.import;

@import({Color.class,Red.class})
@Configuration(proxyBeanMethods = false)
public class BootConfig {
}

2.2. @import+ 接口importSelector 的使用

//配置类加注解
@import({MyimportSelector.class})
import org.springframework.context.annotation.importSelector;
import org.springframework.core.type.Annotationmetadata;

//自定义批量注入Bean组件
public class MyimportSelector implements importSelector {

    //返回值,就是导入到容器中的组件全类名
    //Annotationmetadata:当前注解@import注解类的所有注解信息
    @Override
    public String[] selectimports(Annotationmetadata importingClassmetadata) {
        //返回null会报错
        return new String[]{"com.example.springboottest.entity.Color","com.example.springboottest.entity.Red"};
    }
}

2.3. @import+ 接口importSelector 的使用

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.importBeanDefinitionRegistrar;
import org.springframework.core.type.Annotationmetadata;

public class MyimportBeanDef implements importBeanDefinitionRegistrar{
    
    @Override
    public void registerBeanDefinitions(Annotationmetadata annotationmetadata, BeanDefinitionRegistry registry) {
        //检查Color类是否注册到容器中
        Boolean definition=registry.containsBeanDefinition("color");
        if(!definition){
            //指定对应的Bean
            RootBeanDefinition rootBeanDefinition=new RootBeanDefinition("com.example.springboottest.entity.Yellow");
            //指定注入到容器的名字
            registry.registerBeanDefinition("yellow",rootBeanDefinition);
        }

    }
}

3、其它XML配置文件引入
3.1、@importResource

	


    
        
        
    

@Configuration(proxyBeanMethods = false)
@importResource("classpath:beanconfig.xml")
public class BootConfig {
}

4、条件装配注解@Conditional
满足Conditional指定条件的,则进行组件注入

//@import({MyimportBeanDef.class}) 
//import和importResource执行顺序在@Bean之后
@Configuration(proxyBeanMethods = false)
public class BootConfig {

    @Bean("green")
    public Green getGreenName(){
        return new Green();
    }

    @ConditionalOnBean(name="green")//如果容器中存在green的组件,则注入
    @Bean("black123")
    public Black getBlackName(){
        return new Black();
    }
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringboottestApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpringboottestApplication.class, args);
        //容器中是否存在该组件
        System.out.println(run.containsBeanDefinition("black123"));
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685944.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号