基于注解的配置

除了采用采用xml来配置bean之外,也可以采用注解的方式来定义,注册,加载bean。

注解的方式在spring中默认时不开启的,所以需要在xml文件中进行配置启用

注解的启动方式有下面两种配置:

下面的配置既可以在所有类中启用注解

<context:annotation-config/>

还可以在部分你需要的类中启用注解,这个可以包含一些类,也可以不去扫描一些类

具体的在另外一篇博客事物管理中有讲到。

<context:component-scan base-package="com.b505.common.service.impl" />

 @Required  

应用于bean的setter方法,当在setter方法上加上这个注释后,表明必须在xml中配置这个属性,不然会抛BeanInitializationException异常

在beans.xml中配置启动注释的两个方式二选一,在本例中,都不配置的话,程序照样不会出错,这是因为这样就不会启用注解配置了

而是xml配置,

    <context:annotation-config/>
<!-- 这两个配置2选1 -->
<!-- <context:component-scan base-package="com.test.Student" /> -->
<bean id="student" class="com.test.Student">
<property name="name" value="zara"></property>
<property name="age" value="11"></property>
</bean>

Student.java实体类

public class Student {
private Integer age;
private String name;
@Required
public void setAge(Integer age) {
System.out.println("**setAge(Integer age) "+age);
this.age = age;
}
@Required
public void setName(String name) {
System.out.println("**setName(String name) "+name);
this.name = name;
}
 @Autowired

setter方法上的@Autowired

在setter方法上加上@Autowored注解后,spring容器会试图以“byType”的方式去连接bean

这中间就涉及到一个问题,若是我们在bean中有多个相同的type的bean,会报错的。

beans.xml

<context:annotation-config/>
<bean id="money1" class="com.test.Money1"></bean>
<bean id="student" class="com.test.Student">
<property name="name" value="zara"></property>
<property name="age" value="11"></property>
</bean>

Student.java

    @Autowired
public void setMoney(Money money) {
System.out.println("**setMoney(Money money) "+money);
this.money = money;
}

 在属性上面应用@Autowired

可以消除java代码中的setter方法,若是定义了setter方法,在注入bean时也不是通过这个setter方法的。

    @Autowired
private Money money;

在构造函数上使用@Autowired

    <context:annotation-config/>
<bean id="student" class="com.test.Student">
</bean>
<bean id="money2" class="com.test.Money2"></bean>

Student.java

    @Autowired
public Student(Money money){
System.out.println("***Student(Money money)");
this.money=money;
}
@qualifiler注释

beans.xml

在beans.xml中有两个类型相同的bean,money1和money2,当你需要注入时,就又能会不知道注入哪一个而抛异常,这时就用到@qualifiler注解了

    <context:annotation-config/>
<bean id="student" class="com.test.Student">
</bean>
<bean id="money1" class="com.test.Money1"></bean>
<bean id="money2" class="com.test.Money2"></bean>

Studen.java

这样指定了注入的bean为money2(id号)

    @Autowired
@Qualifier("money2")
public void setMoney(Money money) {
System.out.println("**setMoney(Money money) "+money);
this.money = money;
}
 spring Jsr-250注释

@Resource

他类似于遵循“by-name”的方式,找到id=“money2”的bean进行注入。

Student.java

    @Resource(name="money2")
public void setMoney(Money money) {
System.out.println("**setMoney(Money money) "+money);
this.money = money;
}

参考文章:w3cschool教程

spring-基于注解的配置的更多相关文章

  1. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  2. (spring-第4回【IoC基础篇】)spring基于注解的配置

    基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...

  3. Spring基于注解@Required配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  4. Spring 基于注解的配置 简介

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  5. Spring基于注解的配置概述

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...

  6. java Spring 基于注解的配置(一)

    注解引用:1.service.xml 配置注解模式 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  7. Spring基于注解的配置1——@Required、@Autowired、@Qualifier示例及与传统注入方法的对比

    @Required注释 作用:用于属性的set方法,那么这个属性必须在xml文件的bean标签里面进行配置,否则就会抛出一个BeanInitializationException异常. 首先准备一个类 ...

  8. Spring框架bean的配置(3):基于注解的配置

    1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Contr ...

  9. Spring IoC — 基于注解的配置

    基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...

  10. 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

    复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...

随机推荐

  1. mysql 存在更新,不存在插入

    String sql = "insert into wb_result " + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ...

  2. CPP-基础:字节对齐

    一.   什么是字节对齐,为什么要对齐?      现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的 ...

  3. noj-1102-黑白图像

    1 //题目地址:http://acm.njupt.edu.cn/acmhome/problemdetail.do?method=showdetail&id=1102              ...

  4. MySQL 实时监控日志

    简单的梳理一下为什么要写这边文章,主要是学了ORM之后,发现通过ORM插入数据真的很方便,但是通过ORM生成的SQL语句又是怎么写的呢,百思不得姐.于是就找到了这个办法 首先查看一下查看MySQL 日 ...

  5. PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)

    http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...

  6. ViewController的lifecycle和autolayout

  7. webpack4.x加vue模板文件简单还原vue-cli

    1.首先 npm init -y 创建一个项目 2.安装vue npm install vue --save 3.然后安装webpack 注意如果全局没有还要安装全局的webpack和webpack- ...

  8. [LUOGU]P1443 马的遍历

    题目描述 有一个n*m的棋盘(1< n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘的大小和马的坐标 输 ...

  9. Juqyer:$.ajax()方法详解

    Jquery中的ajax方法参数总是记不住,这里记录一下. 最常用的属性是:url.data 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为S ...

  10. 【linux】【网络安全】linux中怎样关闭ICMP回应功能

    引用自:http://blog.csdn.net/qq844352155/article/details/49700121       linux中怎样关闭ICMP回应功能   输入:   echo ...