自定义test.properties文件
my.name = leif
my.age = 24
方法一
使用@value
注解
package com.fas.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/my")
@PropertySource("classpath:test.properties")
public class MyController {
@Value("${my.name}")
private String name;
@Value("${my.age}")
private int age;
@GetMapping("/test")
@ResponseBody
public String test() {
return name + " - " + age;
}
}
方法二
使用Environment
类
package com.fas.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/my")
@PropertySource("classpath:test.properties")
public class MyController {
@Autowired
private Environment environment;
@GetMapping("/test")
@ResponseBody
public String test() {
return environment.getProperty("my.name") + " - " + environment.getProperty("my.age");
}
}
方法三
使用@ConfigurationProperties
注解
package com.fas.demo.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "my")
@PropertySource("classpath:test.properties")
public class MyPropertiesConfiguration {
private String name;
private int age;
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;
}
}
package com.fas.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fas.demo.configuration.MyPropertiesConfiguration;
@Controller
@RequestMapping("/my")
public class MyController {
@Autowired
private MyPropertiesConfiguration myPropertiesConfiguration;
@GetMapping("/test")
@ResponseBody
public String test() {
return myPropertiesConfiguration.getName() + " - " + myPropertiesConfiguration.getAge();
}
}
方法四
使用注册监听器
package com.fas.demo.configuration;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.core.io.support.PropertiesLoaderUtils;
public class MyPropertiesConfiguration {
public static Map<String, String> propertiesMap = new HashMap<String, String>();
private static void processProperties(Properties properties) {
for (Object key : properties.keySet()) {
String keyString = key.toString();
try {
propertiesMap.put(keyString, new String(properties.getProperty(keyString).getBytes("ISO-8859-1"), "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
public static void loadAllProperties(String propertyFileName) {
try {
Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
processProperties(properties);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperty(String name) {
return propertiesMap.get(name);
}
public static Map<String, String> getAllProperty() {
return propertiesMap;
}
}
package com.fas.demo.listener;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import com.fas.demo.configuration.MyPropertiesConfiguration;
public class MyPropertiesListener implements ApplicationListener<ApplicationEvent> {
private String propertyFileName;
public MyPropertiesListener(String propertyFileName) {
this.propertyFileName = propertyFileName;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
MyPropertiesConfiguration.loadAllProperties(propertyFileName);
}
}
package com.fas.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.fas.demo.listener.MyPropertiesListener;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
springApplication.addListeners(new MyPropertiesListener("test.properties"));
springApplication.run(args);
}
}
package com.fas.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fas.demo.configuration.MyPropertiesConfiguration;
@Controller
@RequestMapping("/my")
public class MyController {
@GetMapping("/test")
@ResponseBody
public String test() {
return MyPropertiesConfiguration.getProperty("my.name") + " - " + MyPropertiesConfiguration.getProperty("my.age");
}
}
网友评论