zoukankan      html  css  js  c++  java
  • spring的IOC(Inversion of Control)控制反转

    1.spring依赖注入的方式

    a)      通过set方法(property标签)完成依赖注入

                  <!--

                  通过set方法完成注入

                  id:被外面调用时用的标识

                  class:spring管理的类的全类名

                  property:类中的属性

                  name:属性名称

                  ref:属性是个对象用ref注入

                  value:属性是String、基本类型以及基本类型的包装类注入时用

            -->

           <bean id="userController" class="com.zhiyou100.kfs.controller.UserController">

                  <property name="userService" ref="userService"></property>

                  <property name="name" value="zs"></property>

                  <!—bean:表示对象 -->

                  <bean id=”testService” class=”com.test.TestService”/>

                  <!-- list:表示集合 -->

                  <property name="list">

                         <list>

                                <value>张三</value>

                                <value>张四</value>

                         </list>

                  </property>

                  <!--

                         map:表示Map类型

                         entry:

                   -->

                  <property name="map">

                         <map>

                                <entry key="1" value="张三"></entry>

                                <entry key="2" value="张四"></entry>

                         </map>

                  </property>

           </bean>

           <bean id="userService" class="com.zhiyou100.kfs.service.UserServiceImp"></bean>

    b)      通过构造方法(constructor-arg标签)完成依赖注入

           <bean id="hello2" class="com.zhiyou100.kfs.bean.Hellow">

                  <!--

                         2.constructor-arg:用构造方法为类注入属性值

                         index:第几个参数 索引从0开始

                         name:通过构造方法的参数名为类注入属性值

                   -->

                  <constructor-arg index="0" value="张四"/>

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

           </bean>

    2.spring依赖注入的类型

    a)      基本数据类型

    b)      对象

    c)      List类型,Array类型

    d)      Map类型

    3.bean的作用域

           <!--

                  scope:bean的作用域,默认为singleton,prototype:原生对象(非单例)

            -->

           <bean id="hello" class="com.zhiyou100.kfs.bean.Hellow" scope="prototype"/>

    4.自动注入的方式

           <!-- autowire:自动注入的属性

                  byType:根据userDao属性的类型找与之匹配的 bean

                  byName:根据属性姓名找与之匹配的bean的id,id和属性名一致

            -->

           <bean id="uService" class="com.zhiyou100.kfs.bean.UserService" autowire="byName"></bean>

    5.在spring配置文件中引入属性文件

    <!-- spring引入属性文件 ,spring2.5之后

                  若想引入多个属性文件可以用通配符:*或用多个classpath:

           -->

           <context:property-placeholder location="classpath:*.properties"/>

          

           <!--spring2.5之前

            <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

                  <property name="location" value="classpath:my.properties"></property>

           </bean> -->

    6.使用注解的方式

                  1.jar包(多加个AOPjar包)

                  2.配置包扫描

                 

           <!-- 包扫描 -->

           <context:component-scan base-package="com.zhiyou100.kfs"/>

                  3.在相应的类上加上注解

                         @Repository(value="userDao"):持久化注解

                         @Service(value="userService"):业务层注解

                         @Controller(value="userController"):控制层注解

                         @Autowired:自动按照类型注入,若有多个类型相同的那么会再按照名称注入

                         @Resource(name="") 自动注入,先按照名称注入,若没有相同名称那么会按照类型注入。它可以指定名称来注入

  • 相关阅读:
    Express 框架中 使用ejs
    Nodejs操作MongoDB数据库
    MongoDB基础操作
    node中的包、npm和模块
    background
    animation
    transition
    transform
    【SpringCloud】各种组件的更新情况
    【SpringCloud】版本选择
  • 原文地址:https://www.cnblogs.com/kfsrex/p/11494610.html
Copyright © 2011-2022 走看看