Spring注解注入

来源:互联网 发布:字符串大小写转换c语言 编辑:程序博客网 时间:2024/06/14 16:04
/*****************************************************************************************************/ 
9:18 2017/10/19    注解注入
/*****************************************************************************************************/ 
·对于Spring框架,类中的空构造一定要写,不管注入方式是什么。
·通过构造器注入,在配置文件中,使用的是<constructor-arg></constructor-arg>标签。
·注入的三种方式:
     (1)、构造器注入。(需要一个带参的构造)
     (2)、自动注入。
     (3)、set方法注入。(比较常用)
范例:
    <!-- bean标签中 autowire="byName"表示的是自动注入;使用最多的是set方法的注入,使用的是property标签-->
      <bean id = "Action" class = "com.wk.action.Action" scope = "prototype" autowire="byName">
            <!-- 通过构造器注入 ,基本不用-->
            <!-- <constructor-arg ref="userService"></constructor-arg> -->
            <!--<property name="userService" ref = "userService"></property>-->
      </bean>
·当感觉映射文件态麻烦时,引入了注解。
·使用注解的话,先要加上相关的约束。
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"
·打开注解:
       <?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:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       <context:annotation-config/>
      </beans>
·创建对象用@Component注解。
  依赖注入的形式:@Resource。 
·注解默认的形式是单例。
  要设置成多例,是要在类上加@Scope("prototype")//注解默认的形式表示单例。设置成多例是prototype。
原创粉丝点击