springboot中怎么获取yaml配置为static
时间: 2024-05-09 16:21:03 浏览: 9
在SpringBoot中,要获取yaml配置文件中的static属性,需要通过`@Value`注解和`Environment`接口来实现。具体步骤如下:
1. 在application.yml文件中添加static属性:
```yaml
my:
static: /static
```
2. 在Java类中使用`@Value`注解获取static属性的值:
```java
@Value("${my.static}")
private String staticPath;
```
3. 在需要使用static属性的地方,通过`Environment`接口获取static属性的值:
```java
@Autowired
private Environment environment;
public void someMethod() {
String staticPath = environment.getProperty("my.static");
}
```
注意:在使用`Environment`接口获取属性值时,需要注入`Environment`对象。而在使用`@Value`注解获取属性值时,不需要注入对象,Spring框架会自动为我们注入。
相关问题
静态类中如何读取springboot yaml文件配置信息
静态类中可以通过使用Spring提供的静态方法读取Spring Boot的YAML配置文件信息。具体实现方式如下:
1. 在静态类中引入静态方法:org.springframework.boot.SpringApplication
2. 通过SpringApplication类的静态方法load方法读取指定 YAML 配置文件中的配置属性。具体代码如下:
```
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
public class YamlUtils {
private static PropertySource<?> getPropertySource(String name) throws IOException {
ClassPathResource resource = new ClassPathResource(name);
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
PropertySource<?> propertySource = loader.load(name, resource).get(0);
return propertySource;
}
public static String getString(String name){
try {
return (String) getPropertySource("application.yml").getProperty(name);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static int getInt(String name) {
try {
return Integer.valueOf(getString(name));
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}
```
3. 调用YamlUtils类的方法即可读取配置信息。
注意:在静态类中读取配置信息时,需要使用Spring相关的组件,如SpringApplication等,所以需要引入相关的Spring依赖。
springboot中mybatisplus配置信息
以下是SpringBoot整合MyBatisPlus配置信息的步骤:
1.在pom.xml文件中添加MyBatisPlus和MySQL驱动的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
2.在application.yml文件中配置数据源信息和MyBatisPlus的相关配置:
```yaml
spring:
datasource:
dynamic:
primary: master
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_master?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
slave:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_slave?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
mybatis-plus:
mapper-locations: classpath*:mapper/**/*.xml
type-aliases-package: com.example.demo.entity
```
3.创建数据源配置类DynamicDataSourceConfig,用于动态切换数据源:
```java
@Configuration
public class DynamicDataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.slave")
public DataSource slaveDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceEnum.MASTER.getName(), masterDataSource);
targetDataSources.put(DataSourceEnum.SLAVE.getName(), slaveDataSource);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
}
```
4.创建数据源枚举类DataSourceEnum,用于定义数据源的名称:
```java
public enum DataSourceEnum {
MASTER("master"),
SLAVE("slave");
private String name;
DataSourceEnum(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
```
5.创建动态数据源类DynamicDataSource,用于动态切换数据源:
```java
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
private final Map<Object, Object> targetDataSources;
public DynamicDataSource(DataSource defaultDataSource, Map<Object, Object> targetDataSources) {
super.setDefaultTargetDataSource(defaultDataSource);
this.targetDataSources = targetDataSources;
super.setTargetDataSources(this.targetDataSources);
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey() {
return getDataSource();
}
public static void setDataSource(String dataSource) {
CONTEXT_HOLDER.set(dataSource);
}
public static String getDataSource() {
return CONTEXT_HOLDER.get();
}
public static void clearDataSource() {
CONTEXT_HOLDER.remove();
}
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
}
}
```
6.创建数据源切换的AOP类DataSourceAspect,用于在方法执行前切换数据源:
```java
@Aspect
@Component
public class DataSourceAspect {
@Pointcut("@annotation(com.example.demo.annotation.DataSource)")
public void dataSourcePointCut() {
}
@Before("dataSourcePointCut()")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
DataSource dataSource = signature.getMethod().getAnnotation(DataSource.class);
if (dataSource == null) {
DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
} else {
DynamicDataSource.setDataSource(dataSource.value().getName());
}
}
@After("dataSourcePointCut()")
public void after(JoinPoint joinPoint) {
DynamicDataSource.clearDataSource();
}
}
```
7.在Mapper接口中使用@Mapper注解,并在Mapper.xml文件中使用MyBatisPlus提供的标签进行SQL语句的编写。