首先说下错误:
警告: 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();
}
}