Spring @Autowired 注解用法解释


开启Spring服务的时候,它会根据声明先扫描配置文件中的bean(注解配置同理),
再根据bean中类的全限定名去寻找它,一般为我们自己声明的pojo(实体类),
然后这时如果有@Autowired注解则会根据类型寻找其它的被Spring托管的bean进行自动注入,
调用默认的构造函数

一.pojo

1.1.Cat

package com.kuang.pojo;

public class Cat {
    public void shout(){
        System.out.println("miao~");
    }
} 

1.2.Dog

package com.kuang.pojo;

public class Dog {
    public void shout(){
        System.out.println("wang~");
    }
}

1.3.People

package com.kuang.pojo;


import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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


    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

1.4.beans.xml

<?xml version="1.0" encoding="UTF-8"?>


    
        
        

        

1.5.MyTest

import com.kuang.pojo.Cat;
import com.kuang.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();

        Cat cat = new Cat();
        People people1 = new People();
        people1.setCat(cat);
        people.getCat().shout();
        System.out.println(people1.toString());

    }
}

二.Spring Boot 注入接口 @Autowired interface

2.1.VideoService

package com.kuang.services;

public interface VideoService {
    String getVideoName();
}

2.2.VideoServiceImpl

package com.kuang.services;

import org.springframework.stereotype.Service;

@Service
public class VideoServiceImpl implements VideoService {

    public String getVideoName() {
        return "三生三世十里桃花";
    }
}

2.3.HelloController 

    package com.kuang.controller;

    import com.kuang.services.VideoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    @RestController
    public class HelloController {

        @Autowired
        private VideoService videoService;

        @RequestMapping("/video")
        public String videoName(){
            System.out.println(videoService.getVideoName());//三生三世十里桃花
            return videoService.getVideoName();
        }

    }

三.关于Could not autowire问题的解决

具体的原因可能有两个:
1.IntellijIDEA自身级别检测导致的问题
2.导入@Service包的时候导错包

四. Error creating bean with name 解决

报错原因:

  1. 未开启注解扫描;
  2. 未设置注解扫描的包路径;
  3. DAO、Controller、Service层中相应的注解是否加上;
  4. 某个Spring容器托管的类你写成了抽象类,即abstract Class,抽象类是无法new的;
  5. IDEA是否开启了Buid Automaically(如果未开启自动编译,需要自己手动编