一些注解

来源:互联网 发布:淘宝没有货源怎么办 编辑:程序博客网 时间:2024/06/01 11:19

1、@RequestParam

@RequestMapping("/accounts/show") public void show(@RequestParam("number") String number, Map<String, Object> model) {     model.put("account", accountRepository.findAccount(number)); } 

 这里@RequestParam注解可以用来提取名为“number”的String类型的参数,并将之作为输入参数传入。 @RequestParam支持类型转换,还有必需和可选参数。类型转换目前支持所有的基本Java类型,你可通过定制的PropertyEditors 来扩展它的范围。下面是一些例子,其中包括了必需和可选参数:
    @RequestParam(value="number", required=false) String number
    @RequestParam("id") Long id
    @RequestParam("balance") double balance
    @RequestParam double amount 
注意,最后一个例子没有提供清晰的参数名。当且仅当代码带调试符号编译时,结果会提取名为“amount ”的参数,否则,将抛出IllegalStateException异常,因为当前的信息不足以从请求中提取参数。由于这个原因,在编码时最好显式的指定参数名。

 

2、@RequestMapping

类级别(Type-level),就是注释定义在类定义的上面。

方法级别(Method-level),就是注释定义在方法定义的上面。

举例说明:

@Controller@RequestMapping("/a")public class HelloWorldController {    @RequestMapping("/helloWorld")    public String helloWorld(Model model) {       model.addAttribute("message", "Hello World!");       return "helloWorld";    }}

@RequestMapping("/a")为类级别(Class-level),@RequestMapping("/helloWorld")为方法级别(Method-level)。这个例子是把请求地址/a/helloWorld映射到helloWorld处理器上。

@RequestMapping的属性:

value:通过这个注释表达主要的映射。

method:通过HTTP请求的method来缩小主映射的范围。GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE。例:@RequestMapping(value="/b",method=RequestMethod.POST)

params:通过映射请求的参数来缩小主映射的范围。

 

3、@RequestBody 和@ResonseBody

@RequestBody 将 HTTP 请求正文插入方法中,使用适合的HttpMessageConverter将请求体写入某个对象。

@ResponseBody 将内容或对象作为 HTTP 响应正文返回,使用@ResponseBody将会跳过视图处理部分,而是调用适合HttpMessageConverter,将返回值写入输出流。

 

4、@Value:对配置文件中配置的值,根据键值对注入到程序中。

java代码:

@Controller  @RequestMapping("/reg")   public class RegController extends BaseController {            @Value("${jdbc.url}")        private String signDetail;  

application.xml :

<import resource="classpath:org/jep/applicationContext/applicationContext-infrastructure.xml"/>

applicationContext-infrastructure.xml

<!-- 数据源定义,使用Apache DBCP 连接池-->      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="locations">              <list>                  <value>classpath:jdbc.properties</value>                  <value>classpath:base.properties</value>              </list>                      </property>                                  </bean>  


另外一种注入方式:

在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件 中的文件,进行键值对的注入,例子如下: 1 首先在applicationContext.xml中加入:    <beans xmlns:util="http://www.springframework.org/schema/util"      xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">  </beans>       的命名空间,然后 2 <util:properties id="settings" location="WEB-INF/classes/META-INF/spring/test.properties" />  3 创建test.properties    abc=123 4 import org.springframework.beans.factory.annotation.Value;   import org.springframework.stereotype.Controller;   import org.springframework.web.bind.annotation.RequestMapping;     @RequestMapping("/admin/images")   @Controller   public class ImageAdminController {         private String imageDir;              @Value("#{settings['test.abc']}")       public void setImageDir(String val) {           this.imageDir = val;       }     }  


5、@Resource

       @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
  @Resource装配顺序
 1、 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
 2、如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
 3、 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
 4、 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

 

6、@Component、@Repository、@Service 和 @Controller

@Service:用于标注业务层组件;

@Controller:用于标注控制层组件;

@Repository:标注数据访问组件,及数据层组件;

@Component:泛指组件。

spring2.5引入了组件自动扫描机制,在类路径下寻找标注了上述注解的类,并把这些类纳入到spring容器中管理。

1)<context:annotation-config/>

是对标记了spring的@Component、@Repository等注解的类进行对应的操作使注解自动生效。

隐式地向spring容器注册:AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor和

RequiredAnnotationBeanPostProcessor。

     如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。

     如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

     如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下:

  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> 

     如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:

  <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 

方式

2)<context:component-scan/>

其功能和上面的annotation-config是一样的。对指定的类包进行扫描以实施注释驱动bean定义的功能,同时隐式地向spring容器注册。<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。<context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

das

 

7、@Required:注解检查,但只能检查属性是否已经设置而不会测试属性是否为空。只能设置在setter方法上。

在spring2.5之前,需要在配置文件中加上:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 

但在之后,可以加上<context:annotation-config/>的简写形式。

 

8、@Qualifier:当有多个候选bean时,通过这个注解来选定指定的实现方法。

原创粉丝点击