zoukankan      html  css  js  c++  java
  • Spring入门

    控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。

    在Spring中实现控制反转的是IoC容器,其实现方式是依赖注入(Dependency Injection,DI)

     

    实现方式:

    测试方法:如下列

    @Test
        public void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
            User user = context.getBean("user",User.class);
            System.out.println(user);
    }

    applicationContext.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">

    </beans>

    IoC创建对象的方式:包括普通、bean、数组、list、map、set、null、properties

    <bean id="address" class = "com.hu.pojo.Address">
            <property name="address" value="南京"></property>
        </bean>
        <bean id="student" class="com.hu.pojo.Student">
            <!--第一种,普通值注入-->
            <property name="name" value="胡承曦"/>
    
            <!--第二种,bean注入,ref-->
            <property name="address" ref="address"/>
    
            <!--数组注入:ref-->
            <property name="books">
                <array>
                    <value>红楼梦</value>
                    <value>西游记</value>
                    <value>三国演义</value>
                </array>
            </property>
    
            <!--List-->
            <property name="hobbies">
                <list>
                    <value>听歌</value>
                    <value>看电影</value>
                    <value>写代码</value>
                </list>
            </property>
    
            <!--  map      -->
            <property name="card">
                <map>
                    <entry key="身份证" value="320102199312180818"/>
                    <entry key="学生卡" value="1218043112"/>
                </map>
            </property>
    
            <!--   set     -->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>COC</value>
                    <value>BOB</value>
                </set>
            </property>
    
            <!--  null      -->
            <property name="wife" >
                <null/>
            </property>
    
            <!--   properties     -->
            <property name="info">
                <props>
                    <prop key="学号">1218043112</prop>
                    <prop key="余额">12</prop>
                </props>
            </property>
        </bean>

    p注入和c注入需要先加入以下xml约束

           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:c="http://www.springframework.org/schema/c"
    <!--p命名空间注入,可以直接注入属性的值:property-->
        <bean id="user" class="com.kuang.pojo.User" p:name="憨批" p:age="18"/>
    
        <!--c命名空间注入,通过构造器注入:construct-args-->
        <bean id="user2" class="com.kuang.pojo.User" c:age="18" c:name="憨批"/>

    别名-alias

    <!--别名,如果添加了别名,我们也可以使用别名获取到-->
    <alias name="user" alias="userNew"></alias>

    bean配置-bean

    <!--
        id:bean的唯一标识符,相当于我们学的对象名;
        class:bean对象所对应的全限定名:包名+类名;
        name:也是别名,可以同时取多个别名,逗号分割
    -->
    <bean id="userT" class="com.kuang.pojo.UserT" name="user2,u2">
    </bean>

    导入多个xml-import

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

    bean的作用域-scope

    1.singleton 代理模式(Spring默认机制):get到的都是同一个对象!

    <bean id="user2" class="com.kuang.pojo.User" c:age="18" c:name="憨批" scope="singleton"/>

    2.prototype原型模式:每次从容器中get的时候,都会产生一个新的对象!

    <bean id="user2" class="com.kuang.pojo.User" c:age="18" c:name="憨批" scope="prototype"/>

    3.其余的request、session、application、这些个只能在web开发中使用

    Bean自动装配-autowire="ByName""ByType"

    byName:会自动在容器上下文中查找和自己对象set方法后面的值对应的beanid!

    byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!

    byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!必须保证类型全局唯一。

    byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!

  • 相关阅读:
    web中缓存的使用
    .net1.1 Read Byte Array From File
    数据库连接的多种方式(二)
    存储过程和SQL语句比较及存储过程在C#中调用方法(转)
    exec与sp_executesql语法的区别详解(转)
    SQL Server存储过程入门案例详解
    asp.net结合aspnetpager用sql语句分页
    数据库连接的多种方式(一)
    分页存储过程1
    asp.net结合aspnetpager使用SQL2005的存储过程分页(转)
  • 原文地址:https://www.cnblogs.com/huchengxi/p/12364544.html
Copyright © 2011-2022 走看看