spring常用注解及用法总结

https://blog.csdn.net/Song_JiangTao/article/details/81946886

引入注解的目的:

  ·当bean过多时,会引起xml文件的过于臃肿、庞大、难以维护;

  ·加强java bean的内聚度。

常用注解概述:

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

·@Controller:用于标注控制层组件(如Struts中的action);

·@Repository:用于标注数据访问组件,即DAO组件;

·@Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注;

·@Autowired:默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:

  @Autowired@Qualifier("personDaoBean")存在多个实例配合使用;

·@Resource:默认按名称装配,当找不到与名称匹配的bean才会按类型装配;


·@PreDestory:摧毁注解,默认单例,启动就加载;

·@Async:异步方法调用;

·@Configuration:把一个类作为一个IOC容器,它的某个方法头上如果注册了@Bean,就会作为这个spring容器中的Bean;

·@Scope:用于指定scope作用域的(用在类上);

·@PostConstruct:用于指定初始化方法(用在方法上);

·@PreDestory:用于指定销毁方法(用在方法上);

·@DependsOn:定义Bean初始化及销毁时的顺序;

·@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常;

·@Lazy(true):表示延迟初始化。

前面的更为常见。

详述:

在使用注解之前:需要将使用注解的类都加载进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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd" >  
<!-- 这里使用base-package来引入这个包下的所有类-->
<context:component-scan base-package="com.annotation" />
</beans>

1、@Autowired

@Autowired,顾名思义,就是自动装配,其作用是为了消除java代码里面的setter与bean属性中的property;

@Autowired,默认按类型匹配的方式,在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中。

package com.annotation.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.annotation.dao.AnnotationDao;
import com.annotation.service.AnnotationService;
@Service
@Scope("prototype")//这里的Scope和xml文件的用法一致
public class AnnotationServiceImpl2 implements AnnotationService {
    @Autowired
    private AnnotationDao ad;
    @Override
    public void p() {
        System.out.println("Service注解测试2:"+this.hashCode());
        ad.p();//加注解@Autowired就可以将dao注入到service中,同样在setter方法上,构造方法上,都可以注入成功,而不需要在xml中配置
    }
}

如上,加上注解,这样在xml中就不用配置<bean></bean>了,同样,在类上加上这个注解也可以。

假如:在xml中配置了bean而且有property(即没有注解的那种配置),但是,却没有setter方法,而给这个属性加了@Autowired,这样会成功吗?

答案是否定的,因为:Spring会按照xml优先的原则去相应的java文件中,寻找这两个属性的setter方法来配置。

2、@Scope

如上:@Scope可以指定模式,默认还是singleton,即单例模式。“prototype”表示原型,即每次都会new出一个新的出来。

3、@Service

@Service和@Controller、@Repository、@Component其实都是一样的,当你知道这个类具体是干什么的就用具体的注解,如果不知道就用@Component。(概述中已叙述清楚)

另外:这里的@Service,干了两件事:

a.声明该类是一个bean,这样才可以使用@Autowired来自动注入;

b.这个类的bean的id是类名,且首字母小写,你也可以这样做,@Service("这里是你想要的id名")。

4、@Autowired@Qualifier(“指定bean”)配套使用

这种情况是:当出现一个接口有多个实现类时来区分使用,例如下面的AnnotationService,这个接口就有两个实现类。

package com.annotation;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.annotation.service.AnnotationService;

@Component
public class QualifierResoutce {
    @Autowired
    @Qualifier("annotationServiceImpl2")
    //  @Resource(name="annotationServiceImpl2")
    public AnnotationService as;

    @Resource
    public List<AnnotationService> list;

    @Resource
    public Map<String,AnnotationService> map;

    public void p(){
        System.out.println("QualifierResoutce");
        as.p();
    }

    public void print(){
        System.out.println("-----list存放------");
        for(AnnotationService a:list){
            System.out.println(a.getClass().getName());
        }
        System.out.println("-----map存放------");
        for(Entry<String, AnnotationService> e:map.entrySet()){
            System.out.println(e.getKey()+"   "+e.getValue());
        }
    }
}

上面的@Component,就是不知道这个类是干嘛的,所以就使用这个注解;

而这两个注解的配套使用就是,自动注入这个属性,并且走的是这个属性(接口)的哪个实现类。

5、@Resource

上面也出现了这个注解,这个注解和@Autowired非常类似。

@Resource的装配顺序:

a.@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配;

b.指定了name或者type则根据指定的类型去匹配bean;

c.指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错。

@Resource和@Autowired的区别:

a.@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配;

b.@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了。

Spring属于第三方的,J2EE是java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。

6、@Controller

@Controller对应表现层的Bean,也就是Action,当使用这个注解以后,spring容器中就会有一个和这个注解的类名相同(首字母小写)的action;

也可以这样@Controller("ActionName")来指定名字;

同样,@Repository,也是这样的。

原文地址:https://www.cnblogs.com/arrows/p/10531413.html