Spring几种常用注解
来源:互联网 发布:原单包淘宝店铺 编辑:程序博客网 时间:2024/05/22 07:06
记住这下面Spring几种常用注解:
Spring自带的@Component注解及扩展@Repository、@Service、@Controller
1、被@Component注解的POJO类将自动被Spring识别并注册到Spring容器中,且自动支持自动装配。
2、被@Repository注解的POJO类表示DAO层实现
3、被@Service注解的POJO类表示Service层实现,从而见到该注解就想到Service层实现,使用方式和@Component相同;
4、被@Controller注解的类表示Web层实现,从而见到该注解就想到Web层实现,使用方式和@Component相同;
都可以通过spring的getBean形式得到注解类:如下:
1. ctx.getBean("component", TestCompoment.class);
==========================================================================
@Controller、@Service、@Repository是在@Component基础上拓展而来,它们分别对应表现层Bean,业务层Bean ,和数据层Bean,他们除了语义上不同之外,本质和用法上没有什么区别,。而@Component以过时不推荐使用。
@Resource其实与@Autowired的作用相似,都是代替<property>标签来注入对象的,但推荐使用@Resource。
@Scope是用来设定Bean的生命周期的。
除了上述常用的之外,其他的还有@Lazy、@DependsOn、@PostConstruct、@PreDestroy等其他的注解,作用分别是:延迟初始化、依赖其他Bea、初始化方法、析构方法。
注解@标识放置的代码位置不是随意的,根据jdk1.5规范,要求注解写在类、接口、属性(成员变量)、方法、构造函数或方法参数上。不同的注解,可放置的位置也不同。例如上述:@Controller、@Service、@Repository、@Scope、@Lazy、@DependsOn都只能放在类上;@Resource、@Autowired能放在属性、方法、构造函数上;@PostConstruct、@PreDestroy则只能放在方法上
要想使用Spring注解,必须要在applicationContext.xml中配置<context:annotation-config>和<context:component-scan>这两个标签,前者用来告诉Spring要启用注解,后者则用来告诉Spring注解所在的包,以便Spring启动时扫描包并进行注入。
http://hanyexiaoxiao.iteye.com/blog/410123 ---spring 注解的讲解博客
- Spring几种常用注解
- Spring 中常用的几种注解
- 8.1.Spring几种常用注解
- Spring 几种注解
- 声明Spring Bean和注入Bean的几种常用注解和区别
- spring下的几种注解
- 【spring ----- 注解篇】 常用注解
- spring常用注解
- Spring IOC 常用注解
- Spring常用注解方式
- Spring常用注解
- spring常用注解
- spring 常用注解集合
- Spring IOC 常用注解
- spring常用注解
- Spring常用注解
- Spring IoC常用注解
- spring常用注解
- poj-1151-Atlantis
- java中判断字符串是否为数字的方法
- SQL Server 触发器
- C++primer plus第六版课后编程题答案16.7
- 删除已经编译好的内核
- Spring几种常用注解
- 堆和栈的区别
- 关于 函数指针和函数名 例子的疑难解答
- Android 4.0 事件输入(Event Input)系统
- 《算法导论》笔记 第17章 17.3 势能方法
- 查询数据库所有表名
- OGNL表达上的“%,#,$”
- Android 4.0中按键的处理流程
- Android 中input event的分析