栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring快速入门-注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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、导入依赖
 
    
        org.springframework
        spring-webmvc
        5.3.3
    
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、方式一



    
    
    

测试
@Test
    public void test5(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student);
    }
    
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);
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389846.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号