03_Spring Bean的装配模式_基于Annotation配置方式
前言
在Spring中尽管使用XML配置文件可以实现Bean的装配工作,但如果应用中Bean的数量较多,会导致XML配置文件过于臃肿,从而给维护和升级带来一定的困难。从JDK 5开始提供了名为Annotation(注解)的功能,Spring正是利用这一特性,Spring逐步完善对Annotation注解技术的全面支持,使XML配置文件不再臃肿。
常用注解:
(1) @Component注解:是一个泛化的概念,仅仅表示一个组件(Bean),可以作用在任何层次。
(2) @Repository注解:用于将数据访问层(DAO 层)的类标识为Spring的Bean。
(3) @Service注解:通常作用在业务层,但是目前该功能与@Component相同。
(4) @Controller注解:标识表示层组件,但是目前该功能与@Component相同
(5) @Resource注解:作用相当于@Autowired,配置对应的注解处理器完成Bean的自动配置工作。
(6) @Autowired注解:默认按照Bean类型进行装配。@Autowired注解加上@Qualifier注解,可直接指定一个Bean实例名称来进行装配。
(7) @Qualifier注解:与@Autowired注解配合,将默认按Bean类型装配修改为按Bean实例名称进行装配,Bean的实例名称由@Qualifier注解的参数指定。
1、applicationContext.xml配置
1.引入context命名空间 、 2.配置 <context:component-scan base-package="xxx"></context:component-scan>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
    <!-- xml配置方式 -->
     <bean name="addr" class="com.wann.bean.Address">
        <property name="city" value="兰州"></property>
        <property name="street" value="西固西路"></property>
    </bean>
    <!-- 可以通过注解的方式 -->
    <context:component-scan base-package="com.wann"></context:component-scan>
</beans>2.Bean的属性注入
1)普通属性
@Value(value="提醒")
private String info;2)对象属性
@Autowired
@Qualifier("userDao")
private UserDao userDao;
等价于
@Resource(name="userDao")
private UserDao userDao;
代码实现:
@Component("user")
@Scope(scopeName = "singleton")
public class User {
    //  @Value(value = "tom")
    private String name;
    // @Value(value = "19")
    private Integer age;
    private Address address;
    public String getName() {
        return name;
    }
    @Value(value = "tom")
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    @Value(value = "19")
    public void setAge(Integer age) {
        this.age = age;
    }
    public Address getAddress() {
        return address;
    }
   // @Resource(name = "addr")
    @Autowired
    @Qualifier("addr")
    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User{" + "name='" + name + '\'' + ", age=" + age + ", city=" + address.getCity() + ", street=" + address.getStreet() +'}';
    }
}注意:也可以将注解写在set方法
使用注解配置的Bean和<bean>配置的一样,默认作用范围都是singleton
@Scope注解用于指定Bean的作用范围
Sping集成测试
@RunWith(SpringJUnit4ClassRunner.class) //帮我们创建容器
@ContextConfiguration("classpath:applicationContext.xml")//引入配置文件
public class TestJunit {
    @Resource(name="user")
    private User user;
    @Test
    public void test(){
        System.out.println(user);
    }
}多种装配Bean方式比较
| 基于XML配置 | 基于注解配置 | 基于Java类配置 | |
| Bean定义 | <bean id="..." class="..." /> | @Component 衍生类@Repository @Service @Controller | @Configuration标注类,@Bean标注提供Bean方法 | 
| Bean名称 | 通过 id或name指定 | @Component("person") | @Bean("person") | 
| Bean注入 | <property> 或者 通过p命名空间 | @Autowired 按类型注入 @Qualifier按名称注入 | 在方法内部编写注入代码逻辑 | 
| 适合场景 | Bean来自第三方,使用其它命名空间 | Bean的实现类由用户自己开发 | 实例化Bean的逻辑比较复杂 | 
03_Spring Bean的装配模式_基于Annotation配置方式的更多相关文章
- 02_Spring Bean的装配模式_基于XML配置方式
		一.三种实例化Bean的方式 1.使用类构造器实例化(默认无参数) <bean id="bean1" class="com.demo1.Bean1"> ... 
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
		Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ... 
- struts_20_对Action中所有方法、某一个方法进行输入校验(基于XML配置方式实现输入校验)
		第01步:导包 第02步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app ... 
- struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)
		课时22 基于XML配置方式实现对action的所有方法进行校验 使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ... 
- eureka集群基于DNS配置方式
		https://www.cnblogs.com/relinson/p/eureka_ha_use_dns.html 最近在研究spring cloud eureka集群配置的时候碰到问题:多台eu ... 
- 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验
		出处:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.html http://www.blogjava.net/focusJ/arc ... 
- spring与hibernate整合配置基于Annotation注解方式管理实务
		1.配置数据源 数据库连接基本信息存放到properties文件中,因此先加载properties文件 <!-- jdbc连接信息 --> <context:property-pla ... 
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring声明式事务管理(基于Annotation注解方式实现)
		在 Spring 中,除了使用基于 XML 的方式可以实现声明式事务管理以外,还可以通过 Annotation 注解的方式实现声明式事务管理. 使用 Annotation 的方式非常简单,只需要在项目 ... 
- Bean的基于XML配置方式
		基于XML配置 Beans.xml <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns= ... 
随机推荐
- Shiro学习笔记1 —— Hello World
			1.创建一个Maven工程加载Shiro的jar包 <!-- junit --> <dependency> <groupId>junit</groupId&g ... 
- kaggle 实战 (2): CNN 手写数字识别
			文章目录 Tensorflow 官方示例 CNN 提交结果 Tensorflow 官方示例 import tensorflow as tf mnist = tf.keras.datasets.mnis ... 
- MongoDB使用固定集合
			MongoDB中的固定集合:大小是固定的,类似于循环队列,如果没有空间了,最老的文档会被删除以释放空间,新插入的会占据这块空间. 1.固定集合(oplog) oplog是一个典型的固定集合,因为其大小 ... 
- 8.0后广播在AndroidManifest.xml中注册后发送intent接收不到广播
			8.0后广播在AndroidManifest.xml中注册后发送intent是接收不到广播了,看了一下原因,好像是8.0为了管理系统和节约电量特别针对广播和服务发送intent的方式启动做出的改变,也 ... 
- Erlang学习记录:app demo
			目录结构 │ Emakefile │ make.bat │ start.bat ├─config │ config.config │ server.app ├─ebin │ wulin_app.bea ... 
- windwos下的转excel到PDF并预览的工具,有Aspose,Spire,原生Office三种方式
			SchacoPDFViewer 项目链接:https://github.com/tiancai4652/SchacoPDFViewer/tree/master 主要实现了对于Excel文件转换PDF, ... 
- thinkphp 三元运算
			模板可以支持三元运算符,例如: {$status?'正常':'错误'} {$info['status']?$info['msg']:$info['error']} 注意:三元运算符中暂时不支持点语法. ... 
- MFC打开/保存文件对话框:CFileDialog
			MFC打开/保存文件对话框:CFileDialog CFileDialog 文件选择对话框的使用:首先构造一个对象并提供相应的参数,构造函数原型如下: CFileDialog::CFileDial ... 
- Parse-轻松构建移动APP的后台服务
			目前正在开发的产品告一段落,有时间总结下经验,也顺便分享一下我们主要使用的平台-Parse. 什么是Parse? Parse是一群美国人开发的专为移动APP服务的云计算平台,与现有的其他云计算平台相 ... 
- 总结加密、机密jar中的class
			1.加密和解密部署到jboss中间件中的的单个class文件,原理:使用“java源程序加密解决方案(基于Classloader解密) (2014-07-13 11:31)”blog即可实现: imp ... 
