[spring] spring中bean的装配
spring中的bean的自动装配。
一、pojo包中的实体:
//Const.java package com.wa.spring.spEL.pojo; public class Const { private Integer id ; private String name ; } //User.java package com.wa.spring.spEL.pojo; import java.util.List; public class User { private Integer id ; private String name ; private Double salary; private Boolean status ; private List<Son> sons; } //son.java import java.util.Date; public class Son { private Integer id ; private String name ; private Date birth; } //省略getter、setter方法
二.hello接口和实现类:
package com.wa.spring.spEL.dao; public interface Hello { public String say(); public String sayTo(String str); } //实现类: package com.wa.spring.spEL.dao.impl; import com.wa.spring.spEL.dao.Hello; import com.wa.spring.spEL.pojo.User; public class HelloImpl implements Hello { private String name="SpEL" ; private User user ; public String getName() { return name; } public void setName(String name) { this.name = name; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public HelloImpl(){ } public HelloImpl(String name){ this.name=name; } public String say() { return "spring 表达式语言:"+name; } public String sayTo(String str) { return "hello,"+str; } public void greet(){ System.out.println("hello"+user); } }
三、beans.xml:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="hello" class="com.wa.spring.spEL.dao.impl.HelloImpl" scope="prototype"> <!-- 基本的构造器注入 --> <constructor-arg value="什么东西" /> <!-- 基本的属性注入 --> <property name="user" ref="user"></property> </bean> <aop:aspectj-autoproxy proxy-target-class="true"/> <bean id="user" class="com.wa.spring.spEL.pojo.User"> <!-- 引用其他bean的属性 --> <property name="id" value="#{const.id}"></property> <!-- 通过调用toUpperCase方法,如果为空抛出一下异常, org.springframework.expression.spel.SpelEvaluationException: EL1011E:(pos 11): Method call: Attempted to call method toUpperCase() on null context object 将 . 改为 ?.后即可避免这种情况 --> <property name="name" value="#{const.name?.toUpperCase()}"></property> <property name="salary" value="4323.43"></property> <property name="status" value="true"></property> <property name="sons" > <list> <!-- 引用集合属性的值注入到对象中 --> <value type="com.wa.spring.spEL.pojo.Son" >#{list[1]}</value> <ref bean="son2"/> </list> </property> </bean> <!-- 使用p命名空间装配属性 T()运算符可以调用类的方法和常量; 可以进行运算+,-,*,/, or,not ,!,and ,? :,==,>=,<=, --> <bean id="const" class="com.wa.spring.spEL.pojo.Const" p:id="#{T(java.lang.Math).random()+12}"> <property name="name"><null/></property> </bean> <bean id="son1" class="com.wa.spring.spEL.pojo.Son" p:id="1" p:name="我阿凡达" > <property name="birth" value="2010-02-10"></property> </bean> <bean id="son2" class="com.wa.spring.spEL.pojo.Son" p:id="2" p:name="阿凡提"> <property name="birth" value="2010-10-11"></property> </bean> <!-- 日期类型的转换器--> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <bean class="com.wa.spring.spEL.convent.PropertyConvent"> <property name="format" value="yyyy-MM-dd"></property> </bean> </entry> </map> </property> </bean> <!-- util中工具类提供list,map,set等集合属性的操作和正则表达式的支持 --> <util:list id="list"> <bean class="com.wa.spring.spEL.pojo.Son" p:id="2" p:name="son1" p:birth="2001-01-21"> </bean> <bean class="com.wa.spring.spEL.pojo.Son" p:id="3" p:name="son2" p:birth="2010-10-21"> </bean> <bean class="com.wa.spring.spEL.pojo.Son" p:id="4" p:name="son3" p:birth="2090-04-12"> </bean> </util:list> </beans>
四、日期转换类:
package com.wa.spring.spEL.convent; import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; /** * @author Administrator *@Date 2015-02-01 *日期和字符串的转换 */ public class PropertyConvent extends PropertyEditorSupport { private String format="yyyy-MM-dd"; @Override public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat sdf = new SimpleDateFormat(format); try { this.setValue(sdf.parse(text)); } catch (ParseException e) { e.printStackTrace(); } } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } }
五、测试类:
package com.wa.spring.spEL.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wa.spring.spEL.dao.impl.HelloImpl; public class TestBean { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("com/wa/spring/spEL/beans.xml"); HelloImpl helloImpl = (HelloImpl) context.getBean("hello"); System.out.println(helloImpl.say()); helloImpl.greet(); } }
参考信息:
Spring日期类型注入
spring配置 no matching editors or conversion strategy found 异常解决方案

浙公网安备 33010602011771号