属性文件是使用它们的最佳方式(Properties files best way of using them)

编程入门 行业动态 更新时间:2024-10-28 17:29:04
属性文件是使用它们的最佳方式(Properties files best way of using them)

我正在努力处理财产文件。 在我的应用程序中,我使用两种属性文件:在spring和我的应用程序中直接使用。

在应用程序中我使用方法来获取属性:

private String getHome() { String name = null; try { Properties prop = new Properties(); String propFname = "path.properties"; InputStream is = getClass().getClassLoader().getResourceAsStream(propFname); if (is == null) { throw new FileNotFoundException("path.properties File Not found"); } prop.load(is); name = prop.getProperty("HOME"); } catch (FileNotFoundException e) { log.error(e); System.exit(1); } catch (IOException e) { log.error(e); System.exit(1); } return name; }

然后在我的班上我使用:

private static String home = new MailService().getHome();

但对于JDBC连接等,我使用带有context:property-placeholder的Spring xml文件。

我想让这两种方式只有一种 - 属性文件的位置也不同,从上面我将它们放在/ WEB-INF / classes /中,而来自Spring只在/ WEB-INF /中可能会让人困惑。

关于我的问题:

统一这两种方式是否有任何意义,我的意思是它们都具有它们的意义还是仅仅使用弹簧对我们已经使用它们两种方式更好? 在Spring中,我还发现了两种初始化属性的方法,一种是使用@Value注释,另一种是使用@Autowiring bean。 如果我是正确的,他们都应该做同样的功能,那只是更新更老的风格还是这两者之间有真正的区别? 最后一个问题:我搜索的是保存属性文件的不同方法。 最常见的是/ resources,/ WEB-INF和/ classes / config /。 它也可以随着时间的推移而区分,或者仅仅是程序员所喜欢的? 我没有找到直接答案。

对不起,如果这个问题看起来很虚假,但是从搜索不同的答案我感到困惑的是正确

谢谢。

I am struggling with property files. In my application I am using two ways of property files: in spring and in my app directly.

In app I use a method to get property:

private String getHome() { String name = null; try { Properties prop = new Properties(); String propFname = "path.properties"; InputStream is = getClass().getClassLoader().getResourceAsStream(propFname); if (is == null) { throw new FileNotFoundException("path.properties File Not found"); } prop.load(is); name = prop.getProperty("HOME"); } catch (FileNotFoundException e) { log.error(e); System.exit(1); } catch (IOException e) { log.error(e); System.exit(1); } return name; }

and then in my class I use:

private static String home = new MailService().getHome();

But for JDBC connections etc I use Spring xml file with context:property-placeholder.

I wanted to make these two ways only one - the location of property files is also different, from above I have them located in /WEB-INF/classes/ and from Spring only in /WEB-INF/ which might be confusing.

About my questions:

Does it have any sense to unify these two ways, I mean are they both having their meanings or is it just better to use spring for both of them I am already using it? In Spring I also found two ways of initializing the property, one using @Value annotation and second using @Autowiring bean. If I am correct they should both do the same function, is that just newer and older style or is there real difference between these two? And last question: what I searched was different ways of saving property files to. Most common were /resources, /WEB-INF and /classes/config/. It that also differentiating through time or it is just on the programmer what he prefers? I didn't find direct answer to that.

Sorry if that question seems dummy but from searching different answers I got confused about the proper way.

Thanks.

最满意答案

是的,请统一这一点 - 没有理由编写自己的代码来访问属性文件,因为Spring通过使用@Value或表达式将值直接注入到需要它们的类中来提供此功能,例如${home}在您的应用程序上下文XML中。 使用@Value从属性文件中注入值。 @Autowired用于注入bean而不是默认值 - 它不是环境感知的,你不能将它传递给表达式。 将属性文件放在src/main/resources 。 这是Maven标准 - Maven将它们部署到WEB-INF / classes,因此它们可以在您的类路径中使用,并且可以通过Spring应用程序轻松访问。 Yes, please unify this - there is no reason to write your own code for accessing properties files since Spring provides this functionality either by injecting the values directly into the classes where they are needed with @Value or with an expression, e.g. ${home} in your application-context XML. Use @Value to inject values from property files. @Autowired serves the purpose of injecting bean not default values - it is not environment aware and you can't pass it an expression. Put your property files in src/main/resources. This is Maven standard - Maven will deploy them to WEB-INF/classes and thus they are available on your classpath and easily accessible by your Spring application.

更多推荐

本文发布于:2023-07-28 12:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   方式   文件   files   Properties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!