[求解答]写SpringEL和资源调用时,出现文件无法发现的错误

谙忆
Java领域优质创作者
博客专家认证
2016-11-14 12:50:11

首先说下错误:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [cn.hncu.p2_2_2SpringEL.ElConfig]; nested exception is java.io.FileNotFoundException: class path resource [cn/hncu/p2_2_2SpringEL/test.properties] cannot be opened because it does not exist
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [cn.hncu.p2_2_2SpringEL.ElConfig]; nested exception is java.io.FileNotFoundException: class path resource [cn/hncu/p2_2_2SpringEL/test.properties] cannot be opened because it does not exist
...
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at cn.hncu.p2_2_2SpringEL.Main.main(Main.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.io.FileNotFoundException: class path resource [cn/hncu/p2_2_2SpringEL/test.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)


也就是文件没有发现,所有代码在下面,为什么会这样,求大神解答!

首先,Maven的pox.xml的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.wisely</groupId>
<artifactId>highlight_spring4</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>

<!-- spring aop 支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<!-- aspectj支持 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>

<!--简化文件操作-commons-io-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>



文件位置:


代码:
text.propertise:
project.name=SpringEL
project.author=chenhaoxiang


test.txt:
chenhaoxiang


DemoService.java
package cn.hncu.p2_2_2SpringEL;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
* Created with IntelliJ IDEA.
* User: 陈浩翔.
* Date: 2016/11/13.
* Time: 下午 9:06.
* Explain:被注入的Bean
*/
@Service
public class DemoService {
@Value("DemoService类的属性")//注入字符串
private String another;
public String getAnother() {
return another;
}
public void setAnother(String another) {
this.another = another;
}
}


ElConfig.java
package cn.hncu.p2_2_2SpringEL;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

import java.io.IOException;

/**
* Created with IntelliJ IDEA.
* User: 陈浩翔.
* Date: 2016/11/13.
* Time: 下午 9:11.
* Explain:配置类
*/
@Configuration
@ComponentScan("cn.hncu.p2_2_2SpringEL")
@PropertySource("classpath:cn/hncu/p2_2_2SpringEL/test.properties")
public class ElConfig {

@Value("I LOVE YOU!")//注入字符串
private String normal;

@Value("#{systemProperties['os.name']}")//获取操作系统名
private String osName;

@Value("#{ T(java.lang.Math).random() * 100.0 }")//注入表达式结果
private double randomNumber;

@Value("#{demoService.another}")//注入其他Bean的属性
private String fromAnother;

@Value("${project.name}")//注入配置文件
private String projectName;

@Value("classpath:cn/hncu/p2_2_2SpringEL/test.txt")
private Resource testFile;//注意这个Resource是:org.springframework.core.io.Resource;

@Autowired //注入配置文件
private Environment environment;

@Value("http://www.chaojijuhui.com")//注入网址资源
private Resource testUrl;

@Bean //注入配置文件
public static PropertySourcesPlaceholderConfigurer propertyConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}

public void outputResource(){
try {
System.out.println(normal);
System.out.println(osName);
System.out.println(randomNumber);
System.out.println(fromAnother);
System.out.println(projectName);
System.out.println(IOUtils.toString(testFile.getInputStream()));
System.out.println(environment);
System.out.println(IOUtils.toString(testUrl.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}

}


Main.java
package cn.hncu.p2_2_2SpringEL;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* Created with IntelliJ IDEA.
* User: 陈浩翔.
* Date: 2016/11/13.
* Time: 下午 11:44.
* Explain:运行类
*/
public class Main {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
ElConfig resourceService = context.getBean(ElConfig.class);
resourceService.outputResource();
context.close();
}

}
...全文
368 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
谙忆 2016-11-14
  • 打赏
  • 举报
回复
谢谢,问题解决了,确实是你说的那样,但是我没有找到IDEA的设置,在pom文件里面加了如下代码:
<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
家里敷泥呀 2016-11-14
  • 打赏
  • 举报
回复
要看的不是源码,是编译后的程序。
通过资源管理器,去target目录里面找找,大概没有这个文件。
有个配置项,默认只把.class的文件放到编译目录中,改一下这个配置就行了。

Eclipse是在build path里配置

你的是idea?自己摸索一下吧。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧