SpringBoot-03自动装配原理2(注解)

1、组件添加

1.1 @Configuration

/**proxyBeanMethods特性
 * 1、配置类本身也是一个组件
 * 2、组件注册默认是单例的
 * 3、proxyBeanMethods:代理bean的方法,为true时每次都是从容器中取,保持单实例,每次拿到的对象是相等的
 *    Full:proxyBeanMethods = true
 *    Lits:proxyBeanMethods = false
 */
 //告诉这是一个配置类
 @Configuration(proxyBeanMethods = true)
 

最佳实战

  • 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
  • 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式

主启动类测试

package com.springboot;

import com.springboot.Entity.ConfigUser;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

//@SpringBootConfiguration
//@EnableTransactionManagement
//@ComponentScan("com.springboot")
//以上3注解等效于 @SpringBootApplication
@SpringBootApplication(scanBasePackages = "com.springboot")
@MapperScan("com.springboot.Mapper")
public class SpringbootApplication {

    public static void main(String[] args) {
        //SpringApplication.run(SpringbootApplication.class, args);
        //获取TOC容器
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        String[] names = context.getBeanDefinitionNames();
        for (String name:names) {
            System.out.println(name);
        }
        ConfigUser configUser1 =context.getBean("pige",ConfigUser.class);
        System.out.println("组件为:"+configUser1);
        ConfigUser configUser2 =context.getBean("pige",ConfigUser.class);
        System.out.println(configUser1 == configUser2);  
    }
    
}

1. 2 原始实现

  • @Bean、@Component、@Controller、@Service、@Repository

1.3 @ComponentScan、@Import

  • @ComponentScan:扫描
  • @Import:在配置类中导入组件,可以为新注册,也可以为依赖中已有的
@Import({User.class, Depart.class})//导入组件,默认组件名称为全类名

1.4 @Conditional

条件装配:满足Conditional指定的条件,则进行组件注入

例如:
如果存在为pige的组件则注册组件user01,否则不注册

    @Bean  //注册装配组件
    @ConditionalOnBean(name = "pige")//选择性注册,当容器中有组件pige时,该组件才生效,也可以在类上
    public ConfigUser user01() {
        return new ConfigUser("yan", 18);
    }

    @Bean(name = "pige")//自定义
    public ConfigPet pet(){
        return  new ConfigPet("pige");
    }
2 、原生配置文件引入
@ImportResource("classpath:beans.xml")//引入原生的配置文件
3、配置绑定

将properties配置文件装配到JavaBean中

3.1 @Component + @ConfigurationProperties

在实体类中添加注解(@Component + @ConfigurationProperties)引入配置,在配置文件中添加配置

  • ConfigStudent.java
package com.springboot.Entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
/**
 * 配置绑定
 * 只有注册到容器中的组件,才会拥有SpringBoot提供的功能
 */
@Component
@ConfigurationProperties(prefix = "student")

public class ConfigStudent {
    private Long id;
    private String name;
    private Integer age;
}
  • application.yml
#对象赋值
student:
  id: 1
  name: pige
  age: 17
  ###############布尔型
  #  happy: true
  ###############日期
  #  birth: 1998/09/09
  ###############map
  #  maps: {k1: v1,k2: v2}
  ###############list
#  lists:
#    - play
#    - music
#    - girl
  • 测试
package com.springboot.Controller;

import com.springboot.Entity.ConfigStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/configstudent")
public class ConfigStudentController {
    @Autowired
    ConfigStudent configStudent;

    @RequestMapping("/student")
    public ConfigStudent configStudent(){
        return configStudent;
    }
}

浏览器访问:http://localhost:8003/configstudent/student
显 示 结 果:{"id":1,"name":"pige","age":17}

3.2 @EnableConfigurationProperties + @ConfigurationProperties

在配置类中开启属性配置(@EnableConfigurationProperties)功能,在实体类中引入配置( @ConfigurationProperties)

  • 配置类
@EnableConfigurationProperties(ConfigStudent.class)
  • 实体类
package com.springboot.Entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
/**
 * 配置绑定
 * 只有注册到容器中的组件,才会拥有SpringBoot提供的功能
 */
//@Component
@ConfigurationProperties(prefix = "student")

public class ConfigStudent {
    private Long id;
    private String name;
    private Integer age;
}

4、总结

配置类:

package com.springboot.Config;

import com.springboot.Entity.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/**
 * 特性
 * 1、配置类本身也是一个组件
 * 2、组件注册默认是单例的
 * 3、proxyBeanMethods:代理bean的方法,为true时每次都是从容器中取,保持单实例,每次拿到的对象是相等的
 * Full:proxyBeanMethods = true
 * Lits:proxyBeanMethods = false
 */

/**
 * 1-添加配置组件
 */
@Configuration(proxyBeanMethods = true) //告诉这是一个配置类
@Import({User.class, Depart.class})//导入组件,默认组件名称为全类名
/**
 * 2、引入原生bean.xml组件
 */
@ImportResource("classpath:beans.xml")//引入原生的配置文件
/**
 *3、配置绑定
 */
@EnableConfigurationProperties(ConfigStudent.class)
public class MyConfig {

    @Bean  //注册装配组件
    @ConditionalOnBean(name = "pige")//选择性注册,当容器中有组件pige时,该组件才生效,也可以在类上
    public ConfigUser user01() {
        return new ConfigUser("yan", 18);
    }

    @Bean(name ="pige") //自定义
    public ConfigPet pet() {
        return new ConfigPet("pige");
    }
}

实体类:

package com.springboot.Entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ConfigUser {

    private String name;
    private Integer age;

}

package com.springboot.Entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ConfigPet {
    private String petName;
}


package com.springboot.Entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
/**
 * 配置绑定
 * 只有注册到容器中的组件,才会拥有SpringBoot提供的功能
 */
//@Component
@ConfigurationProperties(prefix = "student")

public class ConfigStudent {
    private Long id;
    private String name;
    private Integer age;
}

application文件:

server:
  port: 8003

#对象赋值
student:
  id: 1
  name: pige
  age: 17
  ###############布尔型
  #  happy: true
  ###############日期
  #  birth: 1998/09/09
  ###############map
  #  maps: {k1: v1,k2: v2}
  ###############list
#  lists:
#    - play
#    - music
#    - girl
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值