Spring中使用annotation注入 (2009-04-15 15:56:44)
<wbr><wbr><wbr><wbr> 我们使用Spring 一般式在xml配置文件中进行注入.但是这种方式使得配置过于臃肿。试想一个应用中,有上千个对象,而每个对象又需要注入很多其它对象,那么我们的配置文件就显得非常的臃肿了。</wbr></wbr></wbr></wbr>
Spring2.0 以后,我们可以使用annotation来为Spring的配置文件进行“减肥”
<wbr><wbr><wbr><wbr> 我使用的是Spring2.5.</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> 第一:首先准备需要的jar包:<span style="color:#000000">SPRING_FRAMEWORK_HOME为spring发行包所在的目录</span></wbr></wbr></wbr></wbr>
A)<wbr><wbr><wbr><wbr> SPRING_FRAMEWORK_HOME/dist/spring.jar </wbr></wbr></wbr></wbr>
B)<wbr><wbr><wbr><wbr> SPRING_FRAMEWORK_HOME/lib/ jakarta-commons/ commons-logging.jar</wbr></wbr></wbr></wbr>
C)<wbr><wbr><wbr><wbr> SPRING_FRAMEWORK_HOME/lib/log4j / log4j- 1.2.15.jar(为了在项目中使用log4j输出日志信息)</wbr></wbr></wbr></wbr>
D)<wbr><wbr><wbr><wbr> SPRING_FRAMEWORK_HOME/lib/j2ee/common-annotations.jar</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr> 第二:引入Spring配置的命名空间以及命名空间的schema文件的配置.这些配置可以到参考手册中找到。参考手册在SPRING_FRAMEWORK_HOME/ docs/reference中。有html版本和pdf版本。找到【3.2.1.1. Configuration metadata】拷贝配置即可
<wbr></wbr>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" <wbr><wbr><wbr><wbr><wbr><wbr> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</wbr></wbr></wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr><strong>xmlns:context="http://www.springframework.org/schema/context"</strong></wbr></wbr></wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr> xsi:schemaLocation="http://www.springframework.org/schema/beans</wbr></wbr></wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr><a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a></wbr></wbr></wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong><a href="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a></strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr><strong><a href="http://www.springframework.org/schema/context/spring-context-2.5.xsd">http://www.springframework.org/schema/context/spring-context-2.5.xsd</a></strong>"></wbr></wbr></wbr></wbr></wbr></wbr> <wbr></wbr> <wbr><wbr><wbr><strong><context:annotation-config /></strong></wbr></wbr></wbr> <wbr></wbr> </beans> 在java代码中使用@Autowired或者@Resource注解方式进行装配。但我们要在xml配置中引入以下信息: A)<wbr><wbr><wbr><wbr> context命名空间以及这个命名空间的schema文件</wbr></wbr></wbr></wbr> B)<wbr><wbr><wbr><wbr> <context:annotation-config /> 让Spring启用对annotation的支持。它其实是注册了多个对annotation进行解析处理的处理器:</wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>AutowiredAnnotionBeanPos<wbr>tProcessor</wbr></strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>CommonAnnotationBeanPost<wbr>Processor</wbr></strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>PersistenceAnnotionBeanP<wbr>ostProcessor</wbr></strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> RequiredAnnotationBeanPo<wbr>stProcessor.</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> (注意:annotation本身是不能干活的,要想让annotation干活,必须要有处理器来解析annotion) <wbr></wbr> 第三:在java代码中使用@Resource进行注入 <wbr>public class UserService {<br><strong><span style="color:#ff0000"><wbr>@Resource(name="userDao")<br></wbr></span></strong><wbr>private UserDAO userDao;</wbr></wbr> <wbr>public UserDAO getUserDao() {<br><wbr><wbr>return userDao;<br><wbr>}<br><wbr>public void setUserDao(UserDAO userDao) {<br><wbr><wbr>this.userDao = userDao;<br><wbr>}<br><wbr>public String[] getAllUser(){<br><wbr><wbr>return userDao.findUsers();<br><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr></wbr> 第四:配置文件中的配置: <wbr>可以看到,我们没有在配置文件中为userService注入userDao,因为在 UserService类中使用annotation的方式注入</wbr> |
<wbr></wbr>