手动演示SpringBoot自动装配

我这里用一个小汽车类,通过导入自己模拟的包,来实现小汽车的对象实例化
一、war包工程结构(首先说明,我这是一个空的Maven项目开始的)
在这里插入图片描述
二、需要的xml配置

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.1.5.RELEASE</version>
      </dependency>

三、小汽车类

package cn.lyj.service;

public class CarService {
    private String color; //小汽车颜色
    private String carname; //小汽车牌子名字

    public CarService(String color, String carname){
        this.color=color;
        this.carname=carname;
    }

    public String myCar(){
        return "我的小汽车是"+this.color+"色的,它好像叫"+carname;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }
}

四、对应的给小汽车赋值的配置类

package cn.lyj.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="car") //这里为了被引用时有对应的口令赋值
public class CarProperties {
    private String color;
    private String carname;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }
}

五、实现小汽车配置类

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@ConditionalOnClass(CarService.class)  //导入CarService,这个注解是为了实现创建单例的CarService
@EnableConfigurationProperties(CarProperties.class) //导入CarProperties配置类,该类是CarService的替身,帮助CarService在引用配置中拿值
public class CarAutoConfiguration {

    @Resource
    private CarProperties carProperties; //注入Car属性配置类

    @Bean
    @ConditionalOnMissingBean(CarService.class)
    @ConditionalOnProperty(prefix="car",value="enabled",havingValue = "true") //设置“暗号”,等下引用赋值要用
    public CarService carService(){
        CarService carService = new CarService(carProperties.getColor(),carProperties.getCarname());
        return carService;
    }

}

六、别忘了在resources文件下创建META-INF(spring.factories)

org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
  cn.lyj.test.CarAutoConfiguration

七、打包
在这里插入图片描述

接下来就是新建个项目来引用了

一、向pom.xml自己写的依赖

       <dependency>
            <groupId>cn.lyj</groupId>   <!--包路径-->
            <artifactId>demo_fistconfig</artifactId> <!--小车类所在的工程名-->
            <version>1.0-SNAPSHOT</version>  <!--版本号-->
        </dependency>

二、给小汽车赋值
在这里插入图片描述
三、测试(自己写个测试类)

@SpringBootTest
class DemoConfigApplicationTests {

    @Autowired
    CarService carService;

    @Test
    void contextLoads() {
        System.out.println(carService.myCar());
    }

}

测试结果
在这里插入图片描述
总结:不懂SpringBoot的自动装配原理就自己写一个吧!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值