spring初级java 应用。搭建环境。基本语法

搭建环境完成之后。使用spring config editor打开配置的spring xml文件。

自己实现了,spring在java上的一些基本的应用。一共看了四节视频。下面是自己实现的编码:

最基本的就是创建spring的ioc容器对象。从ioc中得到bean的实例对象

对于spring的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:util="http://www.springframework.org/schema/util"
    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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
    <!-- 配置bean文件 -->
    <bean id="helloword" class="lib.Helloword">
       <property name="name" value="spring"></property>
    </bean>
    <bean id="car22" class="lib.Car">
        <constructor-arg value="wen" index="0"></constructor-arg>
        <constructor-arg index="1">
            <value><![CDATA[<ncjsnjkad>]]></value>
        </constructor-arg>
    </bean>
    <bean id="person" class="lib.Person">
        <property name="name" value="Tom"></property>
        <property name="age" value="24"></property>
        <!-- 建立bean之间的关系的ref写法 -->
        <!-- <property name="car" ref="car2"></property>
         
         <property name="car">
             <ref bean="car2"/>
         </property>-->
         <!-- 内部的bean的使用方法,不能为外部的引用 -->
         <property name="car">
           <bean class="lib.Car">
               <constructor-arg value="wen" index="0"></constructor-arg>
               <constructor-arg index="1">
               <value><![CDATA[<ncjsnjkad>]]></value>
        </constructor-arg>
        <!-- 这是null的专属标记
        <constructor-arg><null/></constructor-arg> -->
        <!-- 为级联属性赋值,属性需要初始化才可以在次进行级联赋值
          <property name="person.name" value="399"></property>
         -->
           </bean>
         </property>
    </bean>
    <bean id="car1" class="lib2.Car">
        <constructor-arg value="wen" index="0"></constructor-arg>
        <constructor-arg index="1">
            <value><![CDATA[<ncjsnjkad>]]></value>
        </constructor-arg>
    </bean>
    <bean id="car2" class="lib2.Car">
        <constructor-arg value="wen" index="0"></constructor-arg>
        <constructor-arg index="1">
            <value><![CDATA[<ncjsnjkad>]]></value>
        </constructor-arg>
    </bean>
    <bean id="car3" class="lib2.Car">
        <constructor-arg value="wen" index="0"></constructor-arg>
        <constructor-arg index="1">
            <value><![CDATA[<ncjsnjkad>]]></value>
        </constructor-arg>
    </bean>
    <!-- 配置集合属性 -->
    <bean id="person3" class="lib2.Person">
        <property name="name" value="mike"></property>
        <property name="age" value="24"></property>
        <property name="cars">
            <list>
                <ref bean="car1"/>
                <ref bean="car2"/>
                <ref bean="car3"/>
            </list>
        </property>
    </bean>
    <!-- 配置map属性值 -->
    <bean id="person33" class="lib2.Person3">
        <property name="name" value="mao"></property>
        <property name="age" value="34"></property>
        <property name="cars">
            <map>
                <!-- 使用map的节点和map的子节点entry进行配置 -->
                <entry key="AA" value-ref="car1"></entry>
                <entry key="BB" value-ref="car2"></entry>
            </map>
        </property>
    </bean>
    <bean id="datasourse" class="lib2.Datasourse">
        <property name="properties">
            <!-- 使用props和prop节点来为properties来配置信息 -->
            <props>
                <prop key="us">djkchsk</prop>
                <prop key="ee">hkjcsd</prop>
                <prop key="www">chjdss</prop>
            </props>
        </property>
    </bean>
    <!-- 配置单例的集合bean,以供多个bean进行引用 -->
    <util:list id="cars">
        <ref bean="car1"/>
        <ref bean="car2"/>
        <ref bean="car3"/>
    </util:list>
    <!-- 通过排名明空间为bean的属性赋值、需要先导入p的命名空间,先对于传统的更加简洁 -->
    <bean id="person5" class="lib2.Person" p:age="30" p:name="quens" p:cars-ref="cars"></bean>
</beans>

简单列出其中的主函数:

package lib2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ceshi.newww;

public class spring {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext se3t=new ClassPathXmlApplicationContext("applicationcontext.xml");
        Person person3=(Person) se3t.getBean("person3");
        System.out.println("1  "+person3);
        Person3 person33=(Person3) se3t.getBean("person33");
        System.out.println("2  "+person33);
        Datasourse datasourse=se3t.getBean(Datasourse.class);
        System.out.println("3  "+datasourse);
     }

}
原文地址:https://www.cnblogs.com/dazhi151/p/12577630.html