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注入 二.复杂类型注入
 
随机推荐
- Q我音乐
 - JavaScript中instanceof与typeof运算符的用法及区别详细解析
			
JavaScript中的instanceof和typeof常被用来判断一个变量是什么类型的(实例),但它们的使用还是有区别的: typeof 运算符 返回一个用来表示表达式的数据类型的字符串. typ ...
 - 基于VC的串行通信技术应用实例
			
在工业控制中,串口是常用的计算机与外部串行设备之间的数据传输通道,由于串行通信方便易行,所以应用广泛. 本文将介绍在Windows平台下串行通信的工作机制和用Visual C++设计串行通信程序的 ...
 - jQuery 的ready事件和 JavaScript 的load事件对比
			
为了理解2个事件的异同,先了解一下HTML文档加载顺序 HTML DOM文档加载步骤 HTML DOM文档加载是按顺序执行的,这与浏览器的渲染方式有关,一般浏览器渲染操作的顺序大致按如下几个步骤 1, ...
 - PAT - IO-01. 表格输出(5)
			
题目: 本题要求编写程序,按照规定格式输出表格. 输入格式: 本题目没有输入. 输出格式: 要求严格按照给出的格式输出下列表格: ----------------------------------- ...
 - python:UnboundLocalError: local variable 'xxx' referenced before assignment
			
近来一直都在学习python语言,偶然在伯乐在线看到2017年京东C/C++的面试题.就打算用python+ST3 IDE顺便敲下面试题代码. 原题 C语言: #include <stdio.h ...
 - apache静态文件配置
			
开发环境配置 需要下面几个步骤 1. 在app目录下创建static目录,将静态文件和相关文件夹放到此目录下,如your_app/static/img等 2. 确保settings.py中的INSTA ...
 - Maven插件开发
			
Maven为我们提供了丰富的插件资源,使得开发调试过程中非常方便,可以满足大多数场景下的需求.当然有时候,我们也需要根据需求定制自己的插件.下面是在开发Maven插件时的一点备忘录,具体的开发流程请G ...
 - 『Python』 多线程 端口扫描器
			
0x 00 Before Coding 当端口打开时,向端口发送 TCP SYN 请求,会返回一个 ACK 响应: 当端口关闭,返回的是 RST 响应: 0x 01 Coding 可以用 socke ...
 - temp gbk2utf8
			
__author__ = 'root' # -*- coding: utf-8 -*- ps = '/data/poitestdata/行政地名.csv' pt = '/data/poitestdat ...