弘创空中充值,踏仙升魔,遥感卫星
原文链接:http://www.yiidian.com/spring/spring-el-helloworld.html
在spring3中就已经支持el表达式了, spring expression language(spel)是类似于ognl和jsf el的表达式语言, 能够在运行时构建复杂表达式, 存取对象属性、调用对象方法等, 而且所有的spel都支持xml和annotation两种方式, 使用的格式均为:#{spel expression}。
下面的例子,这个例子将展示如何利用spel注入string、bean到属性中。
customer.java package com.yiidian.domain; import java.io.serializable; /** * * @author http://www.yiidian.com * */ public class customer implements serializable{ private string name; private string telephone; public string getname() { return name; } public void setname(string name) { this.name = name; } public string gettelephone() { return telephone; } public void settelephone(string telephone) { this.telephone = telephone; } }
customerdao接口:
/** * * @author http://www.yiidian.com * */ public interface customerdao { }
customerdaoimpl类:
注意:在这个类中注入customer对象和custname的字符串。
package com.yiidian.dao.impl; import com.yiidian.dao.customerdao; import com.yiidian.domain.customer; /** * @author http://www.yiidian.com * */ public class customerdaoimpl implements customerdao { private customer customer; private string custname; public void setcustomer(customer customer) { this.customer = customer; } public void setcustname(string custname) { this.custname = custname; } @override public string tostring() { return "customerdaoimpl [customer=" + customer + ", custname=" + custname + "]"; } }
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="customer" class="com.yiidian.domain.customer"> <property name="name" value="张三"/> <property name="telephone" value="13666666666"/> </bean> <bean id="customerdao" class="com.yiidian.dao.impl.customerdaoimpl"> <!-- #{customer}:注入customer对象 #{customer.name}: 注入cutomer的name属性值 --> <property name="customer" value="#{customer}"></property> <property name="custname" value="#{customer.name}"></property> </bean> </beans>
package com.yiidian.test; import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.yiidian.dao.customerdao; /** * @author http://www.yiidian.com * */ public class demo1 { @test public void test1() { applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml"); customerdao customerdao = (customerdao)context.getbean("customerdao"); system.out.println(customerdao); } }
源码下载:http://pan.baidu.com/s/1i4hlqzb
欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。
如果您对我的系列教程感兴趣,也可以关注我的网站:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论