spring常用注解

     2022-04-10     730

关键词:

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)@[email protected] @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)@[email protected]  //看这里!!!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




































spring常用注解

...y在数据访问层(dao层)使用。@Controller在展现层(MVC-->SpringMVC)使用注入Bean的注解,一般情况下通用@Autowired:Spring提供的注解@Inject:JSR-330提供的注解@Resource:JSR-250提供的 查看详情

spring注解的实现原理和spring常用注解介绍

1.【Spring如何使用注解机制完成自动装配】Java实例构造时会调用默认父类无参构造方法,Spring正是利用了这一点,让"操作元素的代码"得以执行。【两种处理策略】(1)类级别的注解:如@Component、@Repository、@Controller、@Service以及Jav... 查看详情

spring常用注解

spring常用注解@Controller:控制层,里面有多个连接@Service:业务层,一般对于接口和实现,声明是一个业务处理类(实现类非接口类)@Qualifier:如果一个接口有多个实现,那么注入时候加上唯一标示,为Bean指定名称,随后再通过名... 查看详情

spring12种常用注解!

Java必须掌握的12种Spring常用注解! 1.声明bean的注解  @Component组件,没有明确的角色  @Service在业务逻辑层使用(service层)  @Repository在数据访问层使用(dao层)  @Controller在展现层使用,控制器的声明(C)2.注入bean... 查看详情

ssm框架常用注解(代码片段)

ssm框架常用注解一:常用注解1.1mybatis常用注解1.2spring常用注解1.3springMVC常用注解一:常用注解1.1mybatis常用注解1.2spring常用注解1.创建当前对象交给spring容器管理的注解2.属性依赖注入的注解3.生命周期相关的注解4.使用配... 查看详情

ssm框架常用注解(代码片段)

ssm框架常用注解一:常用注解1.1mybatis常用注解1.2spring常用注解1.3springMVC常用注解一:常用注解1.1mybatis常用注解1.2spring常用注解1.创建当前对象交给spring容器管理的注解2.属性依赖注入的注解3.生命周期相关的注解4.使用配... 查看详情

spring注解之实体类常用注解

importorg.hibernate.annotations.AccessType;importorg.hibernate.annotations.*;importjavax.persistence.*;importjavax.persistence.Entity;@SelectBeforeUpdate(value=true)//指明Hibernate从不运行SQLUpdate& 查看详情

spring常用注解解析(代码片段)

1.常用注解1.1@Configuration@ConfigurationpublicclassMainConfig@Configuration注解表明这个类是一个配置类,该类中应该包含如何在Spring应用上下文中创建bean的细节1.2@ComponentScan@Configuration@ComponentScan("per.ym")publicclassMainConfig@Co 查看详情

spring常用注解总结

spring注解使用@Controller@Service(“itemService”)(如果没给(“itemService”),则对象名(被扫描的类的对象)默认是类名首字母小写)@Component@Repository以上四个注解作用相同(都是spring框架扫描类,对类进行管理使用)使用方法... 查看详情

spring常用注解

SpringMVC常用注解: @Component:通用  @Controller:controller层使用@Service:service层使用@Repository:dao层使用@PathVariable:映射URL绑定的占位符例:访问xxx/testPathVariable/1//@PathVariable可以用来映射URL中的占位符到目标方法的参数中@RequestMap... 查看详情

spring常用注解

使用注解来构造IoC容器用注解来向Spring容器注册Bean。需要在applicationContext.xml中注册<context:component-scanbase-package=”pagkage1[,pagkage2,…,pagkageN]”/>。如:在base-package指明一个包1 <context:component-scan base 查看详情

spring常用注解(收藏大全)

Spring部分 1.声明bean的注解 @Component组件,没有明确的角色@Service在业务逻辑层使用(service层)@Repository在数据访问层使用(dao层)@Controller在展现层使用,控制器的声明(C)2.注入bean的注解 @Autowired:由Spring提供@Inject:由JSR-33... 查看详情

spring常用注解及常用依赖

常用依赖<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.6.RELEASE</version></dependenc 查看详情

spring常用注解及常用依赖

常用依赖<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.6.RELEASE</version></dependenc 查看详情

spring最常用的7类注解

...迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。而spring的的注解那么多,可能做java很多年,都用不上。这里按照类型... 查看详情

spring常用注解

packagecom.itheima.service.impl;importcom.itheima.dao.IAccountDao;importcom.itheima.dao.impl.AccountDaoImpl;importcom.itheima.service.IAccountService;importorg.springframework.beans.factory.annotation 查看详情

spring中常用的注解(代码片段)

Spring中常用的注解1)@Component2)@Repository3)@Service4)@Controller5)@Autowired6)@Resource7)@QualifierSpring注解装配BeanSpring默认不使用注解装 查看详情

spring中常用的注解(代码片段)

Spring中常用的注解1)@Component2)@Repository3)@Service4)@Controller5)@Autowired6)@Resource7)@QualifierSpring注解装配BeanSpring默认不使用注解装 查看详情