@Component—@Autowired—@Mapper—@Bean

注解详解

  • 一、@Component
  • 二、@Autowired
        • 注解支持
          • context:annotation-config——手动注入bean
          • context:component-scan base-package="zy.pojo"——扫描包并自动注入
          • 总结
  • 三、@Mapper
    • @Mapper与@Repository
  • 四、@Bean
    • @Bean与@Component区别


一、@Component

  • 作用:表明了此类为一个组件类,是一个标志,代表可以被Spring注入。
  • 作用域:类上
  • Spring的注解

四个表示组件标志的注解:

  • @Component
  • @Repository(“名称”):dao层 ——自定义bean的id
  • @Service(“名称”):service层
  • @Controller(“名称”):web层

二、@Autowired

  • 作用:在一个类中注入使用一个对象。eg:People中的dog、工具类

两个自动注入的注解

  • @Autowired:根据类型注入——ByType
  • @Resource(“名称”):根据名称注入——ByName

可与@Qualifier搭配使用,指定对应的Bean实现注入

    @Autowired
    @Qualifier(name="userDao1")    
    private UserDao userDao; 
注解支持
context:annotation-config——手动注入bean
  • 作用:可识别@Autowired注解。
  • Beans.xml中手动注册Bean,类上不需要有注解@Component
  • Peopler中Dog,Cat上的@Autowired——表示将两个类作为组件自动注入到People类中

@Autowired放在属性上,不会调用set方法

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    
    public People() {}
    public People(Cat cat, Dog dog) {
        this.cat = cat;
        this.dog = dog;
    }
    public void setCat(Cat cat) {this.cat = cat;}
    public void setDog(Dog dog) {this.dog = dog;}
    public Dog getDog() {return dog;}
    public Cat getCat() {return cat;}
    public void life(){
        System.out.println("人看着宠物吃饭");
        dog.eat();
        cat.eat();
    }
}
context:component-scan base-package=“zy.pojo”——扫描包并自动注入
  • 作用:扫描指定路径的注解,可识别@Component,@Autowired等,并将类自动注入到Spring IOC容器中。
  • 三个类均有注解@Component——表示为一个组件
  • Peopler中Dog,Cat上的@Autowired——表示将两个类作为组件自动注入到People类中
  • Beans.xml中不用手动注入Bean
@Component("dog222")
public class Dog {
@Component
public class Cat {
@Component
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
   
    public People() {}
    public People(Cat cat, Dog dog) {
        this.cat = cat;
        this.dog = dog;
    }
    public void setCat(Cat cat) {this.cat = cat;}
    public void setDog(Dog dog) {this.dog = dog;}
    public Dog getDog() {return dog;}
    public Cat getCat() {return cat;}
    public void life(){
        System.out.println("人看着宠物吃饭");
        dog.eat();
        cat.eat();
    }
}

测试类

public class test01 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        People people = applicationContext.getBean("people", People.class);
        people.life();
    }
}
总结

context:annotation-config

  1. 仅能识别@Component注解
  2. 需要手动注入Bean

context:component-scan base-package="zy.pojo"

  1. 可识别@Component,@Autowired注解
  2. 自动注入,不需要手动注入Bean

三、@Mapper

  • @Mapper 是 Mybatis 的注解
  • 作用:在 Spring 程序中,在接口上添加@Mapper:Mybatis 需要找到对应的 mapper,在编译的时候动态生成代理类,实现数据库查询功能。
  • 作用域:方法上

麻烦的是,若有多个mapper接口,需要在每个mapper上添加@Mapper
——————解决方法:在主配置类上添加**@MapperScan注解**,扫描指定包下的所有mapper。不用在每个Mapper接口中添加注解。

@SpringBootApplication
@MapperScan("com.zy.mapper")
public class SpringBoot04JdbcApplication {

但是当我们只使用@Mapper注解,在实现类注入时会出现红线爆红——————因为@Autowired时系统没有认出这是一个组件,需要添加@Repository注解,说明这是一个组件,可以被自动注入。

@Mapper与@Repository

相同点:都作用在dao层接口,使得其生成代理对象bean,交给spring 容器管理。

不同点

  • Mapper不需要配置扫描地址,可以单独使用,如果有多个mapper文件的话,可以在项目启动类中加入@MapperScan(“mapper文件所在包”),这样就不需要每个mapper文件都加@Mapper注解
  • @Repository不可以单独使用,需要配置扫描地址

总结

  • @Mapper 一定要有,否则 Mybatis 找不到 mapper。
  • @Repository 可有可无,有的话可以消去依赖注入的报错信息。
  • @MapperScan 可以替代 @Mapper。

四、@Bean

  • 作用:表示这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。通常方法体中包含了最终产生bean实例的逻辑(自定义bean产生的过程)。
  • 作用于:方法上

@Bean与@Configuration成对出现

  • @Configuration修饰类,表明当前类为一个配置类,相当于Spring.xml,Spring需要扫描去这个类
  • @Bean修饰方法,方用来自定义类的生成过程,返回类的实体对象作为bean。
@Configuration  // 该类 = beans.xml
@ComponentScan("zy.pojo")  // 扫描该包下的注解
public class MyConfig {
    @Bean
    // id = 方法名
    // return + class
    public User user(){
        return new User();
    }
}

@Bean与@Component区别

  • @Component 作用于类,@Bean作用于方法。
  • @Component和@Bean都是用来注册Bean并装配到Spring容器中,但是Bean比Component的自定义性更强。可以实现一些Component实现不了的自定义加载类。

你可能感兴趣的:(零碎知识点,bean,java,mybatis,spring,component)