我们使用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的方式注入
分享到:
相关推荐
详解 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