在Spring中,有许多高效的注解,其简化了开发并提高代码可读性,这样我们就不用再去spring.xml文件中写标签了非常方便

创建对象的注解

在Spring,有用于识别不同类型的Bean,使得Spring容器可以自动管理这些Bean的创建和生命周期

@Component

  • 作用:将一个类标记为Spring容器管理的Bean,Spring会自动扫描并创建该对象

  • 用法:在类上使用@Component注解,Spring会自动注册为一个Bean

    //默认将id名改为小写
    //可使用value属性修改
    //@Component(value = "a")
    //@Component("s")
    @Component
    public class Admin implements Serializable {
    ...
    }
  • 配置方法

    在配置类或XML配置文件中启用组件扫描(@ComponentScan<context:componet-scan>标签

    • Java配置方法

      @Configuration
      @ComponentScan(basePackages = "com.example")
      public class AppConfig {
      }
    • XML配置方法

          <!--Spring会扫描base-package路径下的所有类-->
      <context:component-scan base-package="com.mashang"></context:component-scan>

@Service

  • 作用:@Service@Component的派生注解,主要用于业务层的类(ServiceImp),表示该类是一个服务组件,其更加见名知意,表明是服务层的Bean

  • 用法:与@Component用法相同

  • 配置:与@Component相同使用配置类或XML文件

    @Service("adminService")
    public class AdminServiceImpl implements AdminService {
    ...
    }

@Repository

  • 作用:@Repository是@Component的另一个派生注解,用于标记数据访问层(DAO)

  • 用法:与@Service一致,特别的是@Repository是针对持久化层的

    @Repository
    public class UserRepository {
    ...
    }
  • 配置方法:在配置类中启用组件扫描,或者使用 XML 配置

@Controller

  • 作用:@Controlller是用于标记Spring MVC控制器类的注解.其表明该类是一个Web控制器.用于处理请求返回视图

  • 用法:与Spring MVC一起使用,用于处理HTTP请求

    @Controller
    public class UserController {
    ....
    }

注入对象的注解

@Autowired

@Autowired是最常用的注解,用于将Spring容器中的Bean自动注入到当前类中

@Service("adminService")
public class AdminServiceImpl implements AdminService {
@Autowired
private AdminDao adminDao;
}

在需要注入的字段上添加@Autowired即可

@Qualifier

  • @Qualifier用于与@Autowired一起使用,在存在多个类型相同的Bean时,即可提供@Qualifier来指定具体注入的Bean
@Repository
public class ClassDao1 extends ClassDao{}
@Repository
public class ClassDao2 extends ClassDao{} @Autowired
@Qualifier("classDao1")
private ClassDao classDao; public void queryAll() {
dao.queryAll();
System.out.println(classDao);
}

@Resource

@Resource可以注入Bean,但通常是按照名称注入,其与@Autowired有相似之处,但在注入方式上有所不同

按名称注入

  • 这是@Resource的默认注入方式,其会查找容器中释放有名称与目标属性名相同的Bean,并将其注入

    public class AdminServiceImpl implements AdminService {
    @Resource(name = "adminDao")
    private AdminDao adminDao;
    }

在这个例子中,@Resource(name = "adminDao") 会根据 adminDao 的名字注入对应的 AdminDao 实现类

按属性注入

  • 若没有指定name属性,@Resource会根据属性的类型自动注入,也就是说,如果@Resource,注解的目标与容器中某个类型匹配,其会自动注入该类型的Bean

    public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminDao adminDao;
    }

    这个例子中,若容器中存在AdminDao类型的Bean,Spring会自动注入到adminDao属性中

@Resource@Autowired的差异

  • @Autowired 默认按类型注入,可使用@Qualifier来指定特定Bean名称,解决滴哦共和Bean的问题
  • @Resource 默认按名称注入,如果为指定name,则按类型进行注入.

@PostConstruct@PreDestroy

两个注解分别为再Bean初始之后和销毁之前执行的方法

  • 用法:

    • @PostConstruct:当Bean再创建并且所有依赖注入完成之后,调用该方法
    • @PreDestroy:在Bean销毁之前调用此方法
    @Component
    public class UserService { @PostConstruct
    public void init() {
    System.out.println("UserService is initialized");
    } @PreDestroy
    public void destroy() {
    System.out.println("UserService is destroyed");
    }
    }

其他的配置注解

@Configuration@ComponentScan

  • @Configuration的作用:使用这个注解标识的类表明,这个类是一个Spring的配置类,这种配置类相比于传统的使用XML文件配置文件来说,Spring配置类通常包括Bean的定义,和配置Bean的方法并将其注册到Spring容器中
  • @ComponentScan的作用:用于指定在Spring初始化容器时应该扫描的包,并将包中的类注册为Bean.其作用类似于XML文件中的<context:component-scan/>标签
@Configuration
@ComponentScan("com.mashang")
//@ComponentScan({"com.mashang"})配置多个路径的方法
public class SpringConfig { }

@Import

  • 作用:该注解可以用于将其他配置类引入到当前配置类中,相当于将另一个配置类的内容导入当前类,可以导入多个配置类
@Configuration
@ComponentScan("com.mashang")
@Import({DruidConfig.class, Test01.class})
public class SpringConfig { }

@PropertySource@Value

@PropertySource

  • 作用:@PropertySource用于配置Spring要加载的.properties文件
@Component
@PropertySource("classpath:druid.properties")
public class JDBCUtil {
...
}
  • classpath:用于接收改文件位于项目内的类路径

@Value

  • 作用:用于将配置文件中的值注入到Spring Bean字段,方法参数或构造函数中.通常与@PropertySource一起使用,通过${}语法引用.properties中的键值对

    Component
    @PropertySource("classpath:druid.properties")
    public class JDBCUtil {
    @Value("${driverClassName}")
    private String driver; @Value("${url}")
    private String url; @Value("${accountName}")
    private String accountName; @Value("${password}")
    private String password; }

@Bean

  • 作用:@Bean注解是作用是一个方法上的,被标识的方法的返回值会被注册为Spring Bean,并放入到容器中.
  • 属性:name属性用于指定改Bean的id,若不指定默认为方法名
@Bean
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, accountName, password);
}

@Bean 注解的方法 getConnection() 返回的 Connection 对象会被注册为 Spring 容器中的 Bean,你可以通过 ctx.getBean() 来获取它

Spring常用注解介绍的更多相关文章

  1. Spring常用注解介绍【经典总结】

    Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...

  2. SpringBoot+Spring常用注解总结

    为什么要写这篇文章? 最近看到网上有一篇关于 SpringBoot 常用注解的文章被转载的比较多,我看了文章内容之后属实觉得质量有点低,并且有点会误导没有太多实际使用经验的人(这些人又占据了大多数). ...

  3. [刘阳Java]_Spring常用注解介绍_第6讲

    Spring的注解是在Spring2.5的版本中引入的,目的简化XML配置.在企业开发过程中使用注解的频率非常高,但是学习注解的前提是大家一定要对Spring基于XML配置要熟悉,这是我个人建议,因为 ...

  4. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  5. Spring常用注解总结

    转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...

  6. Spring注解 系列之Spring常用注解总结

    参考:Spring系列之Spring常用注解总结 (1) Resource 默认是byName的方式进行bean配置,@AutoWired默认是按照byType的方式进行装配bean的:(2)Comp ...

  7. Spring系列之Spring常用注解总结 转载

    Spring系列之Spring常用注解总结   传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...

  8. spring常用注解笔记

    spring常用注解解释: 1. Mybatis的映射文件xxxMapper.xml中resultMap标签的作用 resultMap标签是为了映射select查询出来结果的集合,其主要 作用是将实体 ...

  9. SpringBoot | 第六章:常用注解介绍及简单使用

    前言 之前几个章节,大部分都是算介绍springboot的一些外围配置,比如日志配置等.这章节开始,开始总结一些关于springboot的综合开发的知识点.由于SpringBoot本身是基于Sprin ...

  10. SG-UAP常用注解介绍

    注解基本介绍 Annotation(注解)是JDK5.0及以后版本引入的.它可以用于创建文档,跟踪代码中的依赖性,甚至执行基本编译时检查.注解是以‘@注解名’在代码中存在的,根据注解参数的个数,我们可 ...

随机推荐

  1. 低功耗4G模组Air780E快速入门:固件的远程升级

    ​ 今天我们学习Air780E快速入门之固件的远程升级,小伙伴们,学起来吧! 一.生成差分包 合宙的远程升级支持使用合宙云平台和自建服务器,此例程使用的是合宙云平台. 1.1 准备新旧版的core和脚 ...

  2. 『玩转Streamlit』--交互类组件

    交互类组件在Web应用程序中至关重要,它们允许用户与应用进行实时互动,能够显著提升用户体验. 用户不再只是被动地接收信息,而是可以主动地输入数据.做出选择或触发事件,从而更加深入地参与到应用中来. 此 ...

  3. json数据按照某一个相同键值进行分类成一个新的二维json数组

    1 formatTreeData(checkNodes){ 2 var map = {}, 3 targetData = []; 4 checkNodes.forEach(item => { 5 ...

  4. 解读vue的webpack.base.conf.js配置

    'use strict' // 引入nodejs路径模块 const path = require('path') // 引入utils工具模块,utils主要用来处理css-loader和vue-s ...

  5. 代码质量审查工具之SonarQube8.9(LTS)与gitlab CI集成使用

    官网地址: https://docs.sonarqube.org/8.9/analysis/scan/sonarscanner/ 目标:在push时自动触发GitLab CI/CD pipeline ...

  6. pyc文件花指令

    pyc花指令 常见的python花指令形式有两种:单重叠指令和多重叠指令. 以下以python3.8为例,指令长度为2字节. 单重叠指令: 例如pyc经过反编译后得到的东西为 0 JUMP_ABSOL ...

  7. Windows安装redis并将redis设置成服务开机自启

    Redis 作为一种缓存工具,主要用于解决高并发的问题,在分布式系统中有着极其广泛的应用,Redis 本身是应用于 Linux/Unix 平台的(部署在服务器上边),官方并没有提供 Windows 平 ...

  8. CentOS 更换国内源

    前言 centos默认为官方yum源,国内使用的下载速度完全随缘,且不稳定,一般都需要更换国内源 过程 了解版本 通过cat /etc/redhat-release 可以获取当前centos的版本 知 ...

  9. 用word发博客

    测试 标题一 第一段文字 测试图片 标题二 Test test

  10. Element Plus组件库el-select组件多选回显踩坑

    前情 公司有经常需要做一些后台管理页面,我们选择了Element Plus,它是基于 Vue 3,面向设计师和开发者的组件库,是Vue框架生态中比较火的UI组件库,组件库丰富易用,组件链接:一个 Vu ...