Bean的装配方式

  • 基于注解装配
    • 常用注解:
    • 总结
  • 自动装配

【序言】

在前面的例子中我们就可以看到了使用基于XML方式的缺点了:如果应用中有较多的Bean对象,则会导致xml文件过于臃肿,给后续的维护和升级工作造成困难。为此Spring提供了基于注解的方式。这也是我们最常使用的方式

基于注解装配

常用注解:

  1. @Component:可以使用次数接描述Spring中的Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),并且可以作用于任何层次。使用时只需将该注解标注在相应的类上即可。
  2. @Controller、@Service、@Repository,这三个注解是@Component注解的细化,分别用于标注在Controller(控制层)、Service层(业务层)、Dao层(数据访问层),作用与@Component相同。
  3. @AutoWired:用于对Bean的属性变量、属性的setter方法及构造方法进行标注,配合对应的注解处理器完成Ben啊的自动装配工作,默认Bean类型进行装配。
  4. @Resource:@AutoWired的加强版。默认按Bean实例名称装配。有name和type两个属性,可以通过type属性指定按Bean类型进行装配,此时就等同与@AutoWired。如果都没有指定,则先按实力名称装配,不能匹配在按照Bean类型装配,都无法匹配时,抛出NoSuchBeanDefinitionException异常
  5. @Qualifier :与@AutoWired注解配合使用,会将@AutoWired默认的按Bean类型(byType)装配修改为按Bean实例名(byName)装配,Bean的实例名称由@Qualifier注解参数指定。

UserDao 接口

package com.johnny.annotation.dao;public interface UserDao {public void save();
}
package com.johnny.annotation.dao.imp;import org.springframework.stereotype.Repository;import com.johnny.annotation.dao.UserDao;/*** *在Dao层使用@Repository注解 相当于在xml中编写:<bean id = "userDao" class ="com.johnny.annotation.dao.imp.UserDaoImpl">*/
@Repository("userDao")
public class UserDaoImpl implements UserDao {@Overridepublic void save() {System.out.println("UserDao save!");}}


UserService.java

package com.johnny.annotation.service;public interface UserService {public void save();}

UserServiceImpl

package com.johnny.annotation.service.imp;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.johnny.annotation.dao.UserDao;
import com.johnny.annotation.service.UserService;@Service("userService")
public class UserServiceImp implements UserService {/*** 使用@Resource 注解 且指定name = userDao* 相当于在配置文件 <property name="userDao" ref="userDao"/>*/@Resource(name = "userDao")private UserDao userDao;@Overridepublic void save() {System.out.println("Userservice save!");userDao.save();}}

UserController.java

package com.johnny.annotation.controller;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import com.johnny.annotation.service.UserService;@Controller("userController")
public class UserController {@Resource(name="userService")private UserService userService;public void save() {System.out.println("UserControler save!");userService.save();}}

bean-524.xml

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd" ><!-- 开启注解 --><context:annotation-config /><!-- 声明3个Bean对应的3个实例  与XML装配不同的是 使用注解装配 不用使用ref   --><bean id="userDao" class="com.johnny.annotation.dao.imp.UserDaoImpl" /><bean id="userService" class="com.johnny.annotation.service.imp.UserServiceImp" /><bean id="userController" class="com.johnny.annotation.controller.UserController" /></beans>

在元素中增加了第4行,引入context命名空间,第7、8行增加对context的约束信息;然后通过配置
context:annotation-config/来开启注解 处理器。最后分别定义3个Bean对应编写的实例。与XML装配不同的是,这里 不需要配置子元素。

上述配置文件中的注解方式虽然较大程度地简化了XML文件中Bean的配置,但仍需要在Spring配置文件中一 一配置相应的Bean,为此Spring注解提供了另一种高效的注解配置方式(对包路径下所有的Bean文件进行扫描)。配置方式如下:

<!-- 使用context命名空间 开启注解扫描器: 这样则无需在Spring配置文件中一一配置相应的Bean -->
<context:component-scan base-package = "com.johnny.annotation"/>

可用上述代码将bean524.xml文件中的9-14行代码换掉。

测试类MyTest.java

package com.johnny.annotation.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.johnny.annotation.controller.UserController;
import com.johnny.annotation.dao.UserDao;
import com.johnny.annotation.entity.User;public class Mytest {public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/johnny/annotation/beans/xml/bean-524.xml");UserController userController= (UserController) applicationContext.getBean("userController");userController.save();}}

通过Spring容器加载文件并获取UserController实例,最后嗲用save()方法,输出结果

UserControler save!
Userservice save!
UserDao save!

总结

使用注解定义bean,通过注解的形式将bean以及相应的属性值放入IOC容器中。
<context:component-scan base-package = “com.johnny.annotation”/> Spring在启动的时候,会根据base-package在 该包中扫描所有类,查找这些类是否有注解@Component等注解。如果有,则将该类 加入Spring IOC容器。

步骤:

  1. 在指定类增加注解
  2. 配置文件中添加注解扫描器,指定要扫描的包

自动装配

所谓自动装配,就是将一个Bean自动地注入到其他Bean的Property中。我们可以通过设置元素的autowire的属性值来实现自动装配Bean。

autowire属性有5个值,如下:

  1. 修改UserServiceImpl.java和UserController.java,分别增加类属性的setter方法。
package com.johnny.annotation.service.imp;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.johnny.annotation.dao.UserDao;
import com.johnny.annotation.service.UserService;@Service("userService")
public class UserServiceImp implements UserService {/*** 使用@Resource 注解 且指定name = userDao* 相当于在配置文件 <property name="userDao" ref="userDao"/>*///基于单个注解方式:@Resource(name = "userDao")private UserDao userDao2;  //属性名userDao2public void setUserDao2(UserDao userDao2) {this.userDao2 = userDao2;}@Overridepublic void save() {System.out.println("Userservice save!");userDao2.save();}}

UserController.java

package com.johnny.annotation.controller;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import com.johnny.annotation.dao.UserDao;
import com.johnny.annotation.service.UserService;@Controller("userController")
public class UserController {//基于单个注解方式:@Resource(name="userService")private UserService userService22;public void setUserService22(UserService userService) {this.userService22 = userService;}public void save() {System.out.println("UserControler save!");userService22.save();}}
  1. 修改配置文件为自动装配
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd" ><!-- 使用自动装配  --><bean id="userDao2" class="com.johnny.annotation.dao.imp.UserDaoImpl"  /><bean id="userService22" class="com.johnny.annotation.service.imp.UserServiceImp"  autowire ="byName"/><bean id="userController" class="com.johnny.annotation.controller.UserController" autowire ="byName"/></beans>

在默认情况下,配置文件需要通过ref来装配Bean,但设置了autowire="buName"之后,Spring会自动寻找userService Bean中的属性,并将其属性名称(UserService类属性名 UserDao userDao2)== 配置文件中Bean id(id =userDao2的Bean)进行匹配。由于UserServiceImpl 中定义了userDao2属性及其setter方法,所以SP日国内会自动地将id为userDao2的Bean装配到id为UserService22的Bean中。
同理UserController的配置也是如此。

byType:会自动寻找其他配置文件中是否有其他Bean的类型(class)与该 Bean 的ref属性的类型一致。注意:此种方式的必须满足当前IOC容器中,只有一个Bean满足条件。
constructor:会自动寻找其他配置文件中是否有其他Bean的类型(class)与该 Bean 的构造方法参数的类型一致,本质是通过byType。
可以在头文件中,一次性将该IOC容器的所有bean同一设置成自动装配:

<beans xmlns=“http://www.springframework.org/schema/beans” ……default-autowire=“byName”>

自动装配的原理基于: 约定优于配置

spring框架学习(一):Bean的装配方式 ——基于注解的装配、自动装配相关推荐

  1. Spring(三)——HelloSpring、IOC创建对象的方式、属性注入、自动装配、使用注解开发

    文章目录 1. 简介 2. IOC理论推导 3. HelloSpring 4. IOC创建对象的方式 4.1 使用无参构造创建对象(默认) 4.2 使用有参构造创建对象 5. Spring配置 5.1 ...

  2. 【Spring框架】——5.Bean的作用域及自动装配

    目 录 1. Bean 的作用域 1.1 单例模式 1.2 原型模式 2. Bean 的自动装配 2.1 基于 xml 形式的自动装配 2.1.1 ByName 自动装配 2.1.2 ByType 自 ...

  3. Spring框架学习笔记(三)(AOP,事务管理)

    Spring框架学习笔记(三) 九.AOP 9.1 AOP的注解配置 (1) 新建计算器核心功能(模拟:不能在改动核心代码) (2) 建立一个普通的Java类写增强代码(面向切面编程),使用Sprin ...

  4. Spring框架学习(二)

    Spring框架学习笔记---->AOP AOP概念 AOP:aspect oriented programing,面向切面编程. 将软件的各个模块,按照横向的角度进行观察,发现各个模块之间存在 ...

  5. Spring框架学习笔记,超详细!!(4)

    Java小白开始学习Spring框架,一方面,跟着视频学习,并记录下学习笔记,方便以后复习回顾.另一方面,发布学习笔记来约束自己,学习路程还很遥远,继续加油坚持!!!希望能帮助到大家! 另外还有我的牛 ...

  6. spring框架xml的几种配置方式

    spring框架xml的几种配置方式 ioc配置一般由一下两大类 1 手动装配 利用xml手动配置,分为:setter方法装配 ,构造器装配 2 自动装配 利用注解自动装配 准备工作:userdao ...

  7. Spring框架学习笔记(1) ---[spring框架概念 , 初步上手使用Spring , 控制反转 依赖注入初步理解 ]

    spring官网 -->spring官网 spring5.3.12–>spring-framework 在线文档 --> Spring 5.3.12 文章目录 1.Spring概论 ...

  8. Spring 框架学习 有用

    1.1.1 spring的优势 方便解耦,简化开发 通过Spring提供的IoC容器,可以将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合.用户也不必再为单例模式类.属性文件 ...

  9. Spring框架中的Bean

    1.什么是Bean? 在Spring框架中,Bean是指一个由Spring容器管理的对象.这个对象可以是任何一个Java类的实例,例如数据库连接.业务逻辑类.控制器等等.Bean实例的创建和管理是由S ...

最新文章

  1. ASP.NET MVC扩展库
  2. 这8个专业对“数学”要求很高,考生不要误选!
  3. Java向数据库中插入Boolean类型的字段
  4. 零基础学HTML5和CSS3前端开发第一课
  5. kubernetes-dashboard(1.8.3)部署与踩坑
  6. Android 创建文件,删除文件,加载本地txt文件,string转txt文件,创建文件夹,读取文件夹,open failed: ENOENT
  7. php放量文档,成交量放量过顶买入法(图解)
  8. 学习笔记之sed用法
  9. 踏踏实实学javascript--javascript中的变量
  10. NOI题库--砝码称重V2(多重背包2^n拆分)
  11. Spring之IOC容器
  12. 生成服从正态分布的随机数
  13. Linux 内核版本号查看
  14. 企业级飞速低代码开发平台 | 产品介绍 | APass平台 | 全场景适用
  15. 2019/7/31随笔
  16. 百度年龄计算机在线使用,百度精准年龄计算器在线计算app
  17. waf 防火墙限制_WAF防火墙设备指标及参数说明
  18. 如何用键盘打开设备管理器里计算机的属性,技巧:在Windows10系统中使用键盘打开设备管理器的三种方法...
  19. Syslog-ng3.5 mysql 日志服务器
  20. Android获取WIFI信号强度

热门文章

  1. 构建嵌入式系统软件开发环境-VMware的安装及搭建嵌入式Linux开发环境
  2. 微信小程序-底部导航栏
  3. Linux网络编程之PHP聊天室Workerman-chat
  4. ASP连接Excel的方法
  5. Python高效办公|批量经纬度转地址
  6. C语言实现【小游戏——反弹球消砖块】
  7. 通过硬盘安装linux的方法
  8. 怎样把电脑上的歌曲格式转换成MP3格式
  9. mybatis通过ResultSets返回自定义结果集
  10. 东方通中间件弱密码漏洞检测方法