spring04 spel注入
1.创建需要的实体类对象
public class Student { //学生实体类
private String name; //姓名
private Integer age; //年龄
private Grade grade; //年级
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", grade=" + grade
+ "]";
}
public Student() {
super();
}
public Student(String name, Integer age, Grade grade) {
super();
this.name = name;
this.age = age;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
//根据年龄的不同返回age不同的值
public Integer changeAge(){
return (this.age>25)?20:this.age;
}
}
Student实体类
public class Grade { //年级实体类
private String name; //年级名称
@Override
public String toString() {
return "Grade [name=" + name + "]";
}
public Grade() {
super();
}
public Grade(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Grade实体类
2.创建对应的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- spel 注入 spring el表达式 需要实体类中 有对应的get()
第一个学生 -->
<bean id="student1" class="cn.bdqn.bean.Student">
<property name="name" value="小黑"/>
<property name="age" value="#{T(java.lang.Math).random()*50}"/>
</bean> <!-- 第二个学生 name属性值 是 第一个学生的 age值 是方法中获取的 -->
<bean id="student2" class="cn.bdqn.bean.Student">
<property name="name" value="#{student1.name}"/>
<!-- <property name="age" value="#{student1.age>30?25:student1.age}"/> -->
<!--直接把年龄的判断写在类中 并且得到返回值 -->
<property name="age" value="#{student1.changeAge()}"/>
</bean> <!--匿名内部bean 年级只属于指定的Student -->
<!-- <bean id="student" class="cn.bdqn.bean.Student">
<property name="name" value="小黑"/>
<property name="age" value="50"/>
<property name="grade">
其他的bean中 无法访问
<bean id="grade" class="cn.bdqn.bean.Grade" p:name="1年级"/>
</property>
</bean> --> </beans>
applicationContext.xml文件
3.创建测试类
public class StudentTest {
//使用spel注入
@Test
public void test01(){
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
Student student1=(Student) context.getBean("student1");
Student student2=(Student) context.getBean("student2");
System.out.println("student1信息:"+student1);
System.out.println("student2信息:"+student2);
}
//匿名内部bean
@Test
public void test02(){
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
Student student=(Student) context.getBean("student");
System.out.println("student信息:"+student);
}
}
测试类
spring04 spel注入的更多相关文章
- SpringCloud Function SpEL注入
SpringCloud Function SpEL注入 漏洞分析
- Spring的3.0提供了一种:SpEL注入方式(了解)
1. SpEL:Spring Expression Language是Spring的表达式语言,有一些自己的语法 2. 语法 * #{SpEL} 3. 例如如下的代码 <!-- SpEL的方式 ...
- day38 13-Spring的Bean的属性的注入:SpEL注入
Spring2.5提供了名称空间p注入属性的方式,Spring3.几提供了SpEL属性注入的方式. <?xml version="1.0" encoding="UT ...
- SpringBoot SpEL表达式注入漏洞-分析与复现
目录 0x00前言 0x01触发原因 0x02调试分析 0x03补丁分析 0x04参考文章 影响版本: 1.1.0-1.1.12 1.2.0-1.2.7 1.3.0 修复方案:升至1.3.1或以上版本 ...
- SpEL表达式注入
一.内容简介 Spring Expression Language(简称SpEL)是一种强大的表达式语言,支持在运行时查询和操作对象图.语言语法类似于Unified EL,但提供了额外的功能,特别是方 ...
- SpEL表达式注入漏洞学习和回显poc研究
目录 前言 环境 基础学习和回显实验 语法基础 回显实验 BufferedReader Scanner SpEL漏洞复现 低版本SpringBoot中IllegalStateException CVE ...
- JAVAEE——spring01:介绍、搭建、概念、配置详解、属性注入和应用到项目
一.spring介绍 1.三层架构中spring位置 2.spring一站式框架 正是因为spring框架性质是属于容器性质的. 容器中装什么对象就有什么功能.所以可以一站式. 不仅不排斥其他框架,还 ...
- Spring———bean的创建方式,注入方式,复杂类型注入 概括
Spring相关概念和类 1.IOC inverse of control 控制反转 反转了创建对象的方式 以前:new 对象,管理和维护 ...
- Spring 的属性注入
一.注入方式 (1)set方法注入 (2)构造函数注入 (3)p名称空间注入 (4)spel注入 二.复杂类型注入
随机推荐
- Qt中QWidget加入到父类QWidget不能显示的问题
最近忙活了半天发现了一个不容易发现的问题,比如我有一个父类的自定义Widget,并且自己设置了Pattle,但这个时候我如果再在当前Widget内部加入自己的widget就不能正常显示,但是加QPus ...
- 数据库备份工具mysqldump重要参数详解
1. --single-transaction InnoDB 表在备份时,通常启用选项 --single-transaction 来保证备份的一致性,实际上它的工作原理是设定本次会话的隔离级别为:RE ...
- terminal命令
新建一个文件并写入内容: echo hello world > a.txt (每次echo都会重写文件) 新建文件: touch a.txt 新建目录: mkdir work 用vim打开文件 ...
- 《asp.net mvc3 高级编程》第三章 视图
一.视图的作用 视图的职责是向用户提供界面.从ASP.NET MVC3开始,视图数据也可以通过ViewBag属性访问.例如:ViewBag.Message 就等于ViewData["Mess ...
- 修改PHP的默认时区
每个地区都有自己的本地时间,在网上及无线电通信中,时间的转换问题显得格外突出.整个地球分为24个时区,每个时区都有自己的本地时间.在国际无线电或网络通信场合,为了统一起见,使用一个统一的时间,成为通用 ...
- WinPcap编程(前言&&学习)
计算机网络课设要求用WinPcap写对ARP包的接收与发送. 所以学了一下WinPcap的内容. 参考的博客: http://blog.csdn.net/htttw/article/details/7 ...
- UVA - 11346 Probability (概率)
Description Probability Time Limit: 1 sec Memory Limit: 16MB Consider rectangular coordinate system ...
- easyui combo+pagination 图标选择器
从数据库读取分页显示 $(function () { initTable(1, 180); $('#cc').combo({ editable: false, panelWidth: 'auto', ...
- bzoj2487: Super Poker II
Description I have a set of super poker cards, consisting of an infinite number of cards. For each p ...
- C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h> ...