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注入的更多相关文章

  1. SpringCloud Function SpEL注入

    SpringCloud Function SpEL注入 漏洞分析

  2. Spring的3.0提供了一种:SpEL注入方式(了解)

    1. SpEL:Spring Expression Language是Spring的表达式语言,有一些自己的语法 2. 语法 * #{SpEL} 3. 例如如下的代码 <!-- SpEL的方式 ...

  3. day38 13-Spring的Bean的属性的注入:SpEL注入

    Spring2.5提供了名称空间p注入属性的方式,Spring3.几提供了SpEL属性注入的方式. <?xml version="1.0" encoding="UT ...

  4. 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或以上版本 ...

  5. SpEL表达式注入

    一.内容简介 Spring Expression Language(简称SpEL)是一种强大的表达式语言,支持在运行时查询和操作对象图.语言语法类似于Unified EL,但提供了额外的功能,特别是方 ...

  6. SpEL表达式注入漏洞学习和回显poc研究

    目录 前言 环境 基础学习和回显实验 语法基础 回显实验 BufferedReader Scanner SpEL漏洞复现 低版本SpringBoot中IllegalStateException CVE ...

  7. JAVAEE——spring01:介绍、搭建、概念、配置详解、属性注入和应用到项目

    一.spring介绍 1.三层架构中spring位置 2.spring一站式框架 正是因为spring框架性质是属于容器性质的. 容器中装什么对象就有什么功能.所以可以一站式. 不仅不排斥其他框架,还 ...

  8. Spring———bean的创建方式,注入方式,复杂类型注入 概括

    Spring相关概念和类    1.IOC             inverse of control    控制反转   反转了创建对象的方式            以前:new 对象,管理和维护 ...

  9. Spring 的属性注入

    一.注入方式 (1)set方法注入 (2)构造函数注入 (3)p名称空间注入 (4)spel注入 二.复杂类型注入

随机推荐

  1. 137 Single Number II(找唯一数Medium)

    题目意思:一个int数组,有一个数只出现一次,其他数均出现三次,找到这个唯一数 思路: 1.将所有数用2进制表示,计算每一位的数字和  1*3*n1+0*3*n2+c   唯一数对应位的数字(0或者1 ...

  2. 关闭iOS的自动更新

    Safari打开网址https://oldcat.me/web/NOOTA9.mobileconfig,安装描述文件,就不会自动下载和提示更新最新的iOS了

  3. CSS 字体描边

    -webkit-text-stroke: 2px #; text-stroke: 2px #; -o-text-stroke: 2px #;

  4. Javascript中的onclick事件

    示例: <script type="text/javascript"> function onSubmit() { alert("保存数据"); } ...

  5. C# 代码中 计算某个函数 或WebService 请求花费时间

    /// 计算请求所花费的时间 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start( ...

  6. javaWeb遍历获取session中的值

    //方法一:通过遍历的方法进行遍历 String FileName=""; HttpSession session=request.getSession();//获取session ...

  7. 关于extern "C" 的用法

    而在C语言的头文件中,对其外部函数只能指定为extern类型,C语言中不支持extern "C"声明,在.c文件中包含了extern "C"时会出现编译语法错误 ...

  8. poj 2516Minimum Cost

    http://poj.org/problem?id=2516 #include<cstdio> #include<cstring> #include<algorithm& ...

  9. 调用系统API还是很高效的,不必担心性能

    代码如下: void MainWindow::on_pushButton_2_clicked() { QTime total; total.start(); ; ; i<=*; i++) { Q ...

  10. sort_buffer_size:

    sort_buffer_size: 每个session 必须执行一个排序 分配一个 这个大小的buffer. sort_buffer_size 不特定的对于任何存储引擎适用于一般的优化方式. sort ...