annotation(spring注解)
来源:互联网 发布:观澜网络官网 编辑:程序博客网 时间:2024/05/12 17:18
2.0
@Required
设置在set方法上如果该属性的参数没设置就会报
异常:org.springframework.beans.factory.BeanInitializationException
2.5
@PostConstruct和@PreDestroy
3.0
JSR-330(javaee6:javax.inject),如注解@Inject和@Named。
@Component,@Repository和@Service是一样的,都是在声明Spring IoC容器中的bean。@Autowired和@Component@Inject和@Named基本上,它的工作原理和上面相同,只是用不同的注解
JSR-330和spring的区别:1.@Inject没有required的属性,以确保bean被注入成功2.在spring容器中,JSR默认的范围是单例,提供了@Scope注解。不过,这其中仅用于用于创建自己的注解,不像spring中可以用@Scope来定义范围,3.没有相当于spring中的@Value,@Required或者@Lazy
Spring的注解更强大,但仅适用于Spring框架。该JSR-330是一个标准的规范,并且它支持所有的JavaEE环境遵循JSR-330规范的。对于新的或迁移的项目,它总是建议使用JSR-330的注解,它适用于spring3最好。xml中的定义会覆盖类中注解的Bean(spring工厂管理的类)定义
注释需要组件扫描工作<context:component-scan base-package="" />该标签表示隐式注册后处理器包括AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
或者使用隐式注册(隐式注册 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。) <context:annotation-config/>
<context:annotation-config/> 如果你在web.xml中配置了DispatcherServle,在控制器它只检查@Autowired而不会检查其他,反之它会扫描类中的后置处理器
@Autowired
@Autowired默认按类型自动注入属性,如果要按名字可在方法参数上加@Qualifier("")或者在方法上加@Qualifier(""),如果属性没设置有不想报错加上@Autowired(required=false)
@Autowired
1、Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> </beans>
@Resource
1、@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了。
2、要让注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
3、@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。
@PostConstruct 和 @PreDestroy
标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。
public class CachingMovieLister { @PostConstruct public void populateMovieCache() { // populates the movie cache upon initialization... } @PreDestroy public void clearMovieCache() { // clears the movie cache upon destruction... } }
@Component
1、使用@Component注解可以直接定义Bean,而无需在xml定义。但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义。
2、@Component 有一个可选的入参,用于指定 Bean 的名称。
@Component public class ActionMovieCatalog implements MovieCatalog { // ... }
4、默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标
- spring annotation 注解 简记
- Spring Annotation(注解) 详解
- Spring Annotation(注解) 详解
- Spring Annotation 注解
- Spring Annotation(注解) @Resource
- Spring Annotation(注解) @Component
- 【framework】spring-注解(annotation)
- Spring注解Annotation
- annotation(spring注解)
- spring annotation注解 autowire
- spring annotation注解 @Resource
- spring annotation注解
- Spring---annotation注解初学
- Spring 注解 Annotation
- Spring下Hibernate Annotation注解
- Spring下Hibernate Annotation注解
- spring annotation注解 依赖注入
- Spring Annotation(注解) Autowired Qualifier
- 钱钱贷P2P网贷平台如何稳健发展
- JVM(三)------内存管理和垃圾回收
- javascript window 常用事件
- C++中的单例模式(★好文章firecat推荐,多关注原作者的博客★)
- Android通过播放多张图片形成一个动画
- annotation(spring注解)
- mac下eclipse的svn插件的安装
- poj 1003 Hangover
- JVM(四)------内存调优
- P2P投资者需擦亮眼
- Apache vhost配置
- Mysql 免安装版本
- 学习性能测试笔记
- [BFS]HDU1044 Collect More Jewels