Spring boot 在spring引导中访问属性文件
我正在创建一个简单的spring启动应用程序,试图在其中访问外部Spring boot 在spring引导中访问属性文件,spring-boot,properties-file,Spring Boot,Properties File,我正在创建一个简单的spring启动应用程序,试图在其中访问外部config.properties文件 IndexController.java @Controller public class IndexController { XmlOperation xmlOperation = new XmlOperation(); @RequestMapping("/") public String greeting() { return "greeting
config.properties
文件
IndexController.java
@Controller
public class IndexController {
XmlOperation xmlOperation = new XmlOperation();
@RequestMapping("/")
public String greeting() {
return "greeting";
}
@RequestMapping(params = "btnOpen", method = RequestMethod.POST)
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
try {
InputStream is = file.getInputStream();
model.addAttribute("fileContent", xmlOperation.readXml(is));
} catch (IOException e) {
System.out.println(e.getMessage());
}
return "greeting";
}
}
@PropertySource("classpath:config.properties")
public class XmlOperation {
@Autowired
Environment env;
public String readXml(InputStream is) throws IOException {
System.out.println(env.getProperty("filepath"));
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, StandardCharsets.UTF_8);
String fileContent = writer.toString();
return fileContent;
}
xmlooperation.java
@Controller
public class IndexController {
XmlOperation xmlOperation = new XmlOperation();
@RequestMapping("/")
public String greeting() {
return "greeting";
}
@RequestMapping(params = "btnOpen", method = RequestMethod.POST)
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
try {
InputStream is = file.getInputStream();
model.addAttribute("fileContent", xmlOperation.readXml(is));
} catch (IOException e) {
System.out.println(e.getMessage());
}
return "greeting";
}
}
@PropertySource("classpath:config.properties")
public class XmlOperation {
@Autowired
Environment env;
public String readXml(InputStream is) throws IOException {
System.out.println(env.getProperty("filepath"));
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, StandardCharsets.UTF_8);
String fileContent = writer.toString();
return fileContent;
}
config.properties
文件位于src/main/resources
中。我无法从属性文件中获取值
如果您有任何帮助,我们将不胜感激。
src/main/resources
中的config.properties
文件没有问题,但为什么要初始化:
XmlOperation XmlOperation=新的XmlOperation()代码>
在索引控制器中
?我也不确定xmlooperation
是否是spring组件(在这个问题中,您只有@PropertySource
超过xmlooperation)
基本上,我会将xmlooperation
作为spring@组件
,并使用IoC将该组件注入IndexController
xml操作中的公共字符串readXml(InputStream是)
的行为类似于标准服务,我将创建属性filepath
,并使用@value
注释从配置文件(config.properties
)注入值
完整示例:
@Controller
public class IndexController {
@Autowired
private XmlOperation xmlOperation;
@RequestMapping("/")
public String greeting() {
return "greeting";
}
@RequestMapping(params = "btnOpen", method = RequestMethod.POST)
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
try {
InputStream is = file.getInputStream();
model.addAttribute("fileContent", xmlOperation.readXml(is));
} catch (IOException e) {
System.out.println(e.getMessage());
}
return "greeting";
}
}
我是否可以添加@Service
注释而不是@Component
??如果没有,为什么??我是spring新手,所以理解文档并不难。@newbedeveloper如果您想在XmlOperation中使用业务方法,可以将类注释为服务。组件是一种泛型注释,声明类是在spring容器中管理的。