方式1
配置config
package com.ljzforum.platform.config;
import com.ljzforum.platform.util.CustomizedPropertyConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.util.Properties;
@Configuration
public class YmlConfig {
@Autowired
private Environment env;
@Bean
public CustomizedPropertyConfigurer ymlConfigurer() {
System.out.println(env.getActiveProfiles()[0]);
//1:加载配置文件
Resource app = new ClassPathResource("application.yml");
Resource appEnv = new ClassPathResource("application-"+env.getActiveProfiles()[0]+".yml");
Resource oss = new ClassPathResource("oss.yml");
Resource message = new ClassPathResource("message.yml");
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
// 2:将加载的配置文件交给 YamlPropertiesFactoryBean
yamlPropertiesFactoryBean.setResources(app, oss, message, appEnv);
// 3:将yml转换成 key:val
Properties properties = yamlPropertiesFactoryBean.getObject();
// 4: 将Properties 通过构造方法交给我们写的工具类
CustomizedPropertyConfigurer ymlConfigurer = new CustomizedPropertyConfigurer(properties);
return ymlConfigurer;
}
}
CustomizedPropertyConfigurer
import java.util.Properties;
public class CustomizedPropertyConfigurer {
private static Properties ymlProperties = new Properties();
public CustomizedPropertyConfigurer(Properties properties){
ymlProperties = properties;
}
public static Object getContextProperty(String key) {
return ymlProperties.getProperty(key);
}
public static Object setContextProperty(String key,Object value) {
return ymlProperties.setProperty(key, value.toString());
}
}
//读取方式
String name = (String) CustomizedPropertyConfigurer.getContextProperty("NAME");
方式2
//pom文件配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 支持 @ConfigurationProperties 注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
</dependencies>
<build>
<finalName>java-noob</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
//yml文件配置
service:
applyList: [{"fruit":"apple","interest":"hisrory"},{"fruit":"orange","interest":"music"}]
//配置类
@Component
@ConfigurationProperties(prefix = "service")
public class Service
{
@Getter
@Setter
private JSONObject bannerPicList = new JSONObject(true);
@Override
public String toString()
{
return "Service{" +
"applyList='" + applyList+ '\'' +
'}';
}
}
//单元测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class JavaNoobApplicationTests
{
@Autowired
private Service service;
@Test
public void contextLoads()
{
System.out.println(service);
}
}
//还有要注意的是,从yml文件读取json字符串时,需要在配置类中指定JSONObject类型的,但也可以配置成yml格式的List里面包含map形式的。
出现如下错误 SpringBoot Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration
如异常所译,你需要在注解上加上
@SpringBootTest(classes = Application.class) //Application为本项目的启动类
//或者使用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JPAConfig.class})
//或者
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {JPAConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"myJPAConfig.xml"})
@RunWith(SpringRunner.class)
@ContextConfiguration(value={"myJPAConfig.xml"})