浅谈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>
<?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>
<?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>
@Controllerpublic class UserAction {@RequestMapping("/user/save.jhtml")public ModelAndView Save(HttpServletRequest request,HttpServletResponse response) {return new ModelAndView("save.html");}}