Spring Bean的3种装配方式
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种装配方式的更多相关文章
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
- spring Bean的三种注入方式
1.构造函数注入: 构造函数的注入方式分为很多种 (1)普通构造函数,空参数的构造函数 <bean id="exampleBean" class="examples ...
- spring bean的三种管理方式·
1.无参构造函数 1.xml文件配置内容 <!--无参构造函数--> <bean id="bean1" class="com.imooc.ioc.dem ...
- Spring MVC 实验2-Bean的几种装配方式及基本用法
实验二:Bean的几种装配方式及基本用法 实验目的: (1)掌握2种基于XML的装配方式:设值注入(Setter Injection)和构造注入(Constructor Injection) . ( ...
- Spring Boot 项目几种启动方式
Spring Boot 项目几种启动方式 1. 使用 main 启动 jar xxxx.jar 2. 使用 mvn 启动 mvn spring-boot:run 3. 使用 Spring Boot c ...
- Spring三 Bean的三种创建方式
创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...
- Spring中bean的四种注入方式
一.前言 最近在复习Spring的相关内容,这篇博客就来记录一下Spring为bean的属性注入值的四种方式.这篇博客主要讲解在xml文件中,如何为bean的属性注入值,最后也会简单提一下使用注解 ...
- Spring bean的bean的三种实例化方式
Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean ...
- Spring IOC以及三种注入方式
IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...
随机推荐
- 用acharengine作Android图表
首先要下载acharengine的包,里面重要的有lib和一些简易的工具,等下我附在文件夹里,而这些包都必须调用的. 然后以下附上主要的作图代码: package org.achartengine.c ...
- Distinct去除重复项
之前在做权限模块时,因不同角色可能拥有相同的菜单,导致呈现在浏览器上时出现重复菜单项,所以需要在获取用户拥有菜单项时需要过滤重复项, 用到了Distinct,两个重载 public static IQ ...
- WPF 遍历DataTemplate(获取所有控件)
原文:WPF 遍历DataTemplate(获取所有控件) 情况1:在设定DataTemplate的Name,并且他是在前台表示时,获取DataTemplate里的指定控件. 方法: http://b ...
- Swift 中使用 SwiftyJSON 制作一个比特币价格 APP
Swift 中处理 JSON 数据有很多种方式,可以使用原生的 NSJSONSerialization,也可以使用很多第三方库.原生的 NSJSONSerialization 方式这篇文章中介绍过.这 ...
- ajax默认form表单提交,导致实体不识别
出现位置:实体比较复杂,包含List之类的时候 public class AdvertisementType { /// <summary> /// 广告位名称 /// </summ ...
- WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇) 在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...
- DB First .edmx
DB First查看Entity相互关系.edmx 图表 .edmx源代码——xml文件右键,打开方式 xml内容 详细查看DB:.edmx—Model Browser(模型浏 ...
- WPF ListView的使用
<Window x:Class="XamlTest.Window14" xmlns="http://schemas.microsoft.com/win ...
- socket 主机地址相关的函数
#include <arpa/inet.h> int inet_aton (const char *name, struct in_addr *addr) 将ipv4地址从数字点的形式转化 ...
- spring boot的默认配置
# BANNER banner.charset=UTF- # Banner file encoding. banner.location=classpath:banner.txt # Banner f ...