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

SpringBoot学习笔记(三)

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

SpringBoot学习笔记(三)

注解:

@Configuration----1.定义配置类,告诉Boot这是一个配置类
                  2.有boolean proxyBeanMethods() default true;-------代理bean的方法
   * Full(proxyBeanMethods=true):有依赖关系,方法会调用得到之前单实例组件,用full模式
   * lite(proxyBeanMethods=false):配置类组件没有依赖关系的话用这个可以加速进程 
   * 组件依赖
@Bean ----给容器中添加组件,以方法名作为组件的id,返回类型就是组件类型,注意这里默认是单实例
 注意: 配置类本身也是组件
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods=true)//告诉Boot这是一个配置类
public class MyConfig {
    @Bean
    public User user01(){
        User zhangsan=new User("张三",18);
        zhangsan.setPet(tomcatPet());//这里是user组件依赖了pet组件
          return zhangsan;
    }
    @Bean("tom")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}

@import-----导入组件:放在配置类上

@Conditional-----导入组件:放在配置类上(ConditionalOnBean,ConditionalOnMissingBean)

@importResource----导入配置资源(例如导入XML文件,放在配置类上方指定资源路径@importResource("classpath:beans.xml")

@ConfigurationProperties----1.@Component+@ConfigurationProperties(prefix="")

                                             2.@Component+配置类上@EnableConfigurationProperties(.....class)

package com.cxy.boot.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//类里面的属性和配置文件里面前缀为mycar的绑定后,再自动注入
@Component//只有在容器中的组件才有springboot的强大
@ConfigurationProperties(prefix="mycar")
public class Car {
    private String brand;
    private Integer price;
    public String getBrand() {return brand;}
    public void setBrand(String brand) {this.brand = brand; }
    public Integer getPrice() {return price;}
    public void setPrice(Integer price) {this.price = price;}
    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + ''' +
                ", price=" + price +
                '}';}
}
法二

@import({User.class, CharTypes.class})
@Configuration(proxyBeanMethods=true)//告诉Boot这是一个配置类
@EnableConfigurationProperties(Car.class)//开启Car配置绑定功能,并把其自动注册到容器中
public class MyConfig {

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

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

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