我们使用Spring 一般式在xml配置文件中进行注入.但是这种方式使得配置过于臃肿。试想一个应用中,有上千个对象,而每个对象又需要注入很多其它 对象,那么我们的配置文件就显得非常的臃肿了。 Spring2.0 以后,我们可以使用annotation来为Spring的配置文件进行“减肥”
我使用的是Spring2.5.
第一:首先准备需要的jar包:SPRING_FRAMEWORK_HOME为spring发行包所在的目录
A) SPRING_FRAMEWORK_HOME/dist/spring.jar
B) SPRING_FRAMEWORK_HOME/lib/ jakarta-commons/ commons-logging.jar
C) 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. Configuration metadata】拷贝配置即可
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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 />
</beans>
在java代码中使用@Autowired或者@Resource注解方式进行装配。但我们要在xml配置中引入以下信息:
A) context命名空间以及这个命名空间的schema文件
B) <context:annotation-config /> 让Spring启用对annotation的支持。它其实是注册了多个对annotation进行解析处理的处理器:
AutowiredAnnotionBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotionBeanPostProcessor
RequiredAnnotationBeanPostProcessor.
(注意: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[] getAllUser(){
return userDao.findUsers();
}
}
第四:配置文件中的配置:
可以看到,我们没有在配置文件中为userService注入userDao,因为在 UserService类中使用annotation的方式注入
@Autowired
1、Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
或 者使用隐式注册(隐式注册 post-processors 包括 了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。)
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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/>
</beans>
2、@Autowired默认按照类型匹配的方式进行注入
3、@Autowired注解可以用于成员变量、setter方法、构造器函数等
4、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出 异常
5、Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。
public class MovieRecommender {
@Autowired
@Qualifier("mainCatalog")
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
@Resource
1、@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了。
2、要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
3、@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @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://kdboy.javaeye.com/blog/419159#][/url]
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 的名称。
[url=http://kdboy.javaeye.com/blog/419159#][/url]
@Component
public class ActionMovieCatalog implements MovieCatalog {
// ...
}
3、<context:component-scan/> 允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式:
过滤器类型表达式范例annotationorg.example.SomeAnnotationassignableorg.example.SomeClassregexorg\.example\.Default.*aspectjorg.example..*Service+
下面这个XML配置会忽略所有的@Repository注解并用“stub”储存库代替。
[url=http://kdboy.javaeye.com/blog/419159#][/url]
<beans ...>
<context:component-scan base-package="org.example">
<context:include-filter type="regex" expression=".*Stub.*Repository"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
4、默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标
@Scope("prototype")
@Repository
public class MovieFinderImpl implements MovieFinder {
// ...
}
5、Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)
@Service
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
@Repository
public class JpaMovieFinder implements MovieFinder {
// implementation elided for clarity
}
6、要检测这些类并注册相应的bean,需要在XML中包含以下元素,其中'basePackage'是两个类的公共父包 (或者可以用逗号分隔的列表来分别指定包含各个类的包)。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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:component-scan base-package="org.example"/>
</beans>
此 外,在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都 不需要在XML中提供任何bean配置元数据。
相关推荐
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
Spring IOC Annotation 注入 学习实例
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
NULL 博文链接:https://yufengweb.iteye.com/blog/775717
详解Spring_3.0基于Annotation的依赖注入实现
详解Spring基于Annotation的依赖注入实现
spring annotation注解 使用Spring注解来注入属性
NULL 博文链接:https://hi-beijing.iteye.com/blog/1204766
Spring对注解(Annotation)处理源码分析 解析和注入注解配置的资源 源码级别的分析
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
请举例说明如何在Spring中注入一个Java Collection 如何向Spring Bean中注入一个JavautilProperties 请解释Spring Bean的自动装配 请解释自动装配模式的区别 如何开启基于注解的自动装配 请举例解释Required ...
13、可以在controller中,使用annotation临时指定特殊的Mapping或者ExceptionHandler,而且整个体系更加灵活和可扩展。 14、可以限定request的Content-Type了。。。额,略蛋疼的功能 15、Flash的什么,看不懂 16、...
一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...
SSH笔记-annotation配置注入关系(使用@Component、@Service、@Repository、@Controller)
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。 1、共同点 两者都可以写在字段和setter方法上。两者...
NULL 博文链接:https://wuyuetian.iteye.com/blog/682394