写一个配置类,加载TestUser,使用自动配置的方式实现只需要加载这个jar自动让配置类生效

一个独立的模块必须由版本号,groupId,artifactId,(除非由父模块支撑)。
TestUser.class

package com.itheima.my;
import lombok.Data;
//写一个配置类,加载TestUser,使用自动配置的方式实现只需要加载这个jar自动让配置类生效
@Data
public class TestUser {
    private String username;
}

TestConfig.class

package com.itheima.my;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
    @Bean
    public TestUser testUser(){
        TestUser testUser = new TestUser();
        testUser.setUsername("张三");
        return testUser;
    }
}

resource目录下的META-INF/spring.factories录入
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.itheima.my.TestConfig

pom.xml
 <groupId>springboot-demo</groupId>
 <version>0.0.1-SNAPSHOT</version>
 <artifactId>my-autoConfiguration</artifactId>
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.6.6</version>
        <scope>compile</scope>
</dependency>

父工程引入依赖
<dependency>
            <groupId>springboot-demo</groupId>
            <version>0.0.1-SNAPSHOT</version>
            <artifactId>my-autoConfiguration</artifactId>
</dependency>
测试
    public static void main(String[] args) {
        ConfigurableApplicationContext run
                = SpringApplication.run(DemoApplication.class, args);
        TestUser testUser = run.getBean(TestUser.class);
        System.out.println(testUser);
引入三种方式:
1. 通过META-INF/spring.factories下设置自动配置文件,
2. 通过@Configuration和在启动类扫描包上添加com.itheima.my,来注入对象
3. 在启动类上加入@Import(TestConfig.class)

TestProperties.class

package com.itheima.my;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "test.my")
public class TestProperties {
    private String username;
}

package com.itheima.my;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(TestProperties.class)
public class TestConfig {

    @Bean
    public TestUser testUser(TestProperties testProperties){
        TestUser testUser = new TestUser();
        testUser.setUsername(testProperties.getUsername());
        return testUser;
    }
}
修改打包方式为 jar  ,才可以加载配置文件
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
导入依赖
 <dependency>
            <groupId>com.example</groupId>
            <version>0.0.1-SNAPSHOT</version>
            <artifactId>myauto</artifactId>
 </dependency>
 ==========================
 项目提示依赖包
  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>2.6.6</version>
        <optional>true</optional>
</dependency>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最好的期待,未来可期

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值