springBoot2-底层注解

@Accessors(chain=true)

该注解设置为chain=true,生成setter方法返回this(也就是返回的是对象),代替了默认的返回void,可以使用链式的set

package com.pollyduan;

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain=true)
public class User {
    private Integer userId;
    private String name;
    private Integer age;

    public static void main(String[] args) {
        //开起chain=true后可以使用链式的set
        User user=new User().setAge(27).setName("kevin");//返回对象
        System.out.println(user);
    }

}

@Builder

https://blog.csdn.net/qq_34819372/article/details/123569833
https://www.cnblogs.com/zhaojinhui/p/16719983.html

@Configuration

1、Configuration + Bean

/**
 * 3、proxyBeanMethods:代理bean的方法
 *      Full(proxyBeanMethods = true)(保证每个@Bean方法被调用多少次返回的组件都是单实例的)(默认)
 *      Lite(proxyBeanMethods = false)(每个@Bean方法被调用多少次返回的组件都是新创建的)
**/
@Configuration          //告诉SpringBoot这是一个配置类 == 配置文件 // 配置类本身也是组件
public class MyConfig {
    //使用@Bean标注在方法上,给容器添加组件,默认是单例实例。以方法名作为组件的id。返回类型就是组件类型。
    //返回的值就是组件在容器中的实例 
    @Bean("tom") // 给组件起名字
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        return zhangsan;
    }
}

Full模式与Lite模式
lite:轻模式,每次调用配置类方法都会返回新对象,@Configuration(proxyBeanMethods = false)
full:重模式,每次调用配置类方法返回对象都相同,@Configuration(proxyBeanMethods = true),默认是full模式,每次返回对象前都要先判断容器中是否已存在对象
proxyBeanMethods:代理bean的方法
配置类组件之间无依赖关系,用Lite模式加速容器启动过程,减少判断
配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式

测试注册组件

@SpringBootApplication
public class MainApplication {
	public static void main(String[] args) {
		//1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
          //2、查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
        //3、从容器中获取组件
        Pet tom01 = run.getBean("tom", Pet.class);
        //4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892   配置类本身也是组件
        MyConfig bean = run.getBean(MyConfig.class);
        // 获取实例
        User user = bean.user01();
	}
}

@ConfigurationProperties

绑定.properties配置文件

mycar.a="s"
mycar.b="c"

把.properties配置文件中的值赋给容器中的组件,在组件类上添加@ConfigurationProperties(prefix = “mycar”) + @Component。
prefix = “mycar” 是.properties配置文件中 某个变量的前缀
另一种写法:@EnableConfigurationProperties(car.class) + @ConfigurationProperties(prefix = “mycar”)
@EnableConfigurationProperties: 开启某个类的属性配置功能
示例

@Service
@ConfigurationProperties(prefix = "images")
public class ImageListService {
    private List<ImageDto> list;

    public List<ImageDto> getList(){
        return list;
    }

    public void setList(List<ImageDto> list){
        this.list = list;
    }
}

@Data
public class ImageDto implements Serializable {
    private String type;
    private String desc;
    private String masterImage;
    private String slaveImage;
}

images:
  list:
    - type: HTTP
      desc: 通用镜像
      master-image: acr..com/gsx/master-my-jmeter:1.0.12
      slave-image: acr..com/gsx/slave-my-jmeter:1.0.11
    - type: WEBSOCKET
      desc: 支持websocket协议压测
      master-image: acr..com/gsx/my-jmeter-master-websocket:1.0.2
      slave-image: acr..com/gsx/my-jmeter-slave-websocket:1.0.2

另一种方法

@EnableConfigurationProperties(Car.class)
public class MyConfig {
...
}

@ConfigurationProperties(prefix = "mycar")
public class Car {
...
}

@Component

@Component 代表组件 @Controller 代表控制器 @Service 代表业务逻辑组件 @Repository 代表数据库层组件
@Bean、@Component、@Controller、@Service、@Repository,它们是Spring的基本标签,在Spring Boot中并未改变它们原来的功能。

@ComponentScan

@ComponentScan 自动将扫描路径下边带有@Controller,@Service,@Repository,@Component注解的类加入spring容器,通过includeFilters加入扫描路径下没有以上注解的类加入spring容器,通过excludeFilters过滤出不用加入spring容器的类,自定义增加了@Component注解的注解方式

@Import

@Import({User.class, DBHelper.class})
给容器中自动添加两个类型的组件、默认组件的名字是全类名

@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
}

@ImportResource

原生配置文件引入
@ImportResource({ “classpath:ctx-casclient.xml” }) 导入spring配置文件

======================beans.xml=========================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="haha" class="com.atguigu.boot.bean.User">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="18"></property>
    </bean>

    <bean id="hehe" class="com.atguigu.boot.bean.Pet">
        <property name="name" value="tomcat"></property>
    </bean>
</beans>
@ImportResource("classpath:beans.xml")
public class MyConfig {}

======================测试=================
        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println("haha:"+haha);//true
        System.out.println("hehe:"+hehe);//true

@Conditional

条件装配:满足Conditional指定的条件,则进行组件注入
@Conditional 的子注解如下:
在这里插入图片描述

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(name = "tom")//没有tom名字的Bean时,MyConfig类的Bean才能生效。
public class MyConfig {

    @Bean
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }

    @Bean("tom22")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}

public static void main(String[] args) {
    //1、返回我们IOC容器
    ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

    //2、查看容器里面的组件
    String[] names = run.getBeanDefinitionNames();
    for (String name : names) {
        System.out.println(name);
    }

    boolean tom = run.containsBean("tom");
    System.out.println("容器中Tom组件:"+tom);//false

    boolean user01 = run.containsBean("user01");
    System.out.println("容器中user01组件:"+user01);//true

    boolean tom22 = run.containsBean("tom22");
    System.out.println("容器中tom22组件:"+tom22);//true

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值