SpringMVC注解(持续更新ing)

来源:互联网 发布:hello world java代码 编辑:程序博客网 时间:2024/06/06 20:44

spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置。
@Configuration
Spring Description:Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime
标注在类上,相当于把该类作为spring的xml配置文件中的,作用为:配置spring容器(应用上下文)

for example:

@Configuration public class AppConfig {     @Bean     public MyBean myBean() {         // instantiate, configure and return bean ...     } }

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:jdbc="http://www.springframework.org/schema/jdbc"      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false"></beans>

@Bean 标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的,作用为:注册bean对象

@ComponentScan
Spring Description:Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML’s element.
作用:扫描指定base-package路径下的@Bean@Component@Controller@Service@Ripository注解

for example:

@Configuration  @ComponentScan(basePackages = "org.example", nameGenerator = MyNameGenerator.class)  public class AppConfig {      ...  }  

XML配置

<beans>      <context:component-scan base-package="org.example"          name-generator="org.example.MyNameGenerator" />  </beans>

Optional Elements

basePackages:type Stirng[], Base packages to scan for annotated components.
excludeFilters:type excludeFilters[]

package spittr.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.web.servlet.config.annotation.EnableWebMvc;@Configuration@ComponentScan(basePackages={"spitter"},    excludeFilters={            @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)    })public class RootConfig {}

@Component
Spring Description:Indicates that an annotated class is a “component”.
作用:在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类
Optional Elements(String value)
The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.id标识元素,没有标注的话,默认名称是类名(头字母小写)
@Service
Spring Description:Indicates that an annotated class is a “Service”, originally defined by Domain-Driven Design (Evans, 2003) as “an operation offered as an interface that stands alone in the model, with no encapsulated state.”
作用:用于标注业务逻辑层组件
Optional Elements(String value)
The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.id标识元素,没有标注的话,默认名称是类名(头字母小写)
@ Repository
Spring Description:Indicates that an annotated class is a “Repository”, originally defined by Domain-Driven Design (Evans, 2003) as “a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects”.
作用:用于标注业务持久层组件
Optional Elements(String value)
The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.id标识元素,没有标注的话,默认名称是类名(头字母小写)
@Controller

@Controller("home")public class HomeController {    ...}

Spring Description:Indicates that an annotated class is a “Controller” (e.g. a web controller).
作用:控制层
Optional Elements(String value)
The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.id标识元素,没有标注的话,默认名称是类名(头字母小写)
@RequestMapping
Spring Description:Annotation for mapping web requests onto specific handler classes and/or handler methods. 为请求与控制类/方法之间添加映射
Optional Elements
consumes:type String[] 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

consumes = "text/plain"consumes = {"text/plain", "application/*"}

produces: type String[] 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

produces = "text/plain"produces = {"text/plain", "application/*"}produces = "application/json; charset=UTF-8"

method:The HTTP request methods to map to, narrowing the primary mapping: GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.type RequestMethod[]

import org.springframework.web.bind.annotation.RequestMethod;@RequestMapping(value="/register",method=RequestMethod.GET)    public String showRegistrationForm(Model model){        model.addAttribute(new Person());        return "registerForm";    }

params:The parameters of the mapped request, narrowing the primary mapping.请求参数中需要包含指定参数值
value:In a Servlet environment this is an alias for path(). For example @RequestMapping(“/foo”) is equivalent to @RequestMapping(value=”/foo”).表明请求路径
A 可以指定为普通的具体值;
B 可以指定为含有某变量的一类值(URI Template Patterns with Path Variables);动态绑定参数
C 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);
类定义处:规定初步的请求映射,相对于web应用的根目录;
方法定义处:进一步细分请求映射,相对于类定义处的URL。如果类定义处没有使用该注解,则方法标记的URL相对于根目录而言;