软件框架SpringBoot-实现使用@Autowired@PropertySource(指定自定义文件的位置,开启对应配置)等方法实现将配置文件输出在控制台上

一、前言

1.本文代码实现的功能是配置类,指定自定义配置文件的位置和名称,开启对应配置类的属性注入功能,指定配置文件注入属性的前缀,生成getter和setter,toString方法等;

2.该程序代码是使用idea2021.12版本编写的,若使用其他软件请对照好配置;

3.这个程序具体的内容我忘了,只知道使用@Data@Configuration@Autowired@PropertySource@Test(指定自定义文件的位置,开启对应配置)等方法写的,具体实现的功能就是新建几个.yml和.java文件,实现在.yml文件中配置访问路径(输入信息)通过localhost:8080端口实现在网页上输出;

4.下面写的是这个表需要建的结构和代码段以及运行的结果;

5.这个博文讲的,我已将代码包发布到了我的资源里,有需要的可以直接下载并导入到自己的id里,看看能不能运行;

二、结构

首先需要建包,这里就不多说了,我建的包名为unit2-4;

依次在包中src-main-java-com-example-unit24文件中新建一个软件包,名称为configuration;

其次在configuration软件包中新建MyTestProperties这个Java类文件,这个是实现配置类以及上文提到的有关更能的;

同时还需要在src-main-resources软件包中新建mytest.properties文件,这个是用来配置访问角色信息的,这个后缀一定要对;

原则上新建完文件是有Unit24Application和Unit24ApplicationTests这两个Java类文件的,不需要在新建,只需填写写代码;

以上为本代码的结构,下面为代码的结构,画框的就是需要编写的代码,下面会依次将各代码段粘贴

三、代码段

1.MyTestProperties.java类代码

package com.example.unit24.configuration;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.List;

//配置类
@Configuration
//指定自定义配置文件的位置和名称
@PropertySource("classpath:mytest.properties")
//开启对应配置类的属性注入功能
@EnableConfigurationProperties(MyTestProperties.class)
//指定配置文件注入属性的前缀
@ConfigurationProperties(prefix = "role")
//生成getter和setter,toString方法
@Data
public class MyTestProperties {
      private String name;
      private String description;
      private List<String> permissionids;

}

2.mytest.properties代码

#配置角色信息
#???key=value
role.name = 管理员
role.description = 所有权限
role.permissionids = 20,21,22

3.src——Unit24Application.java类代码

package com.example.unit24;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Unit24Application {

    public static void main(String[] args) {
        SpringApplication.run(Unit24Application.class, args);
    }

}

4.test——Unit24ApplicationTests.java类代码

package com.example.unit24;

import com.example.unit24.configuration.MyTestProperties;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Unit24ApplicationTests {
    @Autowired
    private MyTestProperties myTestProperties;
    @Test
    void contextLoads() {
        System.out.println(myTestProperties);
    }

}

四、运行结果

1.id控制台运行的结果

以上为本程序的相关内容,因为我忘了这个程序的具体使用方法,所以就无法讲解了,有需要或了解的可以看看,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可能出现这种情况的原因有很多,以下是一些可能的解决方案: 1. 确保在配置文件中正确配置了需要自动装配的对象,例如: ``` @Configuration @PropertySource("classpath:application.properties") public class AppConfig { @Autowired private Environment env; @Bean public MyConfig myConfig() { MyConfig config = new MyConfig(); config.setUrl(env.getProperty("my.url")); config.setUsername(env.getProperty("my.username")); config.setPassword(env.getProperty("my.password")); return config; } } ``` 2. 确保配置文件的路径和文件名是正确的,例如: ``` @PropertySource("classpath:application.properties") ``` 这里的classpath表示classpath下的根目录,如果配置文件在子目录下,需要在路径中添加相应的目录。 3. 确保项目的classpath正确配置,例如: 在IDEA中,可以在项目的“Run/Debug Configurations”中查看项目的classpath,确保配置文件在classpath下。 4. 确保需要自动装配的对象类上使用了@Component、@Configuration、@Service等注解,例如: ``` @Component public class MyConfig { private String url; private String username; private String password; // getter and setter methods } ``` 这里使用了@Component注解,表示这是一个Spring组件,可以被自动扫描并装配。 5. 确保使用了正确的@Autowired注解,例如: ``` @Autowired private MyConfig myConfig; ``` 这里使用了@Autowired注解,表示需要自动装配MyConfig对象。 6. 如果使用了多个配置文件,需要确保它们没有冲突,例如: ``` @PropertySources({ @PropertySource("classpath:application.properties"), @PropertySource("classpath:other.properties") }) public class AppConfig { // ... } ``` 这里使用了@PropertySources注解,可以配置多个配置文件。需要确保它们没有相同的属性名,否则会出现冲突。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

姜鸿阳

谢谢您!感谢您的支持!

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

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

打赏作者

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

抵扣说明:

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

余额充值