zoukankan      html  css  js  c++  java
  • Spring--->配置-DI---依赖注入

    1、别名

    • id:bean的唯一标识符,也就是相当于对象名

    • class:bean 对象的全限定名(包名加类型)

    • name:也是别名,而且更高级

        <alias name="hello" alias="hello2"></alias>

    2、Bean的配置

     <bean id="hello" class="com.xian.pojo.Hello" name="hello3,ffff">
            <constructor-arg index="0" value="Spring,Hello"></constructor-arg>
        </bean>

    3、import 

    合作开发时使用,导入不同人的Bean

    <import resource="beans.xml"/>
    <import resource="beans1.xml"/>
    <import resource="beans2.xml"/>

    4、依赖注入

    依赖:

     set注入

     <bean id="student" class="com.xian.Dao.Student">
            <property name="name" value="xian"/>
            <property name="address" ref="address"/>
            <property name="books">
                <array>
                    <value>java se</value>
                    <value>java ee</value>
                    <value>java web</value>
                    <value>java Spring</value>
                </array>
            </property>
            <property name="hobbys">
                <list>
                    <value>敲代码</value>
                    <value>敲脸盆</value>
                    <value>敲钟</value>
                </list>
            </property>
            <property name="card">
                <map>
                    <entry key="1" value="14141414141"/>
                    <entry key="2" value="22222222222"/>
                </map>
            </property>
            <property name="games">
                <value>LOL</value>
            </property>
            <property name="wife">
                <null></null>
            </property>
            <property name="info">
                <props>
                    <prop key="url">www.baidu.com</prop>
                    <prop key="name">baidu</prop>
                </props>
            </property>
        </bean>

    P命名空间和C命名空间

     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:c="http://www.springframework.org/schema/c"
    
     <bean id="hello" class="com.xian.pojo.Hello" p:str="hello P:命名空间"></bean>
     <bean id="helloc" class="com.xian.pojo.Hello" c:str="hello C:命名空间"></bean>
    
     public  void test() {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Hello hello =  context.getBean("hello",Hello.class);
            System.out.println(hello.toString());
            Hello helloc =  context.getBean("helloc",Hello.class);
            System.out.println(helloc.toString());
        }

    5、bean的作用域

    1.单例模式(Spring默认机制)

      <bean id="hello" class="com.xian.pojo.Hello" p:str="hello P:命名空间" 
      scope="singleton"> </bean>

    2.原型模式:每次Get都会产生一个新对象

    <bean id="helloc" class="com.xian.pojo.Hello" c:str="hello C:命名空间" 
    scope="prototype"></bean>

    其余的request、sessioon、application都只作用于web开发中

  • 相关阅读:
    C#入门(3)
    C#入门(2)
    C#入门(1)
    JNI工程搭建及编译
    Java-NestedClass(Interface).
    ConCurrent in Practice小记 (4)
    Java Annotation 注解
    Android使用ViewPager做轮播
    ConCurrent in Practice小记 (3)
    ConCurrent in Practice小记 (2)
  • 原文地址:https://www.cnblogs.com/springxian/p/13546873.html
Copyright © 2011-2022 走看看