spring中使用annotation

上传人:xiao****1972 文档编号:84900178 上传时间:2019-03-05 格式:DOC 页数:7 大小:47.50KB
返回 下载 相关 举报
spring中使用annotation_第1页
第1页 / 共7页
spring中使用annotation_第2页
第2页 / 共7页
spring中使用annotation_第3页
第3页 / 共7页
spring中使用annotation_第4页
第4页 / 共7页
spring中使用annotation_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《spring中使用annotation》由会员分享,可在线阅读,更多相关《spring中使用annotation(7页珍藏版)》请在金锄头文库上搜索。

1、Spring中使用annotation注入 我们使用Spring 一般式在xml配置文件中进行注入.但是这种方式使得配置过于臃肿。试想一个应用中,有上千个对象,而每个对象又需要注入很多其它对象,那么我们的配置文件就显得非常的臃肿了。 Spring2.0 以后,我们可以使用annotation来为Spring的配置文件进行“减肥” 我使用的是Spring2.5. 第一:首先准备需要的jar包:SPRING_FRAMEWORK_HOME为spring发行包所在的目录A) SPRING_FRAMEWORK_HOME/dist/spring.jar B) SPRING_FRAMEWORK_HOME/l

2、ib/ jakarta-commons/ commons-logging.jarC) SPRING_FRAMEWORK_HOME/lib/log4j / log4j- 1.2.15.jar(为了在项目中使用log4j输出日志信息)D) SPRING_FRAMEWORK_HOME/lib/j2ee/common-annotations.jar 第二:引入Spring配置的命名空间以及命名空间的schema文件的配置.这些配置可以到参考手册中找到。参考手册在SPRING_FRAMEWORK_HOME/ docs/reference中。有html版本和pdf版本。找到【3.2.1.1. Config

3、uration metadata】拷贝配置即可 在java代码中使用Autowired或者Resource注解方式进行装配。但我们要在xml配置中引入以下信息:A) context命名空间以及这个命名空间的schema文件B) 让Spring启用对annotation的支持。它其实是注册了多个对annotation进行解析处理的处理器: AutowiredAnnotionBeanPostProcessor CommonAnnotationBeanPostProcessor PersistenceAnnotionBeanPostProcessor RequiredAnnotationBeanPo

4、stProcessor.(注意:annotation本身是不能干活的,要想让annotation干活,必须要有处理器来解析annotion) 第三:在java代码中使用Resource进行注入 public class UserService Resource(name=userDao) private UserDAO userDao; public UserDAO getUserDao() return userDao; public void setUserDao(UserDAO userDao) this.userDao = userDao; public String getAllUs

5、er() return userDao.findUsers(); 第四:配置文件中的配置: 可以看到,我们没有在配置文件中为userService注入userDao,因为在 UserService类中使用annotation的方式注入 Autowired 1、Spring 通过一个 BeanPostProcessor 对 Autowired 进行解析,所以要让 Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。 1 或者使用隐式注册(隐式注册 post-processors 包括了 Autowir

6、edAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。) Java代码 2、Autowired默认按照类型匹配的方式进行注入 3、Autowired注解可以用于成员变量、setter方法、构造器函数等 4、使用Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出 异常 5、Spring 允许

7、我们通过 Qualifier 注释指定注入 Bean 的名称。Autowired 和 Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。 public class MovieRecommender Autowired Qualifier(mainCatalog) private MovieCatalog movieCatalog; private CustomerPreferenceDao customerPreferenceDao; Autowired public MovieRecommender(CustomerPreferenceDao cus

8、tomerPreferenceDao) this.customerPreferenceDao = customerPreferenceDao; / . Resource 1、Resource 的作用相当于 Autowired,只不过 Autowired 按 byType 自动注入,Resource 默认按 byName 自动注入罢了。 2、要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor 3、Resource 有两个属性是比较重要的,分别是 name 和 type,Spring

9、将 Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。 public class SimpleMovieLister private MovieFinder movieFinder; Resource public void setMovieFinder(MovieFinder movieFinder) this.movieFinder = movieFinder; PostConstruct 和 PreDestroy 标注了 PostConstruct 注释的方法将在类实例化后调用,而标注了 PreDestroy 的方法将在类销毁之前调用。 url=http:/ public class CachingMovieLister PostConstruct public void populateMovieCache()

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号