Spring @autowire用法
在传统的spring注入方式中,我们对类变量都要求实现get与set的方法。在pring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。不过在引及@Autowired注释后,要在spring的配置文件 applicationContext.xml中加入:如下代码,
- <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
使用@Autowired的,那么在xml文件中,也不需要配置该类中的成员变量了。如下代码是没有使用@Autowired的注解
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="boss" class="com.baobaotao.Boss">
- <property name="car" ref="car"/>
- <property name="office" ref="office" />
- </bean>
- <bean id="office" class="com.baobaotao.Office">
- <property name="officeNo" value="002"/>
- </bean>
- <bean id="car" class="com.baobaotao.Car" scope="singleton">
- <property name="brand" value=" 红旗 CA72"/>
- <property name="price" value="2000"/>
- </bean>
- </beans>
在上面的代码中boss中要注入两个类成员变量
如果是在代码中在car与office头上写@Autowire,那么这时xml中的文件就变成如下
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
- <bean class="org.springframework.beans.factory.annotation.
- AutowiredAnnotationBeanPostProcessor"/>
- <!-- 移除 boss Bean 的属性注入配置的信息 -->
- <bean id="boss" class="com.baobaotao.Boss"/>
- <bean id="office" class="com.baobaotao.Office">
- <property name="officeNo" value="001"/>
- </bean>
- <bean id="car" class="com.baobaotao.Car" scope="singleton">
- <property name="brand" value=" 红旗 CA72"/>
- <property name="price" value="2000"/>
- </bean>
- </beans>
这时Boss中就无需引用类成员变量,@Autowire会根据名称自动的对其注入
Spring @autowire用法相关推荐
- Java Spring IOC用法
Java Spring IOC用法 Spring IoC 转载于:http://www.cnblogs.com/flowwind/p/4772375.html 在前两篇文章中,我们讲了java web ...
- Spring @Autowired 用法
Spring @Autowired 用法 首先看下@Component ```举例 1 :``` ```举例 2 :``` 验证是否调用的是默认构造器 ```如何,在启动的时候执行有参数的构造函数?? ...
- Spring Boot spring.factories 用法及原理
文章目录 1. spring.factories 用法 2. spring.factories 实现原理 3. spring.factories 用于解决什么问题? 3.1 业务场景思考及 start ...
- Spring autowire 自动装配简介
Spring autowire 自动装配简介 注意本文与一般spring 标注@Autowire 无关 如下例子定义了3个类 Dperson Dcar & Daddress 其中Dperson ...
- Spring AOP用法
软件152 杨浩艺 Spring AOP Java web 环境搭建 Java web 项目搭建 Java Spring IOC用法 spring提供了两个核心功能,一个是IoC(控制反转),另外一个 ...
- spring RestTemplate用法详解
spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client 转载于:https ...
- Spring Autowire自动装配(转http://lep1985520.blog.163.com/blog/static/56600480200901441338486/)
Spring Autowire自动装配 技术收藏 2009-01-14 16:13:38 阅读284 评论0 字号:大中小 订阅 Spring Autowire自动装配 在应用中,我们常常使用 ...
- Spring基本用法1——Spring的核心机制:IOC、DI
前言:一直想写一个关于Spring的系列文章,但是构思许久却不知道以什么方式阐述,毕竟要把一个复杂框架说清楚并不是那么容易的,我也只能尽力而为了.Spring系列文章打算以这样的顺序展 ...
- spring @Autowire 的注解默认是按类型注入bean
spring @Autowire 的注解默认是按类型注入bean,比如下面这个类 import javax.sql.DataSource; import org.springframework.bea ...
最新文章
- 我之我见:samba共享
- OS / 5 种 IO 模型
- python进程池调用实例方法_Python 多进程并发操作中进程池Pool的实例
- Idea缺少Version Control 底部菜单
- 2018中国C++大会精彩回顾
- android 按键上浮动画_android – 浮动动作按钮动画
- mysql windows 乱码问题_MySQL:windows中困扰着我们的中文乱码问题
- 【Mac Buf Install Solution】Mac Buf Not Provide support for this old version Solution
- 配置quick-cocos2d-x-develop OpenGL version too old
- android号码查询归属地,号码归属地识别-Android电话应用
- Android 开机时间优化
- 北京市地铁线路及站点数据
- ssm mysql项目实战_SSM 项目实战
- php字符串去掉指定字符串,php怎么去除指定字符串
- python国内股票数据分析方法_Python股票数据分析
- Xilinx FPGA高速串行收发器简介
- 推荐一款非常好用的效率APP
- U盘在电脑上不显示怎么办?
- C语言中宏定义宏编译的使用#if,#ifdef,#ifndef,#elif,#else,#endif,defined使用详解,可以实现和if else一样的效果
- Win11磁盘空间不足怎么清理?Win11清理磁盘空间的方法