springIOC入门

spring最主要的两个技术之一

       IOC(Inversion Of Control) 控制反转

          也叫做DI(Dependency Injection) 依赖注入,是指把创建

对象的权利交给框架,代替普通的实例化对象。                 

     实例化:

             User user = new User();

             user.setName("小罗伯斯");

     实体类:private String name;

                private int age;

     控制反转:

         控制反转有三种方法:

                1.属性

                2.构造器

                3.工厂方法

   (1)使用property属性创建对象

     在spring的配置文件applicationContext.xml里面添加

      <bean id="user" class="com.bean.User">

        <property name="name" value="小罗伯斯"/>

        <property name="age" value="23"/>

        注意这里实体类里要写get和set方法 而且要有无参构造方法

        bean也可以写在外面,用<property>标签里面的ref标签引入, <ref bean="c1"/>

</bean>

        然后创建一个测试类

    @Test

public void test(){

    ApplicatoinContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

    获取类路径配置文件,用一个配置文件上下文接收

     User user = (User)ac.getBean(user);   user是bean的id

      System.out.println(user);

}

(2)使用构造器创建对象

     在spring的配置文件applicationContext.xml里面添加

      <bean id="user" class="com.bean.User">

           <constructor-arg value="小萝卜丝" type="java.lang.String" index="1"></constructor-arg>

            <constructor-arg value="23" type="int"></constructor-arg>

        这里加上<constructor-arg/>标签代表构造器的一个参数,也可以不写type类型

          用index,index下标从0开始

            <constructor-arg value="小萝卜丝" index="0"></constructor-arg>

            <constructor-arg value="23" index="1"></constructor-arg>

</bean>

     使用这种配置文件有一个缺点,就是不能输入特殊字符,比如“” 或者\n

         解决办法:  在里面加上在property标签里面加上<value>标签

  <property name="name">

            <value><![CDATA[""想怎么写就怎么写“”哈哈哈\n\n]]></value>

     </property>

            <![CDATA][]]>是转义字符,里面写什么都可以

(2)工厂方法(依赖)

        工厂方法:bean的属性是对象,  比如user对象里有一个car

   <bean id="user" class="com.beans.User">
        <property name="name" value="小萝卜丝"></property>
        <property name="age" value="23"></property>
        <property name="car">
            <bean class="com.beans.Car">
                <property name="name" value="自行车"></property>
                <property name="money" value="5000"></property>
            </bean>
        </property>

    </bean>

         对象是集合

     <bean id="user" class="com.beans.User">

        <property name="name" value="小萝卜丝"></property>

   <property name="age" value="23"></property>    

        <property name="computers">
            <list>
                <ref bean="c1"/>
                <ref bean="c2"/>
            </list>
        </property>  

    </bean>

        对象是map

     <property name="map">
            <map>
                <entry key="emma" value-ref="con1"></entry>
                <entry key="皇后" value-ref="con2"></entry>
            </map>

     </property>

  配置数据源

        数据源用来连接数据库

    数据源里面有这么个属性  private Properties dataSource;

<bean id="dataSource" class="com.beans.DataSource">
        <property name="dataSource">
            <props>
                <prop key="mysql.root">root</prop>
                <prop key="mysql.password">123456</prop>
                <prop key="mysql.driver">com.mysql.jdbc.Driver</prop>
            </props>
        </property>
    </bean>

   测试:

        public void getDataSource(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            DataSource dataSource = (DataSource)ac.getBean("dataSource");

            DataSource data = (DataSource)ac.getBean("dataSource");

             这里并不复杂,只是遍历一个set集合

            Set<Entry<Object, Object>> entrys= data.getDataSource().entrySet();  //使用entrySet获取Set
            Iterator<Entry<Object,Object>> iterator = entrys.iterator();  //用Set获取迭代器

            Entry<Object,Object> en = null;
            while(iterator.hasNext()){                        //最后遍历
                en = iterator.next();
                System.out.println(en.getKey()+":"+en.getValue());
            }

    }

           打印结果:   
                mysql.password:123456
                mysql.root:root
                mysql.driver:com.mysql.jdbc.Driver

   另外,bean里面的property也可以用p:命名方式来代替

       在配置文件头加上

xmlns:p="http://www.springframework.org/schema/p"

    <bean id="con1" class="com.oracle.beans.Cons"
        p:name="em" p:sex="女" p:hehe="2">

    </bean>

     引用bean的时候可以用        

          <util:list id="util">

        <ref bean="con1"/>

        <ref bean="con2"/>

    </util:list>

    加这句话   xmlns:util="http://www.springframework.org/schema/util"

 这样写就方便了






猜你喜欢

转载自blog.csdn.net/weixin_40258928/article/details/80165601