**@Override**表示这个方法来自父类(接口)的

注解都是以@开头的,后面加上名字;其实注解也是一个类

  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.SOURCE)
  3. public @interface Override {
  4. }
  1. @interface: 表示注解类型的接口
  2. @Target: 这个注解可以用在那里 ElementType.METHOD: 方法上
  3. RetentionPolicy.SOURCE: 运行时可以用在源代码上使用的

基于注解的di

通过注解完成java对象创建,属性赋值。

使用步骤
  • 1.加入Maven依赖:spring-context,在你加入spring-context的同时,它会间接的加入spring-aop的依赖。使用注解必须使用**spring-aop**依赖

image.png

  • 2.在类中加入spring的注解(多个不同功能的注解)
  • 3.在spring配置文件中,加入一个组件扫描器的标签,说明注解在你的项目中的位置

    学习的注解
  • @Component

  • @Respotory
  • @Service
  • @Controller
  • @Value
  • Autowired
  • @Resource
    pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.chentianyu</groupId>
  <artifactId>SpringLearnOnetwo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>SpringLearnOnetwo</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <!--单元测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.16</version>
    </dependency>

    <!--mybatis依赖-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.9</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.28</version>
    </dependency>
  </dependencies>

  <build>

  </build>
</project>