zoukankan      html  css  js  c++  java
  • Spring-初识Spring框架-IOC控制反转(DI依赖注入)

    ---恢复内容开始---


    IOC :控制反转 (DI:依赖注入)
    使用ioc模式开发 实体类必须有无参构造方法
    1.搭建Spring环境
    下载jar
    http://maven.springframework.org/release/org/springframework/spring/
    spring-framework-4.3.9.RELEASE-dist.zip
    开发spring至少需要使用的jar(5个+1个):
    spring-aop.jar        开发AOP特性时需要的JAR
    spring-beans.jar    处理Bean的jar            <bean>
    spring-context.jar    处理spring上下文的jar        <context>
    spring-core.jar        spring核心jar
    spring-expression.jar    spring表达式
    三方提供的日志jar
    commons-logging.jar    日志

    2.编写配置文件 

    为了编写时有一些提示、自动生成一些配置信息:
    方式一:增加sts插件
    可以给eclipse增加 支持spring的插件:spring tool suite(https://spring.io/tools/sts/all)
    下载springsource-tool-suite-3.9.4.RELEASE-e4.7.3a-updatesite.zip,然后在Eclipse中安装:Help-Install new SoftWare.. - Add

    方式二:
        直接下载sts工具(相当于一个集合了Spring tool suite的Eclipse): https://spring.io/tools/sts/

    新建:bean configuration .. - applicationContext.xml


    3.开发Spring程序(IOC)

            ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
            //执行从springIOC容器中获取一个 id为student的对象
            Student student = (Student)conext.getBean("student") ;
    可以发现,springioc容器 帮我们new了对象,并且给对象赋了值



    SpringIOC发展史:
    1.
    Student student = new Student();
    student.setXxx();

    2.
    简单工厂

    3.ioc (超级工厂)



    IOC(控制反转)也可以称之为DI(依赖注入):
    控制反转:将 创建对象、属性值 的方式 进行了翻转,从new、setXxx()  翻转为了 从springIOC容器getBean()
    依赖注入:将属性值 注入给了属性,将属性 注入给了bean,将bean注入给了ioc容器;

        
        
    总结:ioc/di ,无论要什么对象,都可以直接去springioc容器中获取,而不需要自己操作(newsetXxx())

    因此之后的ioc分为2步:1 先给springioc中存放对象并赋值   2 拿


    DI:依赖注入 ,
    Teacher  

    Course  : cname  teacher

    IOC容器赋值:如果是简单类型(8个基本+String),value;
        如果是对象类型,ref="需要引用的id值",因此实现了 对象与对象之间的依赖关系


    conext.getBean(需要获取的bean的id值)



    依赖注入3种方式:
    1.set注入:通过setXxx()赋值

    赋值,默认使用的是 set方法();
    依赖注入底层是通过反射实现的。
    <property...>


    2.构造器注入:通过构造方法赋值
     <constructor-arg value="ls" type="String" index="0" name="name"></constructor-arg>
    需要注意:如果  <constructor-arg>的顺序 与构造方法参数的顺序不一致,则需要通过type或者index或name指定。

    3.p命名空间注入
    引入p命名空间
        xmlns:p="http://www.springframework.org/schema/p"

    <bean id="course" class="org.lanqiao.entity.Course" p:courseHour="300" p:courseName="hadoop" p:teacher-ref="teacher">

    简单类型:
        p:属性名="属性值"
    引用类型(除了String外):
        p:属性名-ref="引用的id"
    注意多个 p赋值的时候 要有空格。


    注意:
    无论是String还是Int/short/long,在赋值时都是 value="值" ,
    因此建议 此种情况 需要配合 name ype进行区分



    示例:
        注入各种集合数据类型: List  Set map properties

     1   <!--注入集合-->
     2 
     3         <bean id="all_list" class="org.hxzy.spring.pojo.All_list_demo">
     4                <!--List 赋值操作-->
     5                 <property name="list_demo" >
     6                         <list>
     7                                 <value>zs</value>
     8                                 <value>ls</value>
     9                         </list>
    10                 </property>
    11                 <!--Map 赋值操作-->
    12                 <property name="map_demo">
    13                         <map>
    14                                 <entry>
    15                                       <key>
    16                                               <value>cy</value>
    17                                       </key>
    18                                         <value></value>
    19                                 </entry>
    20                         </map>
    21                 </property>
    22                 <!--数组赋值-->
    23                 <property name="arr_demo">
    24                         <array>
    25                                 <value>小皮球</value>
    26                                 <value>香蕉梨</value>
    27                         </array>
    28                 </property>
    29                 <!--Set集合赋值-->
    30                 <property name="set_demo">
    31                         <set>
    32                                 <value>gubin</value>
    33                                 <value>hnb</value>
    34                         </set>
    35                 </property>
    36         </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">
            <!--配置User 实体类
            <bean id="user" class="com.hxzy.entity.User" scope="prototype"/>-->
            <!--静态工厂 创建
            <bean id="user1" class="com.hxzy.entity.User" factory-method="getUser"/>
            -->
            <!--使用Set方式 注入
            <bean scope="prototype" id="person" class="com.hxzy.entity.Person">
               <property name="p_name" value="古斌"></property>
            </bean>-->
            <!--使用构造方法进行注入
            <bean id="per" scope="prototype" class="com.hxzy.entity.Person">
                    <constructor-arg name="name" value="张全蛋"></constructor-arg>
            </bean>-->
            <!--注入集合-->
    
            <bean id="all_list" class="org.hxzy.spring.pojo.All_list_demo">
                   <!--List 赋值操作-->
                    <property name="list_demo" >
                            <list>
                                    <value>zs</value>
                                    <value>ls</value>
                            </list>
                    </property>
                    <!--Map 赋值操作-->
                    <property name="map_demo">
                            <map>
                                    <entry>
                                          <key>
                                                  <value>cy</value>
                                          </key>
                                            <value></value>
                                    </entry>
                            </map>
                    </property>
                    <!--数组赋值-->
                    <property name="arr_demo">
                            <array>
                                    <value>小皮球</value>
                                    <value>香蕉梨</value>
                            </array>
                    </property>
                    <!--Set集合赋值-->
                    <property name="set_demo">
                            <set>
                                    <value>gubin</value>
                                    <value>hnb</value>
                            </set>
                    </property>
            </bean>
            <!--注入对象-->
            <bean id="usr" class="com.hxzy.dao.Userdao">
            </bean>
            <bean id="Service_" class="com.hxzy.dao.User_Service">
                <property name="userdao" ref="usr"></property>
            </bean>
            <!--使用P命名空间-->
            <bean id="person" class="com.hxzy.entity.Person" p:p_name="古斌" p:p_address="石家庄" p:p_age="18" scope="prototype"></bean>
    
    </beans>





    给对象类型赋值null :
            <property name="name" >  
                    <null/>       -->注意 没有<value>
            </property>
    赋空值 ""  
            <property name="name" >  
                    <value></value>  
            </property>



    在ioc中定义bean的前提:该bean的类 必须提供了 无参构造



    自动装配(只适用于 ref类型 ):
        约定优于配置


    自动装配:
    <bean ... class="org.lanqiao.entity.Course"  autowire="byName|byType|constructor|no" >  byName本质是byId
    byName:  自动寻找:其他bean的id值=该Course类的属性名
    byType:  其他bean的类型(class)  是否与 该Course类的ref属性类型一致  (注意,此种方式 必须满足:当前Ioc容器中 只能有一个Bean满足条件  )
    constructor: 其他bean的类型(class)  是否与 该Course类的构造方法参数 的类型一致;此种方式的本质就是byType


    可以在头文件中 一次性将该ioc容器的所有bean  统一设置成自动装配:
    <beans xmlns="http://www.springframework.org/schema/beans"
    ...
    default-autowire="byName">

    自动装配虽然可以减少代码量,但是会降低程序的可读性,使用时需要谨慎。



    使用注解定义bean:通过注解的形式 将bean以及相应的属性值 放入ioc容器

    <context:component-scan base-package="org.lanqiao.dao">
    </context:component-scan>Spring在启动的时候,会根据base-package在 该包中扫描所有类,查找这些类是否有注解@Component("studentDao"),如果有,则将该类 加入spring Ioc容器。

    @Component细化:

    dao层注解:@Repository
    service层注解:@Service
    控制器层注解:@Controller































    ---恢复内容结束---

  • 相关阅读:
    30道四则运算
    《梦断代码》第0章读书随笔
    《梦断代码》阅读计划
    [记录] Mybatis原理
    GitLab登录密码重置后无法使用idea拉取代码提交代码问题解决
    写邮件和回复邮件
    XMLDocument 方法中实现post发送消息
    愿我温柔如水,坚强如钢!
    第6届蓝桥杯javaA组第7题,牌型种数,一道简单的题带来的思考
    IE兼容性问题解决方案2--css样式兼容标签
  • 原文地址:https://www.cnblogs.com/gu-bin/p/10456647.html
Copyright © 2011-2022 走看看