Spring 中的注解
1、普通方式注解
a、在配置文件中配置
1、导入命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
2、到入依赖注入的注解解析器
<context:annotation-config></context:annotation-config>
3、把student和person导入进来
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config></context:annotation-config>
<bean id="student" class="cn.test.annotation2.Student"></bean>
<bean id="person" class="cn.test.annotation2.Person"></bean>
</beans>
配置Demo
b、再类中添加注解@Resource--字段注解 @PostConstruct--初始化注解 @PreDestroy--销毁注解
package cn.test.annotation2; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource; public class Person { private Long pid;
@Resource(name="student")
//@Autowired//按照类型进行匹配
//@Qualifier("student")
private Student student; public void say(){
this.student.say();
} @PostConstruct
public void init(){
System.err.println("Person Init");
} @PreDestroy
public void destory(){
System.err.println("Person distory");
}
}
类注解Demo
c、测试
原理
启动spring容器,并且加载配置文件会为student和person两个类创建对象
当解析到<context:annotation-config></context:annotation-config>会启动依赖注入的注解解析器
会在纳入spring管理的bean的范围内查找看哪些bean的属性上有@Resource注解
如果@Resource注解的name属性的值为"",则会把注解所在的属性的名称和spring容器中bean的id进行匹配如果匹配成功,则把id对应的对象赋值给该属性,如果匹配不成功,则按照类型进行匹配,如果再匹配不成功,则报错
如果@Resource注解的name属性的值不为"",会把name属性的值和spring容器中bean的id做匹配,如果匹配成功,则赋值,如果匹配不成功 ,则直接报错
说明:
注解只能用于引用类型
@Test
public void dosome(){
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/annotation2/applicationContext.xml");
Person person= (Person) applicationContext.getBean("person");
person.say();
applicationContext.close();
}
客户端测试Demo
2、扫描注解
1、配置
1、导入命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
2、 component就是bean
base-package
会在base-package的值所在的包及子包下扫描所有的类
<context:component-scan base-package="cn.test.annotation.scan"></context:component-scan>
2、在类中添加注释
@Component
public class Person {
@Resource
private Student student;
}
3、测试
原理
启动spring容器,加载配置文件
spring容器解析到
<context:component-scan base-package="cn.test.annotation.scan"></context:component-scan>
spring容器会在指定的包及子包中查找类上是否有@Component
如果@Component注解没有写任何属性
@Component
public class Person{ }
==
<bean id="person" class="..Person">
如果@Component("aa")
@Component
public class Person{ }
==
<bean id="aa" class="..Person">
在纳入spring管理的bean的范围内查找@Resource注解
执行@Resource注解的过程
说明:
xml效率比较高,但是书写比较麻烦
注解效率比较低,书写比较简单
Spring 中的注解的更多相关文章
- Spring中@Autowired注解、@Resource注解的区别 (zz)
Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...
- Spring中Value注解的使用
Spring中Value注解的使用 分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报 有的时候我们定义了Properties文件,并且使用Spring的Pr ...
- 全面解析Spring中@ModelAttribute注解的用法
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...
- EnableAutoConfiguration注解 Spring中@Import注解的作用和使用
EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...
- Spring中常用注解的介绍
spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...
- Spring中使用注解时启用<context:component-scan/>
在spring中使用注解方式时需要在spring配置文件中配置组件扫描器:http://blog.csdn.net/j080624/article/details/56277315 <conte ...
- Spring中异步注解@Async的使用、原理及使用时可能导致的问题
前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <面试必杀技,讲一讲Spring中的循环依赖> 然后,很多同学碰到了下面这个问题,添加了S ...
- Spring中@Import注解的使用
Spring中@Import注解的使用 @Import注解算是SpringBoot自动配置原理中一个很重要的注解 认识@Import注解 先看一下源码 @Target(ElementType.TYPE ...
- Spring中常用注解
1.@Component 创建类对象,相当于配置<bean/> 2.@Service @Service与@Component功能相同,写在ServiceImpl类上 3.@Reposito ...
- 【进阶】Spring中的注解与反射
[进阶]Spring中的注解与反射 目录 [进阶]Spring中的注解与反射 前言 一.内置(常用)注解 1.1@Overrode 1.2@RequestMapping 1.3@RequestBody ...
随机推荐
- poj1935
给定起点和要经过的点,求最短路径我发现,关于路径的treedp,设计的关键在于每个节点的状态怎么表示对于这道题,有一种常见的方法是令f[i,1]表示经过这个点且还要回来的路径,f[i,0]表示留在以i ...
- oracle查询语句【转载】
建立的表: 表名:REGIONS 序号 列名 数据类型 长度 小数位 标识 主键 允许空 默认值 说明 1 REGION_ID NUMBER 是 否 2 REGION_NAME VARCHAR2 25 ...
- AOP Aspect Oriented Programming
原理AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bus ...
- Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理
题目: http://cojs.tk/cogs/problem/problem.php?pid=1070 1070. [焦作一中2012] 玻璃球游戏 ★ 输入文件:marbles.in 输出 ...
- [转]使用Linux命令行测试网速
装speedtest-cli speedtest-cli是一个用Python编写的轻量级Linux命令行工具,在Python2.4至3.4版本下均可运行.它基于Speedtest.net的基础架构来测 ...
- Sicily1153-马的周游问题:启发式搜索
代码地址: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1153.c 题目如下: 1153. 马的周游问题 C ...
- Magento网站迁移指南
"Magento网站迁移指南":关键词:magento 网站 迁移 指南 上周五,为mkt同事迁移了一个从本机到godaddy的magento系统. 中间出了不少状况, 现在写个迁 ...
- SPOJ DQUERY 求区间内不同数的个数 主席树
这题跟HDU3333差不多吧. 离线的做法很简单,不再说了 以前做过. 主席树的做法就比较暴力了.. 什么是主席树呢.. 其实是某种称号. 在该题中的体现是可持久化的线段树. 对于一个数 如果以前没出 ...
- CFS: 虚拟运行时间
http://edsionte.com/techblog/archives/4331 nice和prio的关系如下: #define NICE_TO_PRIO(nice) (MAX_RT_PRIO+n ...
- Java实现常见排序算法
常见的排序算法有冒泡排序.选择排序.插入排序.堆排序.归并排序.快速排序.希尔排序.基数排序.计数排序,下面通过Java实现这些排序 1.冒泡排序 package com.buaa; import j ...