Spring注入依赖的4个注解

  • @Value   注入int、float、String等基本数据类型,只能标注在成员变量、setter方法上。
  • @Autowired    按类型自动装配,可标注在成员变量(官方不推荐)、构造方法、setter方法上。
  • @Qualifier   按名称自动装配,需要和@Autowired搭配使用,只能标注在成员变量(官方不推荐)、setter方法上。
  • @Resource    按名称或类型自动装配,需要第三方包 javax.annotation.jar 的支持,只能标注在成员变量、setter方法上。

以上3个注解用于自动装配其它bean的实例,尽量标注在setter方法上。

复杂类型需要用xml方式注入。


使用spring的注解,需要引入spring-aop.RELEASE.jar。

如果只使用了上面这些依赖注入的注解,需要在xml中启用注解,还需要配置<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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config></context:annotation-config>
<bean name="b" class="com.chy.bean.B" />
</beans>

如果使用了类注解(@Controller、@Service、@Repository、@Component),直接使用包扫描即可,不必配置<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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.chy.bean" />
</beans>

包扫描已经包括了启用注解的功能。


@Value

用于注入基本类型,只能标注在成员变量、setter上,不能标注在构造方法上。

直接标注在成员变量上:(不需要setter方法)

@Component
public class B {
@Value("20")
private int age;
//......
}

spring会自动将引号中的值转换为需要的类型。值必须放在引号中。

标注在setter方法上:

public class B {
private int age; @Value("20")
public void setName(int age) {
this.age = age;
} //.....
}

@Value不能和 参数是该成员变量的构造方法 一同使用。

比如说使用@Value注入了age字段,该类中就不能有A(int  age)这个构造方法。


@Autowired

按类型自动装配,可以标注在成员变量、构造方法、setter方法上,官方不推荐标注在成员变量上。

标注在构造方法上时,可缺省@Autowired,因为使用包扫描时,如果未显式配置依赖注入,默认使用构造方法的自动装配(按参数类型)。


@Qualifier

@Component
public class A {
private B b; @Autowired
@Qualifier("b")
public void setB(B b) {
this.b = b;
} public A(B b) {
this.b = b;
}
}

@Qualifier不能单独用,需要和@Autowired搭配使用。

是按名称的自动装配,需要在@Autowired("name")写上所依赖bean的name。

只能标注在成员变量(官方不推荐)、setter方法上,虽然不能标注在构造方法上,但可以出现对应的构造方法。


@Resource

spring提供了@Resource注解,但并未提供此注解的实现。

@Resource需要第三方包的支持:javax.annotation.jar。

下载地址:http://www.java2s.com/Code/Jar/j/Downloadjavaxannotationapi12sourcesjar.htm

如果使用maven,会自动下载spring依赖的第三方包commons-logging.jar、javax.annotation.jar,无需我们手动下载添加。

@Component
public class A {
// @Resource(name = "b")
@Resource(type = B.class)
private B b; public void setB(B b) {
this.b = b;
} public A(B b) {
this.b = b;
}
}

@Resource可按名称或按类型自动装配,可在()中指定。

name的值是String形式,type的值是class形式。

@Component
public class A {
private B b; @Resource
public void setB(B b) {
this.b = b;
} public A(B b) {
this.b = b;
}
}

未指定规则时,默认先按名称装配,找不到满足要求的bean,再按类型装配。

@Resource只能标注在成员变量、setter方法上,但可以出现对应的构造方法。

Spring Bean的3种装配方式的更多相关文章

  1. spring Bean的三种配置方式

    Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...

  2. spring Bean的三种注入方式

    1.构造函数注入: 构造函数的注入方式分为很多种 (1)普通构造函数,空参数的构造函数 <bean id="exampleBean" class="examples ...

  3. spring bean的三种管理方式·

    1.无参构造函数 1.xml文件配置内容 <!--无参构造函数--> <bean id="bean1" class="com.imooc.ioc.dem ...

  4. Spring MVC 实验2-Bean的几种装配方式及基本用法

    实验二:Bean的几种装配方式及基本用法  实验目的: (1)掌握2种基于XML的装配方式:设值注入(Setter Injection)和构造注入(Constructor Injection) . ( ...

  5. Spring Boot 项目几种启动方式

    Spring Boot 项目几种启动方式 1. 使用 main 启动 jar xxxx.jar 2. 使用 mvn 启动 mvn spring-boot:run 3. 使用 Spring Boot c ...

  6. Spring三 Bean的三种创建方式

    创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...

  7. Spring中bean的四种注入方式

    一.前言   最近在复习Spring的相关内容,这篇博客就来记录一下Spring为bean的属性注入值的四种方式.这篇博客主要讲解在xml文件中,如何为bean的属性注入值,最后也会简单提一下使用注解 ...

  8. Spring bean的bean的三种实例化方式

     Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean ...

  9. Spring IOC以及三种注入方式

    IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...

随机推荐

  1. XBMC 最新版本号错误

    1. Syntax error: "(" unexpected 改动 tools/depends下的makefile.include 将NDK_VER=0x9d

  2. 简明Python3教程 首页

    A Byte of Python 'A Byte of Python' is a free book on programming using the Python language. It serv ...

  3. Linux性能测试 mpstat命令

    mpstat是MultiProcessor Statistics的缩写,是实时系统监控工具.其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有 ...

  4. WPF“天狗食月”效果

    原文:WPF"天狗食月"效果 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/deta ...

  5. mybatis 使用经验小结 good

    一.多数据源问题 主要思路是把dataSource.sqlSesstionFactory(用来产生sqlSession).MapperScannerConfigurer在配置中区分开,各Mapper对 ...

  6. matlab 矢量化编程(三) —— 软阈值函数

    dj,k^=⎧⎩⎨⎪⎪dj,k−λ,dj,k≥λ0,otherwisedj,k+λ,dj,k≤−λ function y = soft(x, T) y = (x - abs(T) > 0) .* ...

  7. 关于VS2015中的code snippet无法使用的问题

    什么是code snippet? Code snippets are small blocks of reusable code that can be inserted in a code file ...

  8. Entity framework 更改模型,新增表

    在Package Manager Console 中运行命令Enable-Migrations 再次运行可以更新 抄袭 在实体类中增加一个属性以后,执行 Update-Database 命令 ,可以更 ...

  9. 【 D3.js 入门系列 --- 6 】 如何使移动图表

    我的个人博客是: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处,谢谢. [5.1]节中制作了一个比較完好的图表.但它是静态的.想做出它的动 ...

  10. Form submit

    方法1:使用form onsubmit标签 return XXX()方法 <!--onsubmit--> <form id="formid" name=" ...