Skip to content

Commit 0742845

Browse files
committed
【Spring】Spring常用配置-Spring EL和资源调用
1 parent 73b8f13 commit 0742845

21 files changed

Lines changed: 1408 additions & 122 deletions

File tree

springBoot/.idea/libraries/Maven__commons_io_commons_io_2_4.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springBoot/.idea/misc.xml

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springBoot/.idea/workspace.xml

Lines changed: 394 additions & 103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springBoot/pom.xml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
<artifactId>spring-aop</artifactId>
2424
<version>4.2.3.RELEASE</version>
2525
</dependency>
26-
2726
<!-- aspectj支持 -->
28-
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
2927
<dependency>
3028
<groupId>org.aspectj</groupId>
3129
<artifactId>aspectjrt</artifactId>
@@ -37,6 +35,13 @@
3735
<version>1.8.9</version>
3836
</dependency>
3937

38+
<!--简化文件操作-commons-io-->
39+
<dependency>
40+
<groupId>commons-io</groupId>
41+
<artifactId>commons-io</artifactId>
42+
<version>2.4</version>
43+
</dependency>
44+
4045
</dependencies>
4146
<build>
4247
<plugins>
@@ -50,5 +55,18 @@
5055
</configuration>
5156
</plugin>
5257
</plugins>
58+
59+
<resources>
60+
<resource>
61+
<directory>src/main/java</directory>
62+
<includes>
63+
<include>**/*.*</include>
64+
</includes>
65+
<excludes>
66+
<exclude>**/*.java</exclude>
67+
</excludes>
68+
</resource>
69+
</resources>
70+
5371
</build>
5472
</project>

springBoot/springBoot.iml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
1010
<excludeFolder url="file://$MODULE_DIR$/target" />
1111
</content>
12-
<orderEntry type="inheritedJdk" />
12+
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" />
1313
<orderEntry type="sourceFolder" forTests="false" />
1414
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.2.3.RELEASE" level="project" />
1515
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.2.3.RELEASE" level="project" />
@@ -20,5 +20,6 @@
2020
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
2121
<orderEntry type="library" name="Maven: org.aspectj:aspectjrt:1.8.9" level="project" />
2222
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.8.9" level="project" />
23+
<orderEntry type="library" name="Maven: commons-io:commons-io:2.4" level="project" />
2324
</component>
2425
</module>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.hncu.p2_2_2SpringEL;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Service;
5+
6+
/**
7+
* Created with IntelliJ IDEA.
8+
* User: 陈浩翔.
9+
* Date: 2016/11/13.
10+
* Time: 下午 9:06.
11+
* Explain:被注入的Bean
12+
*/
13+
@Service
14+
public class DemoService {
15+
@Value("DemoService类的属性")//注入字符串
16+
private String another;
17+
public String getAnother() {
18+
return another;
19+
}
20+
public void setAnother(String another) {
21+
this.another = another;
22+
}
23+
24+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cn.hncu.p2_2_2SpringEL;
2+
3+
import org.apache.commons.io.IOUtils;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.context.annotation.PropertySource;
10+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
11+
import org.springframework.core.env.Environment;
12+
import org.springframework.core.io.Resource;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* Created with IntelliJ IDEA.
18+
* User: 陈浩翔.
19+
* Date: 2016/11/13.
20+
* Time: 下午 9:11.
21+
* Explain:配置类
22+
*/
23+
@Configuration
24+
@ComponentScan("cn.hncu.p2_2_2SpringEL")
25+
@PropertySource("classpath:cn/hncu/p2_2_2SpringEL/test.properties")
26+
public class ElConfig {
27+
28+
@Value("I LOVE YOU!")//注入字符串
29+
private String normal;
30+
31+
@Value("#{systemProperties['os.name']}")//获取操作系统名
32+
private String osName;
33+
34+
@Value("#{ T(java.lang.Math).random() * 100.0 }")//注入表达式结果
35+
private double randomNumber;
36+
37+
@Value("#{demoService.another}")//注入其他Bean的属性
38+
private String fromAnother;
39+
40+
@Value("${project.name}")//注入配置文件
41+
private String projectName;
42+
43+
@Value("classpath:cn/hncu/p2_2_2SpringEL/test.txt")
44+
private Resource testFile;//注意这个Resource是:org.springframework.core.io.Resource;
45+
46+
@Autowired //注入配置文件
47+
private Environment environment;
48+
49+
@Value("http://www.chaojijuhui.com")//注入网址资源
50+
private Resource testUrl;
51+
52+
@Bean //注入配置文件
53+
public static PropertySourcesPlaceholderConfigurer propertyConfigurer(){
54+
return new PropertySourcesPlaceholderConfigurer();
55+
}
56+
57+
public void outputResource(){
58+
try {
59+
System.out.println("normal:"+normal);
60+
System.out.println("osName:"+osName);
61+
System.out.println("randomNumber:"+randomNumber);
62+
System.out.println("fromAnother:"+fromAnother);
63+
System.out.println("projectName:"+projectName);
64+
System.out.println("测试文件:"+IOUtils.toString(testFile.getInputStream()));
65+
System.out.println("配置文件project.author:"+environment.getProperty("project.author"));
66+
System.out.println("网址资源:"+IOUtils.toString(testUrl.getInputStream()));
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
}
70+
}
71+
72+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.hncu.p2_2_2SpringEL;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: 陈浩翔.
8+
* Date: 2016/11/13.
9+
* Time: 下午 11:44.
10+
* Explain:运行类
11+
*/
12+
public class Main {
13+
14+
public static void main(String[] args) {
15+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
16+
ElConfig resourceService = context.getBean(ElConfig.class);
17+
resourceService.outputResource();
18+
context.close();
19+
}
20+
21+
}

0 commit comments

Comments
 (0)