SpringMVC Service 注解及简单配置文件讲解
2015-08-20 11:01
686 查看
<?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:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 自动扫码的包名 -->
<context:component-scan base-package="con.hao947.core"></context:component-scan>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解释类 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
<!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<!-- 拦截器 -->
<mvc:interceptors>
<bean class="com.hao947.mvc"></bean>
</mvc:interceptors>
<!-- 对静态资源文件的访问 方案一 (二选一) -->
<mvc:default-servlet-handler />
<!-- 对静态资源文件的访问 方案二 (二选一) -->
<mvc:resources mapping="/images/**" location="/images/"
cache-period="" />
<mvc:resources mapping="/js/**" location="/js/"
cache-period="" />
<mvc:resources mapping="/css/**" location="/css/"
cache-period="" />
</beans>
<context:component-scan/> 扫描指定的包中的类上的注解,常用的注解有:
@Controller 声明Action组件
@Service 声明Service组件 @Service("myMovieLister")
@Repository 声明Dao组件
@Component 泛指组件, 当不好归类时.
@RequestMapping("/menu") 请求映射
@Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName")
@Autowired 用于注入,(srping提供的) 默认按类型装配
@Transactional( rollbackFor={Exception.class}) 事务管理
@ResponseBody
@Scope("prototype") 设定bean的作用域
<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。
<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,
@NumberFormatannotation支持,
@DateTimeFormat支持,
@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
后面,我们处理响应ajax请求时,就使用到了对json的支持。
后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。
如何替换 <mvc:annotation-driven />?他到底做了什么工作,请看,最后面的 十九节 <mvc:annotation-driven /> 到底做了什么工作。
<mvc:interceptors/> 是一种简写形式。通过看前面的大图,知道,我们可以配置多个HandlerMapping。<mvc:interceptors/>会为每一
个HandlerMapping,注入一个拦截器。其实我们也可以手动配置为每个HandlerMapping注入一个拦截器。
<mvc:default-servlet-handler/> 使用默认的Servlet来响应静态文件。
<mvc:resources mapping="/images/**"
location="/images/" cache-period=""/> 匹配URL /images/** 的URL被当做静态资源,由Spring读出到内存中再响应http。
原文:http://blog.csdn.net/hao947_hao947/article/details/27844905?utm_source=tuicool
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 自动扫码的包名 -->
<context:component-scan base-package="con.hao947.core"></context:component-scan>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解释类 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
<!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<!-- 拦截器 -->
<mvc:interceptors>
<bean class="com.hao947.mvc"></bean>
</mvc:interceptors>
<!-- 对静态资源文件的访问 方案一 (二选一) -->
<mvc:default-servlet-handler />
<!-- 对静态资源文件的访问 方案二 (二选一) -->
<mvc:resources mapping="/images/**" location="/images/"
cache-period="" />
<mvc:resources mapping="/js/**" location="/js/"
cache-period="" />
<mvc:resources mapping="/css/**" location="/css/"
cache-period="" />
</beans>
<context:component-scan/> 扫描指定的包中的类上的注解,常用的注解有:
@Controller 声明Action组件
@Service 声明Service组件 @Service("myMovieLister")
@Repository 声明Dao组件
@Component 泛指组件, 当不好归类时.
@RequestMapping("/menu") 请求映射
@Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName")
@Autowired 用于注入,(srping提供的) 默认按类型装配
@Transactional( rollbackFor={Exception.class}) 事务管理
@ResponseBody
@Scope("prototype") 设定bean的作用域
<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。
<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,
@NumberFormatannotation支持,
@DateTimeFormat支持,
@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
后面,我们处理响应ajax请求时,就使用到了对json的支持。
后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。
如何替换 <mvc:annotation-driven />?他到底做了什么工作,请看,最后面的 十九节 <mvc:annotation-driven /> 到底做了什么工作。
<mvc:interceptors/> 是一种简写形式。通过看前面的大图,知道,我们可以配置多个HandlerMapping。<mvc:interceptors/>会为每一
个HandlerMapping,注入一个拦截器。其实我们也可以手动配置为每个HandlerMapping注入一个拦截器。
<mvc:default-servlet-handler/> 使用默认的Servlet来响应静态文件。
<mvc:resources mapping="/images/**"
location="/images/" cache-period=""/> 匹配URL /images/** 的URL被当做静态资源,由Spring读出到内存中再响应http。
原文:http://blog.csdn.net/hao947_hao947/article/details/27844905?utm_source=tuicool
相关文章推荐
- 关于Java特种兵下册
- 【java开发系列】—— 自定义注解(转)
- java正则表达式
- Java栈与堆
- 使用JAVA执行SAS带有参数的存储过程
- spring简介-----阿冬专栏
- Java replace 和 replaceAll
- Java虚拟机详解04----GC算法和种类【重要】
- Java虚拟机JVM学习07 类的卸载机制
- 好炫的Lambda表达式,Java党用起来!(最简易Lambda教程)
- struts第一讲为什么有struts和工作原理i
- Java Calendar详解 - 获取近一周日期与星期
- java的垃圾回收
- java的垃圾回收
- 关于java的初始化顺序的问题
- 去掉Eclipse打开后定期弹出Usage Data Upload对话框
- (翻译)什么是Java的永久代(PermGen)内存泄漏
- JAVA正则表达式
- java后台传递json到前台 中文乱码解决方法
- struts2简介----阿冬专栏