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= ...
随机推荐
- c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)
一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...
- jquery学习笔记(三):事件和应用
内容来自[汇智网]jquery学习课程 3.1 页面加载事件 在jQuery中页面加载事件是ready().ready()事件类似于就JavaScript中的onLoad()事件,但前者只要页面的DO ...
- VS2010-MFC(常用控件:组合框控件Combo Box)
转自:http://www.jizhuomi.com/software/189.html 上一节讲了列表框控件ListBox的使用,本节主要讲解组合框控件Combo Box.组合框同样相当常见,例如, ...
- t检验中的t值和p值是什么关系_t检验和p值的关系
t检验中的t值和p值是什么关系_t检验和p值的关系 t检验中通过样本均值 总体均值 样本标准差 样本量 可以计算出一个t值,这个t值和p值有什么关系? 根据界值表又会查出一个数,这个数和t值比较,得出 ...
- HDU-1501-Zipper-字符串的dfs
Given three strings, you are to determine whether the third string can be formed by combining the ch ...
- 像bootstrap一样的去做web编程
1: 闭包 boot的闭包方式有点特别,普通的闭包是这样的: (function ($) { })(jQuery) 这种写法是怕全局污染,把$封闭在自己的空间里,暴露在外面的只有jQuery,这样,如 ...
- Java基础拾遗(一) — 忽略的 Integer 类
学习前我们先看一个笔者之前项目踩过的坑 public static void main(String[] args) { Integer a = 127; Integer b = 127; Syste ...
- 数据可视化(matplotilb)
一,matplotilb库(数学绘图库) mat数学 plot绘图 lib库 matplotlib.pyplot(缩写mp)->python 最常用接口 mp.plot(水平坐标,垂直坐标数组 ...
- java关于lombok(包括父类参数)
java关于lombok对bean对象进行自动设置 使用说明 使用方式 注释类型 @NonNull @Data(常用) @NoArgsConstructor(常用)/@RequiredArgsCons ...
- HTML中被废弃的标签<b><u><i><s>
<strong>代替<b>给文字加粗 <ins>代替<u>给文本添加下划线 <em>代替<i>将文本倾斜 <del> ...