SSM系类代码:org.springframework.context.annotation.Bean

  • 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);
		//通过EL访问普通方法
		exp = parser.parseExpression("'hello world'.charAt(0)");
		char ch = (Character) exp.getValue();
		System.out.println(ch);
		//通过EL访问的getter方法
		exp = parser.parseExpression("'hello world'.bytes");
		byte[] bytes = (byte[]) exp.getValue();
		System.out.println(bytes);
		//通过EL访问属性,相当于"hello world".getBytes().length
		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");
		//相当于从role中获取备注信息
		String note = (String) exp.getValue(role);
		System.out.println(note);

		//变量环境类,并且将角色对象role作为其根节点
		EvaluationContext ctx = new StandardEvaluationContext(role);
		//变量环境类操作根节点
		parser.parseExpression("note").setValue(ctx, "new_note");
		//获取备注,这里的String.class指明,我们希望返回的是一个字符串
		note = parser.parseExpression("note").getValue(ctx, String.class);
		System.out.println(note);
		//调用getRoleName方法
		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 = {
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值