读书人

浅谈SPRING的诠释注入

发布时间: 2012-11-16 14:12:14 作者: rapoo

浅谈SPRING的注释注入
Spring2.5的注释注入Bean已经成为众多Spring爱好者的首选(3.0版本和2.5基本差不多),但是在使用过程中可能会出现很多的问题,笔者找了网上很多的资料,没有一个真正完整使用注释来完成一个系统,这里给笔者的使用心得及遇到主要棘手的问题贴上来,和大家共勉!
相信大家使用Spring2.5主要都是为了注释的应用,尤其是@Controller和@Service、@Component、@Resource这些标签的应用,因为这些标签替代了更多的XML配置,使用Spring2.5注释来完成这些工作会带来很多的便利,当然初次使用肯定会遇到很多的原因!
使用@Controller标签的时候需要注意配置步骤和配置文件的写法,首先配置web.xml文件,代码如下:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><servlet><servlet-name>test</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>test</servlet-name><url-pattern>*.jhtml</url-pattern></servlet-mapping></web-app>

web.xml中在context-param参数中加载applicatonContext.xml,不要再init-param中加载,因为Spring加载扫描注释Bean有个顺序
写好web.xml后需要在web.xml的同等目录下建立一个text-servlet.xml配置文件,代码如下:
<?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"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-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  <!-- 把标记了@Controller注解的类转换为bean -->  <context:component-scan base-package="com.javatalker"/> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  <bean />  <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --><bean id="velocityCongfig"/><property name="contentType"><value>text/html;charset=UTF-8</value></property></bean></beans>    


在这个配置文件中主要配置spring需要扫描的包、开启Spring注释功能的识别、设定Spring视图,这样就可以了
然后写一个applicationContext.xml,代码如下:
<?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"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-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!-- 使Spring关注Annotation -->   <context:annotation-config />   </beans>


在这个配置文件中只需要一行代码,用来告诉Spring启用注释配置。
这样就可以使用@Controller的注释来完成url的导向了,举例如下:
@Controllerpublic class UserAction {@RequestMapping("/user/save.jhtml")public ModelAndView Save(HttpServletRequest request,HttpServletResponse response) {return new ModelAndView("save.html");}}


如上代码,要注意的是UserAction是个单独的class,不继承Spring任何类,不像以前需要继承org.springframework.web.servlet.mvc.multiaction.MultiActionController,使用@Controller注释后,就可以在方法级上来完成对应的url,使用@RequestMapping来完成这项工作,比如上面的注入表示,访问UserAction中save方法就可以使用:/user/save.jhtml这样的url,这样的url比user.jhtml?method=save更加友好了吧!
关于@Controller的说明到这里就差不多了!

其次要强调的是注释注入Bean的应用,使用@Component这些注释可以标注一个class为Spring管理中的Bean,但是有些Bean不是class能够解决的,必须使用配置文件来完成,比如DataSource等,这样就需要使用Spring的配置文件来完成,Spring2.5开始在加载Bean的时候首先扫描xxx-servlet.xml中配置的Bean,然后扫描使用注释标注的Bean,这样一来如果给datasource这些bean配置在applicatonContext.xml中,而在某个Bean中使用@Autowired注入datasource就会出错,解决的方法很简单,就是给配置的Bean放到xxx-servlet.xml,个人感觉这个也是Spring2.5后不好的地方,相信以后很改正过来的!


读书人网 >软件架构设计

热点推荐