zoukankan      html  css  js  c++  java
  • Spring框架day02--IOC

    1.为什么学习Spring

    * 方便解耦,简化开发

       * Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理

    * AOP编程的支持

       * Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能

    * 声明式事务的支持

       * 只需要通过配置就可以完成对事务的管理,而无需手动编程 * 方便程序的测试

    * Spring对Junit4支持,可以通过注解方便的测试Spring程序

      * 方便集成各种优秀框架 * Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支持

    * 降低JavaEE API的使用难度

      * Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

    2.什么是IOC(Inverse of Control,控制反转,将对象的创建权反转给Spring!!

       具体的配置如下

         <bean id="userService" class="com.itcast.demo1.UserServiceImpl"/>

      测试程序:采用Spring框架的工厂方式来获取到UserService接口的具体实现类

        

       public void demo2(){
            // 使用Spring的工厂:
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            // 通过工厂获得类:
            UserService userService = (UserService) applicationContext.getBean("userService");
            userService.sayHello();
        }

    3.什么是DI(Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中!!

      1.属性注入:往UserServiceImpl类里注入一个属性为uname它的值为"小风",注意提供这个属性的set方法。如果注入的是引用类型,<property/>标签上的value为ref

     /

        <bean id="us" class="com.itheima.demo1.UserServiceImpl">

             <property name="uname" value="小风"/>

         </bean>

      2.构造方法注入:在Car这个类中提供两个属性,并且提供构造方法,这时候注意要加上默认的无参构造方法:

        

     <bean id="car" class="com.itheima.demo4.Car">
                    <constructor-arg name="name" value="大奔"/>
                    <constructor-arg name="money" value="100"/>
     </bean>

      

    <?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标签 -->
           <bean id="userService" class="com.itheima.demo2.UserServiceImpl">
               <property name="name" value="小凤"/>
           </bean>
           
           <!-- 演示的依赖注入 -->
           <bean id="customerDao" class="com.itheima.demo3.CustomerDaoImpl"/>
           
           <bean id="customerService" class="com.itheima.demo3.CustomerServiceImpl">
               <property name="customerDao" ref="customerDao"/>
           </bean>
           
           <!-- 演示的构造方法的注入的方式 -->
           <bean id="car1" class="com.itheima.demo4.Car1">
               <!-- <constructor-arg name="cname" value="奇瑞QQ"/>
               <constructor-arg name="price" value="25000"/> -->
               
               <constructor-arg index="0" value="囚车"/>
               <constructor-arg index="1" value="545000"/>
           </bean>
           
           <bean id="person" class="com.itheima.demo4.Person">
               <constructor-arg name="pname" value="美美"/>
               <constructor-arg name="car1" ref="car1"/>
           </bean>
           
           <!-- 采用set方法注入
           <bean id="car2" class="com.itheima.demo4.Car2">
               <property name="cname" value="二八自行车"/>
               <property name="price" value="1000"/>
           </bean> -->
           
           <!-- 采用p名称空间注入的方式(了解) 
               <bean id="car2" class="com.itheima.demo4.Car2" p:cname="保时捷" p:price="1000000"/>
        -->
        
        <!-- 使用SPEL方式注入 -->
        <bean id="car2" class="com.itheima.demo4.Car2">
            <property name="cname" value="#{'福特野马'}"/>
            <property name="price" value="#{450000}"/>
        </bean>
        
        <!-- 注入集合 
        <bean id="user" class="com.itheima.demo4.User">
            <property name="arrs">
                <list>
                    <value>哈哈</value>
                    <value>呵呵</value>
                    <value>嘿嘿</value>
                </list>
            </property>
            
            <property name="list">
                <list>
                    <value>美美</value>
                    <value>小凤</value>
                </list>
            </property>
            
            <property name="map">
                <map>
                    <entry key="aaa" value="小苍"/>
                    <entry key="bbb" value="小泽"/>
                </map>
            </property>
            
            <property name="pro">
                <props>
                    <prop key="username">root</prop>
                    <prop key="password">1234</prop>
                </props>
            </property>
        </bean>
        -->
        
        <!-- 引入其他的配置文件 
            <import resource="applicationContext2.xml"/>
        -->
        
    </beans>
  • 相关阅读:
    Cipherlab CPT9300手持扫描枪开发体验 [转]
    引用(ajaxfileupload.js) ajaxfileupload.js报jQuery.handleError is not a function错误解决方法
    C#锐利体验2.0:泛型编程
    Visual C#中调用Windows服务初探
    C#操作XML代码整理
    个人代码库のC#背景色渐变的功能
    ~~ C#数字时钟 ~~
    DevExpress随笔10.1.5的汉化与破解
    用C#获取局域网内所有机器
    C# 图片格式(JPG,BMP,PNG,GIF)等转换为ICO图标
  • 原文地址:https://www.cnblogs.com/133261c/p/9477951.html
Copyright © 2011-2022 走看看