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注入 二.复杂类型注入
随机推荐
- JQUERY1.9学习笔记 之内容过滤器(四) parent选择器
描述:选择至少包含一个子节点的元素(一个标签或是文本). 例:找出所有有子元素的td标签,包含文本. <!doctype html><html lang="en" ...
- JQuery在光标位置插入内容
(function($) { $.fn.extend({ insertAtCaret: function(myValue) { var $t = $(this)[0]; //IE if (docume ...
- Phalcon 的 bootstrap.php 自动加载完成;非常人性化的设计
<?php /** * Bootstraps the application */ use Phalcon\DI\FactoryDefault as PhDi, Phalcon\Config a ...
- Memcached-1.4.4启动参数——手动设置chunk大小的上限
最近在看memcached的源代码,源码包是memcached-1.4.22,一开始看memcached.c的main函数的时候发现了和1.2.8的不同之处. 可能在1.4.22版本之前就已经添加了这 ...
- cms中if标签的使用
<#if order.EnFriendly ?? && order.EnFriendly==1> <td class="text-center"& ...
- matlab图像类型转换以及uint8、double、im2double、im2uint8和mat2gray等说明
转自:http://blog.csdn.net/fx677588/article/details/53301740 1. matlab图像保存说明 matlab中读取图片后保存的数据是uint8类型( ...
- 完美解决VMware Workstation : Could not open /dev/vmmon: No such file or directory
root@tiger:/usr/bin# cd /etc/init.d root@tiger:/usr/bin# sudo mv /usr/lib/vmware/modules/binary /usr ...
- -_-#【Backbone】Router
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- LVS安装配置
LVS安装部署 一.LVS安装(CENTOS) 1.LVS模块ip_vs已经内置在LINUX内核中,一般情况下ip_vs并没有启动,可以通过lsmod | grep ip_vs查看,能够看到信息表示模 ...
- XJOI——NOIP2015提高组模拟题19-day1——观光旅行
http://www.hzxjhs.com:83/contest/493/problem/3 [题目大意] 给定一个有n(n<=500000)个点,m(1<=500000)条边的无向图.给 ...