Skip to content

Commit 214d4fd

Browse files
committed
1 parent 68410f8 commit 214d4fd

File tree

11 files changed

+249
-0
lines changed

11 files changed

+249
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>spring-core-ioc</module>
1515
<module>spring-bean-lifecycle</module>
1616
<module>spring-core-beanpostprocessor</module>
17+
<module>spring-core-environment-abstraction</module>
1718
</modules>
1819

1920
<properties>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-framework-tutorial-parent</artifactId>
7+
<groupId>com.jstobigdata</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>spring-core-environment-abstraction</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-core</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-context</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>com.jstobigdata</groupId>
30+
<artifactId>spring-tutorial-boms</artifactId>
31+
<version>1.0-SNAPSHOT</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
</dependencies>
36+
</dependencyManagement>
37+
38+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.jsbd.propertysource;
2+
3+
import org.springframework.context.annotation.*;
4+
5+
@Configuration
6+
@PropertySource("classpath:app.properties")
7+
@ComponentScan("com.jsbd.propertysource")
8+
//@ImportResource("classpth:${app.name}.xml")
9+
public class AppConfig {
10+
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.jsbd.propertysource;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.core.env.Environment;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class AppLogger {
9+
10+
@Autowired
11+
private Environment env;
12+
13+
public void printLogLevel() {
14+
String logLevel = env.getProperty("app.log.level");
15+
System.out.println(logLevel);
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.jsbd.propertysource;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
import java.util.StringJoiner;
7+
8+
@Component
9+
public class ConnectionManager {
10+
11+
@Value("${app.database.url}")
12+
private String url;
13+
14+
@Value("${app.database.username}")
15+
private String username;
16+
17+
@Value("${app.database.password}")
18+
private String password;
19+
20+
@Value("${no.value}")
21+
private String noValue;
22+
23+
@Override
24+
public String toString() {
25+
return new StringJoiner(", ", ConnectionManager.class.getSimpleName() + "[", "]")
26+
.add("url='" + url + "'")
27+
.add("username='" + username + "'")
28+
.add("password='" + password + "'")
29+
.add("noValue='" + noValue +"'")
30+
.toString();
31+
}
32+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.jsbd.propertysource;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
6+
public class TestClass {
7+
public static void main(String[] args) {
8+
ApplicationContext context
9+
= new AnnotationConfigApplicationContext(AppConfig.class);
10+
System.out.println(context.getBean(ConnectionManager.class));
11+
12+
context.getBean(AppLogger.class).printLogLevel();
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
app.database.url=http://random-url
2+
app.database.password=abcd@123
3+
app.database.username=root
4+
app.log.level=DEBUG
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package basic.ioc.beanfactory;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Scope;
7+
8+
import java.util.Random;
9+
import java.util.UUID;
10+
11+
@Configuration
12+
@ComponentScan("basic.ioc.autowire")
13+
public class AutowireBeanConfig {
14+
15+
@Bean
16+
@Scope("prototype")
17+
public Item item(){
18+
return new Item(
19+
new Random().nextLong(),
20+
UUID.randomUUID().toString()
21+
);
22+
}
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package basic.ioc.beanfactory;
2+
3+
import java.util.StringJoiner;
4+
5+
public class Item {
6+
private Long id;
7+
private String name;
8+
9+
public Item() {
10+
}
11+
12+
public Item(Long id, String name) {
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public void setId(Long id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return new StringJoiner(", ", Item.class.getSimpleName() + "[", "]")
36+
.add("id=" + id)
37+
.add("name='" + name + "'")
38+
.toString();
39+
}
40+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package basic.ioc.beanfactory;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Scope;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.util.StringJoiner;
8+
import java.util.UUID;
9+
10+
@Component
11+
@Scope("prototype")
12+
public class Store {
13+
private String id;
14+
15+
/* Field injection is not recommended. Instead use, Constructor
16+
or Setter injection
17+
*/
18+
@Autowired
19+
private Item item;
20+
21+
public Store() {
22+
id = UUID.randomUUID().toString();
23+
}
24+
25+
public Store(String id, Item item) {
26+
this.id = id;
27+
this.item = item;
28+
}
29+
30+
public String getId() {
31+
return id;
32+
}
33+
34+
public void setId(String id) {
35+
this.id = id;
36+
}
37+
38+
public Item getItem() {
39+
return item;
40+
}
41+
42+
public void setItem(Item item) {
43+
this.item = item;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return new StringJoiner(", ", Store.class.getSimpleName() + "[", "]")
49+
.add("id=" + id)
50+
.add("item=" + item)
51+
.toString();
52+
}
53+
}

0 commit comments

Comments
 (0)