spring快速入门-注解

前言

什么是注解?

用一个词就可以描述注解,那就是元数据,即一种描述数据的数据。所以,可以说注解就是源代码的元数据。可以理解为一种标记。

为什么要引入注解?

简化开发

需要用到的注解

@Component
@Service
@Controller
@Reposity
上面四个注解都是用于标记Bean的实体类,用于spring容器能够在创建Bean时,能找到对应的实体类进行Bean的初始化,其中@component的作用范围最大


@Autowired 自动装配通过类型,名字

@Autowired顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property。当然,getter看个人需求,如果私有属性需要对外提供的话,应当予以保留。@Autowired默认按类型匹配的方式,在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中。

如果不唯一通过@Qualifier获取

@Resource: 自动装配通过名字,类型
 
@Configuration 标记该类为配置类
@ComponentScan 扫描指定类或包

流程

1、导入依赖

 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.3</version>
    </dependency>

2、创建实体类

package pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Teacher {
    @Value("Jerry")//动态将值注入
    private String name;
    @Value("12")
    private Integer age;

    public Teacher() {
    }

    public Teacher(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package pojo;

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

@Component
public class Student {
    @Value("tom")
    private String name;
    @Value("12")
    private Integer age;
    private Teacher teacher;

    public Student() {
    }

    @Autowired
    @Qualifier("teacher1")
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", teacher=" + teacher +
                '}';
    }
}

3、方式一

<?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 https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="pojo"/>
    <bean id="student" class="pojo.Student"/>
    <bean id="teacher1" class="pojo.Teacher"/>
</beans>
测试
@Test
    public void test5(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student);
    }
    /**
    output:  Student{name='tom', age=12, teacher=Teacher{name='Jerry', age=12}}
    **/

4、方式二

不使用配置文件

  • 创建注解类

  • package config;
    
    import method.UserDAO;
    import method.UserDAOImpl;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import pojo.Student;
    import pojo.Teacher;
    
    @Configuration
    @ComponentScan({"pojo","method"})
    public class springConfig {
        @Bean
        public UserDAO getUserDAO(){
            return new UserDAOImpl();
        }
        @Bean
        public Student student(){
            return new Student();
        }
        @Bean
        public Teacher teacher1(){
            return new Teacher();
        }
    
    }
    
@Test
    public void test4(){
        BeanFactory context = new AnnotationConfigApplicationContext(springConfig.class);
        Student student = context.getBean("student", Student.class);
        System.out.println(student);
    }
/**
output:  Student{name='mike', age=12, teacher=Teacher{name='john', age=55}}
**/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值