在classpath中读取文件 [英] read file in classpath

查看:134
本文介绍了在classpath中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要做的,我想知道是否有任何Spring类将帮助实施。我不需要使用spring来解决这个问题,我只是用其他方法来实现它。



在我的DAO层中,我想将我的sql文件aka每个文件1个sql。我想读取和缓存sql语句,甚至可能作为一个spring bean单例。但在我最初的挣扎中,我只是在类路径中加载一个SQL文件有问题...



春天有什么帮助吗?我已经通过文档,但没有什么是跳出来对我。



这是我以后的样子..但我不能让它识别该文件或可能的类路径...不确定是否需要在applicationContext中定义的东西?



以下是一些似乎不工作的尝试.. 。

  reader = new BufferedReader(new InputStreamReader(new ClassPathResource(com.company .app.dao.sql.SqlQueryFile.sql)。getInputStream())

reader = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(com.company.app.dao.sql.SqlQueryFile。 ));

有什么想法?

解决方案

在您的应用程序中,尝试让Spring注入它,假设您使用Spring作为依赖注入框架。类,做这样的事情:

  public void setSqlResource(R esource sqlResource){
this.sqlResource = sqlResource;





$ b然后在您的应用程序上下文文件中,在bean定义中,只需设置一个property:

 < bean id =someBeanclass =...> 
< property name =sqlResourcevalue =classpath:com / somecompany / sql / sql.txt/>
< / bean>

Spring应该足够聪明,可以从类路径中加载文件并将其作为bean一个资源。



您也可以查看 PropertyPlaceholderConfigurer ,并将所有的SQL存储在属性文件中,并在需要的地方分别注入。有很多选项。


Here is what I want to do and I am wondering if there is any Spring classes that will help with implementing. I don't have to use spring for this particular problem, I'm just implementing it with everything else.

In my DAO layer I want to externalize my sql files aka 1 sql per file. I want to read and cache the sql statement even maybe as a spring bean singleton. But in my initial struggles, I am having a problem just loading a sql file in the classpath...

Is there anything in spring to help with that? I've been through the documentation but nothing is jumping out at me.

Here is kind of what I'm after.. but I can't get it to recognize the file or maybe the classpath... not real sure does something need to be defined in applicationContext?

Here are a couple of attempts that do not seem to work... both spring'ish and just java'ish.

reader = new BufferedReader(new InputStreamReader(new ClassPathResource("com.company.app.dao.sql.SqlQueryFile.sql").getInputStream())

reader = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream("com.company.app.dao.sql.SqlQueryFile.sql")));

Any thoughts?

解决方案

Try getting Spring to inject it, assuming you're using Spring as a dependency-injection framework.

In your class, do something like this:

public void setSqlResource(Resource sqlResource) {
    this.sqlResource = sqlResource;
}

And then in your application context file, in the bean definition, just set a property:

<bean id="someBean" class="...">
    <property name="sqlResource" value="classpath:com/somecompany/sql/sql.txt" />
</bean>

And Spring should be clever enough to load up the file from the classpath and give it to your bean as a resource.

You could also look into PropertyPlaceholderConfigurer, and store all your SQL in property files and just inject each one separately where needed. There are lots of options.

这篇关于在classpath中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆