目录
一、@PropertySource
二、@ImportResource
①SpringConfig (Spring框架全注解)
②@ImportResource注解实现
三、@Bean
四、多配置文件
多Profile文件的使用
文件命名约定:
激活Profile:
YAML文件支持多文档块:
五、配置加载位置优先级
对于SpringBoot项目来说,有3种常用来绑定指定配置文件的注解:@PropertySource,@ImportResource和@Bean。
想看默认配置绑定看这篇:SpringBoot 第一课(Ⅱ)配置文件注入-CSDN博客文章浏览阅读500次,点赞7次,收藏14次。需要为每个属性单独指定,适用于简单的配置或单个属性的注入。属性上,Spring Boot 会处理这种命名风格的差异。使用@ConfigurationProperties注入。),而松散绑定支持这三种名字互通,就比如配置文件里的。允许一次性注入整个对象,适用于复杂的配置结构。Java中常见命名规则有驼峰命名法(实现上面操作后,就可以通过访问“使用注解时,在处理复杂情况时,
https://blog.csdn.net/m0_74977981/article/details/146282864?spm=1001.2014.3001.5501
一、@PropertySource
配置文件
在里面编写:
student1.name=admin
student1.age=20
student1.id=1
Java实体类
package com.qcby.entity1;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@PropertySource(value="classpath:student1.properties")
@ConfigurationProperties(prefix="student1")
public class Student1 {private String name;private int age;private int id;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "Student1{" +"name='" + name + '\'' +", age=" + age +", id=" + id +'}';}
}
现在我们需要测试这个类:
在test路径下测试:
注意这里SpringBoot项目在创建时会自动生成这么一个test类(如下):
为了方便查看,我们再在它旁边创建一个StudentTest类:
package com.qcby.sbdemotest01;import com.qcby.entity1.Student1;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;/*** 从Spring Boot 2.2版本开始,@SpringBootTest 已经包含了 SpringRunner 的功能,* 因此在大多数情况下,工程师不再需要显式地使用 @RunWith(SpringRunner.class)。*/
//@RunWith(SpringRunner.class)
//我用的是2.7.0,所以不用显式这一行【我在这里显式加上它的话,日志会报错警告(虽然能用吧)】(但是要知道有这么一个注释)
@SpringBootTest
public class StudentTest {@AutowiredStudent1 student1;@Testvoid contextLoads() {System.out.println(student1);}}
得到结果:
注意: 在Spring中,
classpath
是一个特殊的路径前缀,在SpringBoot项目中,默认代表的根目录一般是src/main/resources。
①规范情况
即假设一个项目的目录结构是:
src/ ├── main/ │ ├── java/ │ │ └── com/example/Student1.java │ └── resources/ │ ├── student1.properties │ └── beans.xml
那么会在实体类中这么书写:
@PropertySource("classpath:config.properties")
②嵌套子目录
src/ ├── main/ │ ├── java/ │ │ └── com/example/Student1.java │ └── resources/ │ └── config/ │ ├── student1.properties │ └── beans.xml
此时在实体类中定义时,就要加上目录路径前缀:
@PropertySource("classpath:config/config.properties")
不只是@PropertySource,其他用到
classpath
的地方同样遵循这个准则。
二、@ImportResource
在讲@ImportResource这里时,结合Spring框架全注解实现配置一起。
详文参照:Spring(二)---基于注解的方式实现Bean管理和注入属性_有没有注解可以实现bean动态注入,配置项打开注入,关闭则剔除-CSDN博客文章浏览阅读488次,点赞9次,收藏6次。①:注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值...)②:使用注解,注解作用在类上面,方法上面,属性上边③:使用注解的目的:简化XML配置。_有没有注解可以实现bean动态注入,配置项打开注入,关闭则剔除
https://blog.csdn.net/m0_74977981/article/details/144626708?spm=1001.2014.3001.5501
①SpringConfig
(Spring框架全注解)
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;@Configuration
@ComponentScan(value = "com.qcby")
public class SpringConfig {
}
-
@Configuration
注解:标识这是一个配置类,Spring容器会扫描这个类中定义的Bean。 -
@ComponentScan
注解:指定Spring容器扫描的包。在这个例子中,Spring容器会扫描com.qcby
包及其子包中的所有Spring组件(如@Component
,@Service
,@Repository
等),并将它们注册为Spring容器中的Bean。
这种方式是基于Java的配置,它允许项目通过Java代码来定义Spring容器的配置(并且是范围性的)。
②@ImportResource注解实现
配置类
@Configuration
@ImportResource("classpath:applicationContext.xml")
public class AppConfig {// 配置类中的其他配置
}
resources路径下的:applicationContext.xml
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- <context:component-scan base-package="com.qcby" /> --><!-- 扫描可以选择范围扫描或是逐一注入 --><!-- 逐一声明Bean --><bean id="myBean" class="com.qcby.MyBean" />
</beans>
@Configuration
注解:同样标识这是一个配置类。-
@ImportResource
注解:用于导入一个XML配置文件。在这个例子中,它导入了类路径下的applicationContext.xml
文件。这种方式允许你将配置信息放在XML文件中,而不是Java代码中。
三、@Bean
在Spring Boot中,推荐使用全注解的方式来配置和添加组件到Spring容器中。这种方式不仅代码更简洁,而且易于维护和理解。下面我将详细解释如何使用 @Configuration
和 @Bean
注解来实现这一过程。
接口
package com.qcby.service;public interface HelloService {void sayHello();
}
Java实现类
package com.qcby.service;import org.springframework.stereotype.Service;@Service
public class SimpleHelloService implements HelloService {@Overridepublic void sayHello() {System.out.println("Hello, Spring Boot!");}
}
在这个例子中,SimpleHelloService
类被标记为 @Service
,这表示它是一个服务层组件,Spring会自动将其注册为一个Bean。
MyAppConfig类:
我们创建一个配置类 MyAppConfig
,并在其中使用 @Bean
注解来声明一个Bean。
package com.qcby.service;import com.qcby.service.HelloService;
import com.qcby.service.SimpleHelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;@Configuration
public class MyAppConfig {@Bean@Primary // 指定为首选beanpublic HelloService helloService02() {System.out.println("配置类@Bean给容器中添加组件了...");return new SimpleHelloService();}
}
在这个配置类中,我们使用 @Bean
注解声明了一个 HelloService
类型的Bean。这意味着Spring容器将创建一个 SimpleHelloService
实例,并将其注册为 HelloService
类型的Bean。
创建Test类确认是否调用正确:
package com.qcby.sbdemotest01;import com.qcby.service.HelloService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class HelloServiceTest {@Autowiredprivate HelloService helloService;@Testvoid contextLoads() {helloService.sayHello();}}
得到结果:
四、多配置文件
Spring Boot 支持多环境配置,允许为不同的环境(如开发、测试、生产等)创建不同的配置文件。这是通过使用不同的配置文件来实现的,这些文件可以是 .properties
格式或 .yml。
多Profile文件的使用
文件命名约定:
对于 .properties
文件,可以使用 application-{profile}.properties
来命名不同的配置文件。例如:
-
application-dev.properties
:开发环境的配置文件。 -
application-test.properties
:测试环境的配置文件。 -
application-prod.properties
:生产环境的配置文件。
默认情况下,如果没有指定环境,Spring Boot 会使用 application.properties
文件。
激活Profile:
可以通过多种方式激活特定的Profile:
-
在启动应用时,通过命令行参数指定:
java -jar your-application.jar --spring.profiles.active=dev 【终端】
-
在
application.properties
或application.yml
文件中指定:spring.profiles.active=dev【配置文件中】
-
在环境变量中设置:
export SPRING_PROFILES_ACTIVE=dev
(Linux/Mac)或set SPRING_PROFILES_ACTIVE=dev
(Windows)【操作系统级别】
YAML文件支持多文档块:
YAML文件支持多文档块方式,这意味着可以在一个YAML文件中定义多个配置块,每个块对应一个Profile。例如:
spring:profiles: devapplication:name: MyApplication---
spring:profiles: testapplication:name: MyTestApplication---
spring:profiles: prodapplication:name: MyProductionApplication
在这个例子中,我们定义了三个配置块,每个块对应一个不同的Profile(开发、测试、生产)。Spring Boot会根据激活的Profile加载相应的配置块。
五、配置加载位置优先级
Spring Boot在启动时会按照一定的顺序扫描并加载配置文件,这些配置文件的位置和优先级如下:
-
file:./config/
:项目根目录下的config
文件夹中。 -
file:./
:项目根目录中。 -
classpath:/config/
:类路径下的config
文件夹中。 -
classpath:/
:类路径根目录中。
Spring Boot会从这四个位置加载application.properties
或application.yml
文件作为默认配置文件,优先级由高到低,高优先级的配置会覆盖低优先级的配置。这意味着如果同一配置项在多个文件中出现,那么优先级高的文件中的配置将被使用。
/myproject
/src
/main
/java
/com
/yourcompany
/YourApplication.java
/resources
application.properties
/config
application.properties
application.properties
即: