spring配置

<context:annotation-config/>

向spring容器中注册,declares support for general annotations such as @Required, @Autowired, @PostConstruct, and so on.

<mvc:annotation-driven /> 

是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。

通过处理器映射DefaultAnnotationHandlerMapping和处理器适配器 AnnotationMethodHandlerAdapter 来开启支持@Controller 和@RequestMapping注解的处理器。@Controller: 用于标识是处理器类;

@RequestMapping: 请求到处理器功能方法的映射规则;

@RequestParam: 请求参数到处理器功能处理方法的方法参数上的绑定;

@ModelAttribute: 请求参数到命令对象的绑定;

@SessionAttributes: 用于声明session级别存储的属性,放置在处理器类上,通常列出 模型属性(如@ModelAttribute) 对应的名称, 则这些属性会透明的保存到session中;

@InitBinder: 自定义数据绑定注册支持,用于将请求参数转换到命令对象属性的对应类型;

http://www.tuicool.com/articles/36ZVreQ

http://sishuok.com/forum/blogPost/list/6117.html

<context:component-scan base-package="com.shop"  />

Scans the classpath for annotated components that will be auto-registered as Spring beans. By  default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will  be detected. Note: This tag implies the effects of the 'annotation-config' tag, activating @Required,  @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit  annotations in the component classes, which is usually desired for autodetected components

@Repository、@Service、@Controller 和 @Component 将类标识为Bean.@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean.为什么 @Repository 只能标注在 DAO 类上呢?这是因为该注解的作用不只是将类识别为Bean,同时它还能将所标注的类中抛出的数据访问异常封装为 Spring 的数据访问异常类型。 Spring本身提供了一个丰富的并且是与具体的数据访问技术无关的数据访问异常结构,用于封装不同的持久层框架抛出的异常,使得异常独立于底层的框架。

@Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。

@Service 通常作用在业务层,但是目前该功能与 @Component 相同。

@Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。

通过在类上使用 @Repository、@Component、@Service 和 @Constroller 注解,Spring会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中。这些类就成了 Spring受管组件。这三个注解除了作用于不同软件层次的类,其使用方式与 @Repository 是完全相同的。

控制器

@Controller  @RequestMapping(value="/user")                 //①处理器的通用映射前缀  public class HelloWorldController2 {  
    @RequestMapping(value = "/hello2")        //②相对于①处的映射进行窄化  
    public ModelAndView helloWorld() {  
         //省略实现      }  
}

URL映射

spring3 的restful API RequestMapping介绍

http://blog.sina.com.cn/s/blog_72827fb10101pl9i.html




你可能感兴趣的:(spring配置)