目录
建议学习完lombok以及Spring基础中IOC容器后阅读此文
1.配置文件的使用方式
在resource下创建test.properties,输入一个键值对
test.para=spel
在配置文件中加入@PropertySource注解来指定配置文件
@Configuration
@ComponentScan("org.example.entity")
@PropertySource("classpath:test.properties")
public class MainConfiguration {
}
创建一个Test实体类,加入一个属性,并且加入@Value,传入配置文件里面的参数
@Data
@Component
public class Test {
@Value("${test.para}")
String para;
}
执行一下代码
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
System.out.println(context.getBean(Test.class).getPara());
}
}
也可以在传入参数时利用@Value来传入参数
public void method(@Value("10") String str){
System.out.println(str);
}
2.SpEL表达式
1.SpEL的基本操作
SpringEL表达式用到解析器接口ExpressionParser,我们使用默认的实现SpelExpressionParser。使用Expression来获取解析器解析后的Expression。然后通过expression的getValue方法来获取解析的值。
SpEL表达式可以用来解析运算结果
public class Main {
public static void main(String[] args) {
ExpressionParser expressionParser = new SpelExpressionParser();
Expression expression = expressionParser.parseExpression("1+1==2");
System.out.println(expression.getValue());
}
}
SpEL表达式也可以用来打印字符串,甚至可以直接调用方法
ExpressionParser expressionParser = new SpelExpressionParser();
Expression expression = expressionParser.parseExpression("'hello'");
System.out.println(expression.getValue());
ExpressionParser expressionParser = new SpelExpressionParser();
Expression expression = expressionParser.parseExpression("'hello'.length");
System.out.println(expression.getValue());
甚至还可以通过传入对象获取对象中表达式对应的属性,配合IOC容器使用更加丝滑。(其实就是通过get方法来获取对应的属性,因此要加上get方法,这里直接使用lombok)
@Data
@Component
public class Test {
String para = "hello bro";
}
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
ExpressionParser expressionParser = new SpelExpressionParser();
Expression expression = expressionParser.parseExpression("para");
System.out.println(expression.getValue(context.getBean(Test.class)));
如果需要导入一些类,我们还可以这样使用,T相当于导入这个类
Expression exp = parser.parseExpression("T(java.lang.Math).random()");
System.out.println(exp.getValue());
2.集合类操作
SpEL表达式还支持集合类的操作
我们可以用SpEL表达式来取出类中集合属性的元素,注意不能直接获取集合中的元素,必须是集合在某个类中,然后getValue是集合的变量名必须和类中的定义相同且下标或者键是合理的,传入参数时传入对应的类对象即可。
@Component
public class Student {
public List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));
public Map<String,Integer> map = Map.of("k1",1,"k2",2);
}
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
Student student = context.getBean(Student.class);
ExpressionParser expressionParser = new SpelExpressionParser();
Expression exp = expressionParser.parseExpression("list[3]");
System.out.println(exp.getValue(student));
exp = expressionParser.parseExpression("map['k2']");
System.out.println(exp.getValue(student));
}
}
并且我们也可以通过SpEL表达式来快速创建集合
list集合
ExpressionParser expressionParser = new SpelExpressionParser();
Expression exp = expressionParser.parseExpression("{1,2,3,4,5}");
List<Integer> list = (List<Integer>) exp.getValue();
list.forEach(System.out::println);
map集合
ExpressionParser expressionParser = new SpelExpressionParser();
Expression exp = expressionParser.parseExpression("{k1:1,k2:2,k3:3}");
Map<String,Integer> map = (Map<String, Integer>) exp.getValue();
Set<String> set = map.keySet();
set.forEach(s -> {
System.out.println(s+" "+map.get(s));
});
甚至我们可以直接通过很简单的表达式来筛选集合中的元素
@Data
@Component
public class Student {
public record Person(Integer id, String name){}
List<Person> list = List.of(new Person(1,"小明"),new Person(2,"校长"),new Person(3,"小王"));
}
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
Student student = context.getBean(Student.class);
ExpressionParser expressionParser = new SpelExpressionParser();
System.out.println(student.getList());
Expression exp = expressionParser.parseExpression("list.?[id>1]");
System.out.println(exp.getValue(student));
并且我们还可以通过一个表达式来获取其中一个域的集合
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
Student student = context.getBean(Student.class);
ExpressionParser expressionParser = new SpelExpressionParser();
Expression exp = expressionParser.parseExpression("list.![name]");
System.out.println(exp.getValue(student));
并且SpEL表达式还为我们提供了类似于Optional处理异常方式。
@Data
@Component
public class Student {
public String name;
}
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
Student student = context.getBean(Student.class);
ExpressionParser expressionParser = new SpelExpressionParser();
Expression exp = expressionParser.parseExpression("name?.toString");
System.out.println(exp.getValue(student));
还有一些其他的SpEL表达式,感兴趣的可以去Spring官网查看。
学习参考:
如有错误,感谢纠正。