zoukankan      html  css  js  c++  java
  • spring

    spring:一个开源的框架。
    
    1 创建applicationContext.xml 配置文件
    
    1)配置bean
    
    <bean id="helloworld" class = "com.ljs.spring.beans.HelloWorld" scope="prototype">
        <property name="name" value="ljs"></property>
    </bean>
    
    id: bean的唯一标识
    class: 指定全类名
    property: 通过set方法给指定的属性赋值
    
    2. 创建ioc 容器,并容器中获取bean
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("aplicationContext.xml");
        HelloWorld h1 = (HelloWorld) context.getBean("helloworld");
        
    -------------------
    依赖注入:
    1  set 方法注入
    <bean id="car" class="com.ljs.spring.beans.Car" >
        <property name="name" value="baoma"></property>
        <property name="price" value="10000"></property>
    </bean>
    
    2  构造器注入:提供对应的构造器。
    <bean id="car01" class="com.ljs.spring.beans.Car">
        <constructor-arg value="baoma"></constructor-arg>
        <constructor-arg value="123"></constructor-arg>
    </bean>
    
    3  注入含有对象
    <bean id="person" class="com.ljs.spring.beans.Person">
        <property name="name" value="sb"></property>
        <property name="car" ref="car"></property>
    </bean>
    
    4 注入对象中含有对象列表
    <bean id="personlist" class="com.ljs.spring.beans.PersonList">
        <property name="name" value="shbb"></property>
        <property name="cars" >
            <list>
                <ref bean="car" />
                <ref bean="car01" />
            </list>
        </property>
    </bean>
    
    5. 注入对象中含有字典map
    <bean id="personmap" class="com.ljs.spring.beans.PersonMap">
        <property name="name" value="xiaoting"></property>
        <property name="map" >
            <map>
                <entry key="first" value-ref="car"/>
                <entry key="second" value-ref="car01" /> 
            </map>
        </property>
    </bean>
  • 相关阅读:
    每天一个linux命令:head(15)
    用设计模式来替代if-else
    每天一个linux命令:less(14)
    每天一个linux命令:more(13)
    每天一个linux命令:nl(12)
    CDN是什么鬼
    ajax跨域问题
    PDO和MySQLi区别与选择?
    php 依赖注入 和 控制反转 php设计模式
    理解 PHP 依赖注入
  • 原文地址:https://www.cnblogs.com/lijins/p/10235401.html
Copyright © 2011-2022 走看看