方式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轉(zhuǎn)換成 key:val
Properties properties = yamlPropertiesFactoryBean.getObject();
// 4: 將Properties 通過構(gòu)造方法交給我們寫的工具類
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形式的。
出現(xiàn)如下錯誤 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"})
image.png