Spring 依赖注入, P命名空间, SpEL表达式超入门案例

通过一个spring的配置文件来展示spring的依赖注入方式和写法, 包括P命名空间, 和常用的Spring EL表达式.


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    <bean id="user" class="com.itheima.domain.User">
        <constructor-arg name="userName" value="张三">constructor-arg>
        <constructor-arg name="userId" value="186658056">constructor-arg>
        <constructor-arg name="password" value="123">constructor-arg>
    bean>
    <bean id="iUserDao" class="com.itheima.dao.impl.UserDaoImpl">
        <constructor-arg name="id" value="001"/>
        <constructor-arg name="user" ref="user"/>
    bean>

    
    <bean id="user01" class="com.itheima.domain.User">
        <property name="userName" value="zhangsan">property>
        <property name="userId" value="123123">property>
        <property name="password" value="4352">property>
    bean>
    <bean id="iUserDao01" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="id" value="002"/>
        <property name="user" ref="user01">property>
    bean>


    
    <bean id="collection" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="id" value="002">property>
        <property name="user" ref="user01">property>
        <property name="list">
            <list>
                
                <ref bean="user01">ref>
                <ref bean="user02">ref>
            list>
        property>
        <property name="map">
            <map>
                <entry key="001" value-ref="user01">entry>
                <entry key="002" value-ref="user02">entry>
            map>
        property>
    bean>


    
    <bean id="user02" class="com.itheima.domain.User" p:userName="wangwu" p:userId="3453" p:password="213">bean>
    <bean id="iUserDao02" class="com.itheima.dao.impl.UserDaoImpl" p:id="002" p:user-ref="user02">bean>

    
    
    <bean id="iUserDao03" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="id" value="002"/>
        <property name="user" value="#{user02}">property>
    bean>
    
    <bean id="iUserDao04" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="id" value="#{user02.userId}"/>
        <property name="user" value="#{user02}">property>
    bean>
    
    <bean id="iUserDao05" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="id" value="#{user02.action().toLowerCase()}"/>
        <property name="user" value="#{user02}">property>
    bean>
    
    <bean id="iUserDao06" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="id" value="#{T(java.lang.Math).random()}"/>
        <property name="user" value="#{user02}">property>
    bean>
    
    
beans>

你可能感兴趣的:(J2EE框架)