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. 【USACO 2.1.2】法雷序列

    [问题描述]     对任意给定的一个自然数 n(n<=160), 将分母小于等于 n 的不可约的真分数按上升的次序排序 , 并且在第一个分数前加上 0/1, 而在最后一个分数后加上 1/1, ...

  2. ios 中获得应用程序名称和版本号

    IOS程序中的应用名称和版本号在 info.plist 文件中存储着,要想在程序中获得需要使用 NSBundle 对象 下面是示例代码: NSBundle *bundle = [NSBundle ma ...

  3. 使用Ajax.ActionLink时,点击对应的按钮会重新加载一个页面,而不是在当前页面的指定模块加载

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. HDU 2955(01背包问题)

    M - 01背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descript ...

  5. 日期选择器(Query+bootstrap和js两种方式)

    日期选择是在下拉列表中选择年.月.日,年显示前后的五年,12个月,日就是有30.31.29.28天的区别,随着月份的变而变 一.js方式的日期选择 (1)首先就是三个下拉列表了,点击年.月.日显示列表 ...

  6. block 高级

    //从后往前传值 声明block属性 //copy 目的是 将栈区的block拷贝一份到堆区 @property(nonatomic,copy)void (^sendValueBlock)(id); ...

  7. 终端编写c程序

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:JackAlan链接:http://www.zhihu.com/question/21483375/answer/322672 ...

  8. CentOS 6.4 64位 源码编译hadoop 2.2.0

    搭建环境:Centos 6.4 64bit 1.安装JDK 参考这里2.安装mavenmaven官方下载地址,可以选择源码编码安装,这里就直接下载编译好的wget http://mirror.bit. ...

  9. Netbeans8下 Weblogic EJB案例

    1:接口 @Remote public interface XgmZzsNssb {} 2:实现 @Stateless(mappedName="XgmZzsNssbImpl") @ ...

  10. 如何删除windows服务(sc.exe删除和注册表删除两种方法)

    一.什么是Windows服务 Windows服务也称为Windows Service,它是Windows操作系统和Windows网络的基础,属于系统核心的一部分,它支持着整个Windows的各种操作. ...