spring2.5 容器注解的理解

我们使用Spring 一般式在xml配置文件中进行注入.但是这种方式使得配置过于臃肿。试想一个应用中,有上千个对象,而每个对象又需要注入很多其它对象,那么我们的配置文件就显得非常的臃肿了。

Spring2.0 以后,我们可以使用annotation来为Spring的配置文件进行“减肥”

     我使用的是Spring2.5.

     第一:首先准备需要的jar包:SPRING_FRAMEWORK_HOMEspring发行包所在的目录

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的方式注入

posted @ 2009-11-25 19:29 王元森-&gt; 阅读(520) | 评论(0) | 编辑

今天配置spring 是碰到tx:annotation-driven is not bound 的问题,这个错误的原因很简单是:

我们在定义申明AOP的时候。。没有加载schema。

具体表现如下:

<tx:annotation-driven transaction-manager="txManager" />
   <bean id="personService"
     class="cn.itcast.service.impl.PersonServiceBean">
   <property name="dataSource" ref="dataSource" />

这时会抛出异常不认<TX>标签。。起先还以为是没有加载JAR包呢。。

后来读AOP文档才发现<beans>中要加入“xmlns:aop”的命名申明,并在“xsi:schemaLocation”中指定aop配置的schema的地址

配置文件如下:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    
http://www.springframework.org/schema/beans/spring-beans.xsd
                    
http://www.springframework.org/schema/tx
                    
http://www.springframework.org/schema/tx/spring-tx.xsd
                    
http://www.springframework.org/schema/aop
                    
http://www.springframework.org/schema/aop/spring-aop.xsd">

这些才是最关键的地方。。后面的配置不变。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值