Spring学习小结——IOC(控制反转,Inversion of Control)

IOC(控制反转,Inversion of Control),一种思想,并非特定的技术,本文以spring中的IOC为例

基本介绍:

1,类的创建不再主动使用new来创建,而是使用配置文件来创建

2,IOC操作的两中方式:配置文件的方式,注解的方式

IOC的基本原理:

1,xml配置文件

2,dom4j解析xml文件

3,工厂的设计模式

4,反射

 

案例:

 

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

    <!-- bean definitions here -->
    <!--ioc base knowledge,new bean 3 ways-->
    <!--1,use default no param constructor method-->
    <bean id="user" class="com.sohu.zzy.spring.ioc.User"></bean>
    <!--2.use bean factory new bean-->
    <bean id="bean2" class="com.sohu.zzy.spring.bean.Bean2Factory" factory-method="getBean2"></bean>
    <!--3,use instance factory new bean-->
    <bean id="bean3factory" class="com.sohu.zzy.spring.bean.Bean3Factory"></bean>
    <bean id="bean3" factory-bean="bean3factory" factory-method="getBean3"></bean>

    <!--使用有参构造注入属性值-->
    <bean id="student1" class="com.sohu.zzy.spring.ioc.Student">
        <constructor-arg name="name" value="zzy"></constructor-arg>
        <constructor-arg name="age" value="23"></constructor-arg>
    </bean>

    <!--使用set方法进行属性注入-->
    <bean id="student2" class="com.sohu.zzy.spring.ioc.Student">
        <property name="name" value="asdf"></property>
    </bean>

    <!--将对象作为属性注入另一个对象-->
    <bean id="studentdao" class="com.sohu.zzy.spring.ioc.studentDao"></bean>
    <bean id="studentservice" class="com.sohu.zzy.spring.ioc.studentService">
        <!--注入dao对象,此时不用value,而是用ref,name 为属性名称-->
        <property name="studentDao" ref="studentdao"></property>
    </bean>

    <!--p名称空间-->
    <bean id="studentp" class="com.sohu.zzy.spring.ioc.Student" p:name="asdfa"></bean>
    <bean id="studentp2" class="com.sohu.zzy.spring.ioc.Student" >
        <!--数组-->
        <property name="args">
            <list>
                <value>array1</value>
                <value>array2</value>
                <value>array3</value>
            </list>
        </property>
        <!--list-->
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
            </list>
        </property>
        <!--map-->
        <property name="map">
            <map>
                <entry key="a" value="asdf"></entry>
                <entry key="b" value="asdf"></entry>
                <entry key="c" value="asdf"></entry>
            </map>
        </property>
        <!--properties-->
        <property name="properties">
            <props>
                <prop key="driverclass">com.mysql.jdbc.Driver</prop>
                <prop key="username">asdfasdf</prop>
                <prop key="password">123asdf</prop>
            </props>
        </property>



    </bean>


</beans>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值