- el取bean 对象属性规则
- 去map 根据map key
- 取bean中属性
- 根据get方法,getaaa() getAaa()
- ${xxx.000}可以取到此方法。
- ${xxx.00}报错
package com.ssm.chapter9.el.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.ssm.chapter10.el.pojo")
public class ElConfig {
}
package com.ssm.chapter9.el.main;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import com.ssm.chapter10.el.pojo.ElBean;
import com.ssm.chapter10.el.pojo.Role;
import com.ssm.chapter9.el.config.ElConfig;
public class ElMain {
public static void main(String[] args) {
test1();
test2();
}
private static void test1() {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'hello world'");
String str = (String) exp.getValue();
System.out.println(str);
exp = parser.parseExpression("'hello world'.charAt(0)");
char ch = (Character) exp.getValue();
System.out.println(ch);
exp = parser.parseExpression("'hello world'.bytes");
byte[] bytes = (byte[]) exp.getValue();
System.out.println(bytes);
exp = parser.parseExpression("'hello world'.bytes.length");
int length = (Integer)exp.getValue();
System.out.println(length);
exp = parser.parseExpression("new String('abc')");
String abc = (String)exp.getValue();
System.out.println(abc);
Role role = new Role(1L, "role_name", "note");
exp = parser.parseExpression("note");
String note = (String) exp.getValue(role);
System.out.println(note);
EvaluationContext ctx = new StandardEvaluationContext(role);
parser.parseExpression("note").setValue(ctx, "new_note");
note = parser.parseExpression("note").getValue(ctx, String.class);
System.out.println(note);
String roleName = parser.parseExpression("getRoleName()").getValue(ctx, String.class);
System.out.println(roleName);
List<String> list = new ArrayList<String>();
list.add("value1");
list.add("value2");
ctx.setVariable("list", list);
parser.parseExpression("#list[1]").setValue(ctx, "update_value2");
System.out.println(parser.parseExpression("#list[1]").getValue(ctx));
}
public static void test2() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
ElBean elBean = context.getBean(ElBean.class);
System.out.println(elBean.getId() + "\t" + elBean.getRole().getRoleName() + "\t" + elBean.getNote());
}
}
package com.ssm.chapter9.pojo;
public class JuiceMaker2 {
private String beverageShop = null;
private Source source = null;
public String getBeverageShop() {
return beverageShop;
}
public void setBeverageShop(String beverageShop) {
this.beverageShop = beverageShop;
}
public Source getSource() {
return source;
}
public void setSource(Source source) {
this.source = source;
}
public String makeJuice() {
String juice = "这是一杯由" + beverageShop + "饮品店,提供的" + source.getSize() + source.getSugar() + source.getFruit();
return juice;
}
public void init() {
System.out.println("【" + this.getClass().getSimpleName() + "】自定义初始化方法");
}
public void myDestroy() {
System.out.println("【" + this.getClass().getSimpleName() + "】自定义销毁方法");
}
}
package com.ssm.chapter9.pojo;
public class Role {
private Long id;
private String roleName;
private String note;
public Role() {
}
public Role(String roleName, String note) {
this.roleName = roleName;
this.note = note;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
package com.ssm.chapter9.pojo;
public class Source {
private String fruit;
private String sugar;
private String size;
public String getFruit() {
return fruit;
}
public void setFruit(String fruit) {
this.fruit = fruit;
}
public String getSugar() {
return sugar;
}
public void setSugar(String sugar) {
this.sugar = sugar;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
}
package com.ssm.chapter10.annotation.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class DataSourceCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment env = context.getEnvironment();
return env.containsProperty("jdbc.database.driver")
&& env.containsProperty("jdbc.database.url")
&& env.containsProperty("jdbc.database.username")
&& env.containsProperty("jdbc.database.password");
}
}
package com.ssm.chapter10.annotation.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import com.ssm.chapter10.annotation.condition.DataSourceCondition;
import com.ssm.chapter10.annotation.pojo.Role;
import com.ssm.chapter10.annotation.service.impl.RoleServiceImpl;
import com.ssm.chapter9.pojo.JuiceMaker2;
import com.ssm.chapter9.pojo.Source;
@ComponentScan(basePackageClasses = {
Role.class, RoleServiceImpl.class },
excludeFilters = {