Spring Framework-03-01-Data Validation and SpringEL


Insert picture description here


Insert picture description here
Data verification is mainly divided into front-end verification and back-end verification, which should not be omitted

Spring's verification is stricter.

Data verification should not be limited to the web layer to process, it should be verified wherever data verification is needed

Based on the above considerations, Spring has designed a Validator interface that is convenient and can be used at all layers

A simple example of Spring verification

Insert picture description here

Insert picture description here
Insert picture description here


Use of Validator

Insert picture description here
Insert picture description here

Insert picture description here




Introduction to SpringEL

Introduction to SpringEL

  • Spring Expression Language (SpEL for short) is a powerful expression language that supports query and operation of runtime object navigation graph functions
  • Its syntax is similar to traditional EL, but provides additional functions, the most outstanding is function call and simple string template function
  • Although the SpEL engine is used as the basis of expression analysis in the Spring composition, it does not directly depend on Spring and can be used independently

It supports calculations and can call methods, which is more powerful than EL. It
can realize dynamic injection of Bean data

Insert picture description here

Insert picture description here

The first line of code: EL expression parser

SpringEL use

  • SpEL is an expression language similar to OGNL and JSF EL, which can construct complex expressions at runtime, access object properties, and call object methods
  • SpEL expressions can be defined with XML or annotation-based configuration metadata
  • Define the grammatical form of the expression: #{<expression string>}

OGNL: Object navigation graph language, often used in the strust framework.
JSF EL: java server face

Configure data source based on xml

Insert picture description here

Project structure

Insert picture description here

Computer.java

package com;

import javax.imageio.stream.MemoryCacheImageInputStream;

public class Computer {
    
    
	private String brand;//品牌
	private Memory memory;//内存
	private int count;
	
	
	public String getBrand() {
    
    
		return brand;
	}
	public void setBrand(String brand) {
    
    
		this.brand = brand;
	}
	public Memory getMemory() {
    
    
		return memory;
	}
	public void setMemory(Memory memory) {
    
    
		this.memory = memory;
	}
	public int getCount() {
    
    
		return count;
	}
	public void setCount(int count) {
    
    
		this.count = count;
	}//内存大小

	public void show() {
    
    
		System.out.println("内存的大小为 "+count);
	}
	
}


IntelCPU.java

package com;

public class IntelCpu {
    
    

	public void run() {
    
    
		System.out.println("intel cpu is running");
	}
}

Memory.java

package com;

public class Memory {
    
    
	private int memCount;//内存大小
	


	public int getMemCount() {
    
    
		return memCount;
	}

	public void setMemCount(int memCount) {
    
    
		this.memCount = memCount;
	}
	
}

Test.java

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    

	public static void main(String[] args) {
    
    
		
//		//1. 启动Spring容器,参数是数据配置的文件名,
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//		//2. 从容器中拿到对象
		Computer pc = (Computer)ctx.getBean("Computer");
		pc.show();
	}

}

beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <bean id="Memory" class="com.Memory">
    	<property name="memCount" value="1024"/>
    </bean>
    
    <bean id="Computer" class="com.Computer">
    	<property name="brand" value="Lenovo"/>
    	<property name="memory" ref="Memory"/>
    	<property name="count" value="#{Memory.memCount}"/>
    </bean>
    
</beans>


Configure data sources based on annotations

Insert picture description here

Project structure

Insert picture description here

Computer.java

package com;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Computer")
public class Computer {
    
    
	private String brand;//品牌
	private Memory memory;//内存
	
	//动态赋值,#{}不可少,参数为类.属性
	//因为没有在Memory类上起别名,默认小写开头
	@Value("#{Memory.memCount}")
	private int count;//内存大小
	
	
	public String getBrand() {
    
    
		return brand;
	}
	public void setBrand(String brand) {
    
    
		this.brand = brand;
	}
	public Memory getMemory() {
    
    
		return memory;
	}
	public void setMemory(Memory memory) {
    
    
		this.memory = memory;
	}
	public int getCount() {
    
    
		return count;
	}
	public void setCount(int count) {
    
    
		this.count = count;
	}//内存大小

	public void show() {
    
    
		System.out.println("内存的大小为 "+count);
	}
	
}


Memory.java

package com;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Memory")
public class Memory {
	@Value("2048")
	private int memCount;
	
	public Memory() {
		System.out.println("这是Memory类打印的信息");
	}

	public int getMemCount() {
		return memCount;
	}

	public void setMemCount(int memCount) {
		this.memCount = memCount;
	}
	
}

IntelCPU

package com;

public class IntelCpu {

	public void run() {
		System.out.println("intel cpu is running");
	}
}

Test.java

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    

	public static void main(String[] args) {
    
    
		
//		//1. 启动Spring容器,参数是数据配置的文件名,
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//		//2. 从容器中拿到对象
		Computer pc = (Computer)ctx.getBean("Computer");
		pc.show();
	}

}

beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
	<context:annotation-config></context:annotation-config>
	<!-- 扫描注解com包 -->
	<context:component-scan base-package="com"></context:component-scan>
</beans>


SpringEL use case

Insert picture description here

Spring EL method

Insert picture description here
If you have any method, you can call it directly.
It can be a system method or a custom method.
Only if there is a return value, and the type of the return value is consistent with your attribute, it’s ok.

package com;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Computer")
public class Computer {
    
    
//系统已有方法
	@Value("#{'abc'.toUpperCase()}")
	private String brand;//品牌
	private Memory memory;//内存
//自定义方法
	@Value("#{MyMathUtil.area(2,3)}")
	private int area;
	//动态赋值,#{}不可少,参数为类.属性
	//因为没有在Memory类上起别名,默认小写开头
	@Value("#{Memory.memCount}")
	private int count;//内存大小
	
	
	
	//------------------------------------------	
	
	public String getBrand() {
    
    
		return brand;
	}
	public void setBrand(String brand) {
    
    
		this.brand = brand;
	}
	public Memory getMemory() {
    
    
		return memory;
	}
	public void setMemory(Memory memory) {
    
    
		this.memory = memory;
	}
	public int getCount() {
    
    
		return count;
	}
	public void setCount(int count) {
    
    
		this.count = count;
	}//内存大小

	public void show() {
    
    
		System.out.println(brand);
		System.out.println(area);
		System.out.println("内存的大小为 "+count);
	}
	
}




package com;

import org.springframework.stereotype.Component;

@Component("MyMathUtil")
public class MathUtil {
    
    
	public int area(int l, int w) {
    
    
		return l*w;
	}
}

SpringEL structure

Insert picture description here

package com;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Computer")
public class Computer {
    
    
	//参数的包名不可少
	@Value("#{new com.Memory()}")
	private Memory memory;//内存

}


package com;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Memory")
public class Memory {
    
    
	@Value("2048")
	private int memCount;
	
	public Memory() {
    
    
		System.out.println("这是Memory类打印的信息");
	}

	public int getMemCount() {
    
    
		return memCount;
	}

	public void setMemCount(int memCount) {
    
    
		this.memCount = memCount;
	}
	
}

SpringEl operator

##
Insert picture description here

When checking num2, there may be an error that the attribute cannot be found. That is because num1 is a private attribute and there is no public method. Adding the get method is ok.

Insert picture description here

Arithmetic> Relations> Logic> Assignment

Spring El set

Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here

**Bold style
**

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/115265649