The spring IOC (Inversion of Control) control inversion

1.spring dependent manner injection

a) is completed by a set dependency injection method (property tab)

              <!--

              Complete set by implantation method

              id: with identity is called out

              class: class spring full class name management

              property: class attributes

              name: attribute name

              ref: attribute is implanted with ref

              value: attribute String, primitive type and basic type of packaging injection

        -->

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

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

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

              <- list:! Denote the set ->

              <property name="list">

                     <list>

                            <value>张三</value>

                            <value>张四</value>

                     </list>

              </property>

              <!--

                     map: Map represented type

                     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) by the constructor dependency injection is completed (constructor-arg tag)

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

              <!--

                     2.constructor-arg: injecting property values ​​constructor for a class by

                     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="") 自动注入,先按照名称注入,若没有相同名称那么会按照类型注入。它可以指定名称来注入

Guess you like

Origin www.cnblogs.com/kfsrex/p/11494610.html