spring常用注解

Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式。
Spring注解方式减少了配置文件内容,更加便于管理,并且使用注解可以大大提高了开发效率!
下面按照分类讲解Spring中常用的一些注解。

一: 组件类注解

思考:Spring怎么知道应该把哪些Java类当成bean注册到容器中呢?

答案:使用配置文件或者注解的方式进行标识需要处理的java!

1、注解类介绍


@Component :标准一个普通的spring Bean类。
@Repository:标注一个DAO组件类。
@Service:标注一个业务逻辑组件类。
@Controller:标注一个控制器组件类。

这些都是注解在平时的开发过程中出镜率极高,@Component@Repository@Service@Controller实质上属于同一类注解,用法相同,功能相同,区别在于标识组件的类型。@Component可以代替@Repository@Service@Controller,因为这三个注解是被@Component标注的。如下代码

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Controller {

    String value() default "";

}

2、举例详解

1)当一个组件代表数据访问层(DAO)的时候,我们使用@Repository进行注解,如下

@Repositorypublic class HappyDaoImpl implements HappyDao{private final static Logger LOGGER = LoggerFactory.getLogger(HappyDaoImpl .class);public void  club(){

        //do something ,like drinking and singing

    }

  • }

2)当一个组件代表业务层时,我们使用@Service进行注解,如下

@Service(value="goodClubService")//使用@Service注解不加value ,默认名称是clubServicepublic class ClubServiceImpl implements ClubService {

    @Autowired

    private ClubDao clubDao;

    public void doHappy(){

        //do some Happy

    }

  • }

3)当一个组件作为前端交互的控制层,使用@Controller进行注解,如下

@Controllerpublic class HappyController {

    @Autowired //下面进行讲解

    private ClubService clubService;

    // Control the people entering the Club

    // do something

}/*Controller相关的注解下面进行详细讲解,这里简单引入@Controller*/

  • 1

3、总结注意点


1、被注解的java类当做Bean实例,Bean实例的名称默认是Bean类的首字母小写,其他部分不变。@Service也可以自定义Bean名称,但是必须是唯一的!


2、尽量使用对应组件注解的类替换@Component注解,在spring未来的版本中,@Controller@Service@Repository会携带更多语义。并且便于开发和维护!


3、指定了某些类可作为Spring Bean类使用后,最好还需要让spring搜索指定路径,在Spring配置文件加入如下配置:

<!-- 自动扫描指定包及其子包下的所有Bean类 --><context:component-scan base-package="org.springframework.*"/>

二:装配bean时常用的注解

1、注解介绍


@Autowired:属于Spring org.springframework.beans.factory.annotation包下,可用于为类的属性、构造器、方法进行注值
@Resource:不属于spring的注解,而是来自于JSR-250位于java.annotation包下,使用该annotation为目标bean指定协作者Bean
@PostConstruct @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

2、举例说明

1):@Autowired

@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Autowired {

    boolean required() default true;

  • }

@Controllerpublic class HappyController {

    @Autowired //默认依赖的ClubDao 对象(Bean)必须存在

    //@Autowired(required = false) 改变默认方式

    @Qualifier("goodClubService")

    private ClubService clubService;

    // Control the people entering the Club

    // do something

  • }

2):@Resource

@Target({TYPE, FIELD, METHOD})

@Retention(RUNTIME)

public @interface Resource {

 String name() default "";

 Class type() default java.lang.Object.class;

 ...

public class AnotationExp {

    @Resource(name = "HappyClient")

    private HappyClient happyClient;

    @Resource(type = HappyPlayAno .class)

    private HappyPlayAno happyPlayAno;

}

3、总结

1):相同点

@Resource的作用相当于@Autowired,均可标注在字段或属性的setter方法上。

2):不同点

a提供方 @AutowiredSpring的注解,@Resourcejavax.annotation注解,而是来自于JSR-250J2EE提供,需要JDK1.6及以上。
b 注入方式 @Autowired只按照Type 注入;@Resource默认按Name自动注入,也提供按照Type 注入;
c属性 
@Autowired注解可用于为类的属性、构造器、方法进行注值。默认情况下,其依赖的对象必须存在(bean可用),如果需要改变这种默认方式,可以设置其required属性为false
还有一个比较重要的点就是,@Autowired注解默认按照类型装配,如果容器中包含多个同一类型的Bean,那么启动容器时会报找不到指定类型bean的异常,解决办法是结合@Qualified注解进行限定,指定注入的bean名称。
@Resource有两个中重要的属性:nametypename属性指定byName,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。
需要注意的是,@Resource如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。

d@Resource注解的使用性更为灵活,可指定名称,也可以指定类型 ;@Autowired注解进行装配容易抛出异常,特别是装配的bean类型有多个的时候,而解决的办法是需要在增加@Qualitied进行限定。

Spring@Autowired注解与@Resource注解的区别

注意点:使用@Resource也要注意添加配置文件到Spring,如果没有配置component-scan

<context:component-scan> <!--<context:component-scan>的使用,是默认激活<context:annotation-config>功能-->

则一定要配置 annotation-config

<context:annotation-config/>

三:@Component vs @Configuration and @Bean

1、简单介绍


Spring的官方团队说@Component可以替代 @Configuration注解,事实上我们看源码也可以发现看到,如下

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Component  //看这里!!!public @interface Configuration {

    String value() default "";

虽然说可以替代但是两个注解之间还是有区别的!

Bean注解主要用于方法上,有点类似于工厂方法,当使用了@Bean注解,我们可以连续使用多种定义bean时用到的注解,譬如用@Qualifier注解定义工厂方法的名称,用@Scope注解定义该bean的作用域范围,譬如是singleton还是prototype等。

Spring 中新的 Java 配置支持的核心就是@Configuration 注解的类。这些类主要包括 @Bean 注解的方法来为 Spring IoC 容器管理的对象定义实例,配置和初始化逻辑。

使用@Configuration 来注解类表示类可以被 Spring IoC 容器所使用,作为 bean 定义的资源。

@Configurationpublic class AppConfig {

    @Bean

    public MyService myService() {

        return new MyServiceImpl();

    }

这和 Spring XML 文件中的非常类似

<beans>

    <bean id="myService" class="com.acme.services.MyServiceImpl"/></beans>

@Bean 注解扮演了和元素相同的角色。

2、举例说明@Component 和 @Configuration

@Configurationpublic static class Config {

    @Bean

    public SimpleBean simpleBean() {

        return new SimpleBean();

    }

    @Bean

    public SimpleBeanConsumer simpleBeanConsumer() {

        return new SimpleBeanConsumer(simpleBean());

    }

}

@Componentpublic static class Config {

    @Bean

    public SimpleBean simpleBean() {

        return new SimpleBean();

    }

    @Bean

    public SimpleBeanConsumer simpleBeanConsumer() {

        return new SimpleBeanConsumer(simpleBean());

    }

}

第一个代码正常工作,正如预期的那样,SimpleBeanConsumer将会得到一个单例SimpleBean的链接。第二个配置是完全错误的,因为Spring会创建一个SimpleBean的单例bean,但是SimpleBeanConsumer将获得另一个SimpleBean实例(也就是相当于直接调用new SimpleBean() ,这个bean是不归Spring管理的),既new SimpleBean() 实例是Spring上下文控件之外的。

3、原因总结


使用@ configuration,所有标记为@ bean的方法将被包装成一个CGLIB包装器,它的工作方式就好像是这个方法的第一个调用,那么原始方法的主体将被执行,最终的对象将在spring上下文中注册。所有进一步的调用只返回从上下文检索的bean。

在上面的第二个代码块中,新的SimpleBeanConsumer(simpleBean())只调用一个纯java方法。为了纠正第二个代码块,我们可以这样做

@Componentpublic static class Config {

    @Autowired

    SimpleBean simpleBean;

    @Bean

    public SimpleBean simpleBean() {

        return new SimpleBean();

    }

    @Bean

    public SimpleBeanConsumer simpleBeanConsumer() {

        return new SimpleBeanConsumer(simpleBean);

    }

}

Spring @Configuration vs @Component 
基本概念:@Configuration @Bean

原文地址:https://www.cnblogs.com/z--z/p/9565874.html