Spring几种常用注解
2014-04-28 19:54
218 查看
记住这下面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自带的@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 注解的讲解博客
相关文章推荐
- SpringBoot中常用注解@ PathVaribale / @ RequestParam / @ GetMapping介绍 本篇博文将介绍几种如何处理URL中的参数的注解@ PathVariba
- Spring 常用的几种注解
- Spring 中常用的几种注解
- Spring中几种常用的注解
- 常用spring注解的使用
- Spring 常用注解
- Spring常用注解记录
- spring和springmvc常用注解
- SpringBoot | 第六章:常用注解介绍及简单使用
- spring 常用的几个注解的个人理解
- Spring常用注解简析
- spring 常用注解集合
- [转]SpringMVC Controller介绍及常用注解
- Spring常用注解
- spring常用注解使用解析
- Spring常用注解
- Spring常用注解
- Spring-mvc-01了解常用注解
- Spring常用注解
- Spring系列之Spring常用注解总结