`
erikchang
  • 浏览: 49489 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

Spring2.5 综合应用探索

    博客分类:
  • java
阅读更多

     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后不好的地方,相信以后很改正过来的!

分享到:
评论
3 楼 dch1287 2009-12-30  
Spring Reference Doc

另外 3.0 和 2.5 差别还是不小的 尤其是MVC这块
2 楼 chinakite 2009-12-30  
CTO是管理职位,确实可能不知道,你应该问问Senior engineer 
1 楼 erikchang 2009-12-29  
发现一个非常不好的现象,我这篇文章中提到的一些东西,询问了javaeye中CTO级别的都说不清楚,怎么很多人都认为这是新手帖呢?呵呵,这样下去不好!

相关推荐

Global site tag (gtag.js) - Google Analytics