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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://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 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean id="velocityCongfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/WEB-INF/apps/web/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding ">UTF-8</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.velocity.VelocityView" />
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://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的导向了,举例如下:
@Controller
public 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后不好的地方,相信以后很改正过来的!
分享到:
相关推荐
精通Spring2.5pdf书籍proSpring2.5
spring2.5 定时器任务 spring2.5 定时器任务 spring2.5 定时器任务 spring2.5 定时器任务
有很多新手不会分离spring的包而...吾将spring2.5的包分离出来欲给一些人以方便。只需下载该包添加到项目中引用即可,如果单独用spring管理事务,记住用JdbcTemplate来获取数据源对象。 再附xml配置文件一个,以供参考
spring 2.5 spring 2.5帮助文档 spring 2.5中文帮助文档 spring帮助文档 spring文档
Spring2.5中文框架开发手册Spring2.5中文框架开发手册Spring2.5中文框架开发手册
这是一个struts 1.2 + spring 2.5 + hibernate 3.2框架demo, 运行环境为eclipse 3.2 + tomcat 5.5 + oracle 11g
spring2.5是根据Sping2.5官方文档生成 chm格式
spring2.5中文文档 spring2.5中文文档
eclipse搭建spring2.5eclipse搭建spring2.5eclipse搭建spring2.5
Spring2.5的源代码,需要认真研究!
最近做的Spring 2.5 AOP 例子,保留下以后复习说不定用得上. 1.使用类扫描功能这个有点意思.使用时方便了许多. 2.XML配置方式
压缩文件中包含spring2.5 用到的所有jar文件;提供给大家下载使用
Spring2.5支持更多的java注释,使用起来更方便
spring2.5需要的jarspring2.5需要的jarspring2.5需要的jarspring2.5需要的jar
精通spring2.5精通spring2.5精通spring2.5精通spring2.5精通spring2.5
MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, ...
Spring2.5的国际化配置 基于浏览器语言的国际化配置 使用Spring的MVC,并且配置中有配置Resource文件 <!-- 资源文件绑定器 --> <bean id="messageSource" class="org.springframework.context.support....
spring2.5源码 spring2.5源码 spring2.5源码
Spring Framework 开发参考手册