Skip to content

Commit ed1c562

Browse files
committed
Add code for Spring Expression Language article
2 parents 3a5dc3d + f2dd909 commit ed1c562

15 files changed

Lines changed: 1024 additions & 0 deletions

File tree

‎spring-spel/pom.xml‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>spel</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.12</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.hamcrest</groupId>
19+
<artifactId>hamcrest-all</artifactId>
20+
<version>1.3</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework</groupId>
24+
<artifactId>spring-context</artifactId>
25+
<version>4.0.6.RELEASE</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework</groupId>
29+
<artifactId>spring-test</artifactId>
30+
<version>4.0.6.RELEASE</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<version>3.3</version>
39+
<configuration>
40+
<debug>true</debug>
41+
<optimize>true</optimize>
42+
<source>1.8</source>
43+
<target>1.8</target>
44+
<encoding>UTF-8</encoding>
45+
<showDeprecation>true</showDeprecation>
46+
<showWarnings>true</showWarnings>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.spring.spel;
2+
3+
import com.baeldung.spring.spel.examples.SpelConditional;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
public class SpelProgram {
8+
public static void main(String[] args) {
9+
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
10+
SpelConditional spelCollections = (SpelConditional) context.getBean("spelConditional");
11+
12+
// Here you can choose which bean do you want to load insted of spelConditional: spelCollections, spelLogical, etc.
13+
14+
System.out.println(spelCollections);
15+
}
16+
17+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.spring.spel.entity;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("someCar")
7+
public class Car {
8+
@Value("Some make")
9+
private String make;
10+
@Value("Some model")
11+
private String model;
12+
@Value("#{engine}")
13+
private Engine engine;
14+
@Value("#{engine.horsePower}")
15+
private int horsePower;
16+
@Value("2007")
17+
private int yearOfProduction;
18+
@Value("#{engine.capacity >= 3000}")
19+
private boolean isBigEngine;
20+
@Value("#{someCar.yearOfProduction > 2016}")
21+
private boolean isNewCar;
22+
23+
public String getMake() {
24+
return make;
25+
}
26+
27+
public void setMake(String make) {
28+
this.make = make;
29+
}
30+
31+
public String getModel() {
32+
return model;
33+
}
34+
35+
public void setModel(String model) {
36+
this.model = model;
37+
}
38+
39+
public Engine getEngine() {
40+
return engine;
41+
}
42+
43+
public void setEngine(Engine engine) {
44+
this.engine = engine;
45+
}
46+
47+
public int getHorsePower() {
48+
return horsePower;
49+
}
50+
51+
public void setHorsePower(int horsePower) {
52+
this.horsePower = horsePower;
53+
}
54+
55+
public int getYearOfProduction() {
56+
return yearOfProduction;
57+
}
58+
59+
public void setYearOfProduction(int yearOfProduction) {
60+
this.yearOfProduction = yearOfProduction;
61+
}
62+
63+
public boolean isBigEngine() {
64+
return isBigEngine;
65+
}
66+
67+
public void setBigEngine(boolean bigEngine) {
68+
isBigEngine = bigEngine;
69+
}
70+
71+
public boolean isNewCar() {
72+
return isNewCar;
73+
}
74+
75+
public void setNewCar(boolean newCar) {
76+
isNewCar = newCar;
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "Car{" +
82+
"make='" + make + '\'' +
83+
", model='" + model + '\'' +
84+
", engine=" + engine +
85+
", horsePower=" + horsePower +
86+
", yearOfProduction=" + yearOfProduction +
87+
", isBigEngine=" + isBigEngine +
88+
", isNewCar=" + isNewCar +
89+
'}';
90+
}
91+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.spring.spel.entity;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
@Component("carPark")
11+
public class CarPark {
12+
private List<Car> cars = new ArrayList<>();
13+
private Map<String, Car> carsByDriver = new HashMap<>();
14+
15+
public CarPark() {
16+
Car model1 = new Car();
17+
model1.setMake("Good company");
18+
model1.setModel("Model1");
19+
model1.setYearOfProduction(1998);
20+
21+
Car model2 = new Car();
22+
model2.setMake("Good company");
23+
model2.setModel("Model2");
24+
model2.setYearOfProduction(2005);
25+
26+
cars.add(model1);
27+
cars.add(model2);
28+
29+
carsByDriver.put("Driver1", model1);
30+
carsByDriver.put("Driver2", model2);
31+
}
32+
33+
public List<Car> getCars() {
34+
return cars;
35+
}
36+
37+
public void setCars(List<Car> cars) {
38+
this.cars = cars;
39+
}
40+
41+
public Map<String, Car> getCarsByDriver() {
42+
return carsByDriver;
43+
}
44+
45+
public void setCarsByDriver(Map<String, Car> carsByDriver) {
46+
this.carsByDriver = carsByDriver;
47+
}
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.spring.spel.entity;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("engine")
7+
public class Engine {
8+
@Value("3200")
9+
private int capacity;
10+
@Value("250")
11+
private int horsePower;
12+
@Value("6")
13+
private int numberOfCylinders;
14+
15+
public int getCapacity() {
16+
return capacity;
17+
}
18+
19+
public void setCapacity(int capacity) {
20+
this.capacity = capacity;
21+
}
22+
23+
public int getHorsePower() {
24+
return horsePower;
25+
}
26+
27+
public void setHorsePower(int horsePower) {
28+
this.horsePower = horsePower;
29+
}
30+
31+
public int getNumberOfCylinders() {
32+
return numberOfCylinders;
33+
}
34+
35+
public void setNumberOfCylinders(int numberOfCylinders) {
36+
this.numberOfCylinders = numberOfCylinders;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "Engine{" +
42+
"capacity=" + capacity +
43+
", horsePower=" + horsePower +
44+
", numberOfCylinders=" + numberOfCylinders +
45+
'}';
46+
}
47+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.baeldung.spring.spel.examples;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("spelArithmetic")
7+
public class SpelArithmetic {
8+
@Value("#{19 + 1}")
9+
private double add;
10+
11+
@Value("#{'Some string ' + 'plus other string'}")
12+
private String addString;
13+
14+
@Value("#{20 - 1}")
15+
private double subtract;
16+
17+
@Value("#{10 * 2}")
18+
private double multiply;
19+
20+
@Value("#{36 / 2}")
21+
private double divide;
22+
@Value("#{36 div 2}")
23+
private double divideAlphabetic;
24+
25+
@Value("#{37 % 10}")
26+
private double modulo;
27+
@Value("#{37 mod 10}")
28+
private double moduloAlphabetic;
29+
30+
@Value("#{2 ^ 9}")
31+
private double powerOf;
32+
33+
@Value("#{(2 + 2) * 2 + 9}")
34+
private double brackets;
35+
36+
public double getAdd() {
37+
return add;
38+
}
39+
40+
public void setAdd(double add) {
41+
this.add = add;
42+
}
43+
44+
public String getAddString() {
45+
return addString;
46+
}
47+
48+
public void setAddString(String addString) {
49+
this.addString = addString;
50+
}
51+
52+
public double getSubtract() {
53+
return subtract;
54+
}
55+
56+
public void setSubtract(double subtract) {
57+
this.subtract = subtract;
58+
}
59+
60+
public double getMultiply() {
61+
return multiply;
62+
}
63+
64+
public void setMultiply(double multiply) {
65+
this.multiply = multiply;
66+
}
67+
68+
public double getDivide() {
69+
return divide;
70+
}
71+
72+
public void setDivide(double divide) {
73+
this.divide = divide;
74+
}
75+
76+
public double getDivideAlphabetic() {
77+
return divideAlphabetic;
78+
}
79+
80+
public void setDivideAlphabetic(double divideAlphabetic) {
81+
this.divideAlphabetic = divideAlphabetic;
82+
}
83+
84+
public double getModulo() {
85+
return modulo;
86+
}
87+
88+
public void setModulo(double modulo) {
89+
this.modulo = modulo;
90+
}
91+
92+
public double getModuloAlphabetic() {
93+
return moduloAlphabetic;
94+
}
95+
96+
public void setModuloAlphabetic(double moduloAlphabetic) {
97+
this.moduloAlphabetic = moduloAlphabetic;
98+
}
99+
100+
public double getPowerOf() {
101+
return powerOf;
102+
}
103+
104+
public void setPowerOf(double powerOf) {
105+
this.powerOf = powerOf;
106+
}
107+
108+
public double getBrackets() {
109+
return brackets;
110+
}
111+
112+
public void setBrackets(double brackets) {
113+
this.brackets = brackets;
114+
}
115+
116+
@Override
117+
public String toString() {
118+
return "SpelArithmetic{" +
119+
"add=" + add +
120+
", addString='" + addString + '\'' +
121+
", subtract=" + subtract +
122+
", multiply=" + multiply +
123+
", divide=" + divide +
124+
", divideAlphabetic=" + divideAlphabetic +
125+
", modulo=" + modulo +
126+
", moduloAlphabetic=" + moduloAlphabetic +
127+
", powerOf=" + powerOf +
128+
", brackets=" + brackets +
129+
'}';
130+
}
131+
}

0 commit comments

Comments
 (0)