Spring IoC笔记

目录

1.什么是 IoC?

2.IoC类注解(五大注解)

2.1那为什么要这么多类注解? 

2.2五大注解是不是可以混用?

2.3程序被spring管理的条件是?

3.bean对象

3.1Bean 命名约定

3.2获取bean对象

4.⽅法注解 @Bean


1.什么是 IoC?

IoC: Inversion of Control (控制反转),是Spring的核⼼思想
什么是控制反转呢?
获得依赖对象的过程被反转了
也就是说, 当需要某个对象时, 传统开发模式中需要⾃⼰通过 new 创建对象, 现在不需要再进⾏创 建, 把创建对象的任务交给容器, 程序中只需要依赖注⼊ (Dependency Injection,DI)就可以了. 这个容器称为:IoC容器. Spring是⼀个IoC容器, 所以有时Spring 也称为Spring 容器。
通过上述我们还可以知道,Spring 是什么?
Spring 是包含了众多⼯具⽅法的 IoC 容器。

2.IoC类注解(五大注解)

前提: 

1.五大类注解:@Controller、@Service、@Repository、@Component、@Configuration 
2.bean对象:在spring容器中存放的对象。
3.ApplicationContext: 翻译过来就是: Spring 上下⽂。这个上下⽂, 就是指当前的运⾏环境, 也可以看作是⼀个容器。故ApplicationContext的对象中存放了所有与当前的运⾏环境有关的内容,比如 spring容器中存放的bean对象。

 它们的功能都是把对象交给spring管理。

2.1那为什么要这么多类注解? 

与应⽤分层呼应,让程序员看到类注解之后,就能直接了解当前类的⽤途。

(1)@Controller:控制层, 接收请求, 对请求进⾏处理, 并进⾏响应.  

(2)@Servie:业务逻辑层, 处理具体的业务逻辑 

(3)@Repository:数据访问层,也称为持久层. 负责数据访问操作  

(4)@Configuration:配置层. 处理项⽬中的⼀些配置信息. 

(5)@Component 是⼀个元注解,也就是说可以注解其他类注解

@Controller , @Service , @Repository ,@Configuration.这些注解被称为 @Component 的衍⽣注解。因为这些注解⾥⾯都有⼀个注解 @Component。

2.2五大注解是不是可以混用?

答:可以,但不是完全可以。

功能上:@Service , @Repository ,@Configuration,@Component 可以完全混用,@Controller有自己的特殊性。

规范上:不可以混用。因为我们想要与应⽤分层呼应。

2.3程序被spring管理的条件是?

 1.要被spring扫描到(默认路径是启动类所在的目录,包括子目录)

手动设置:

@ComponentScan(basePackages = "~~~~~")

2.需要配置五大注解和@Bean


3.bean对象

 1.   Object getBean(String name) throws BeansException;

 2.   <T> T getBean(String name, Class<T> requiredType) throws BeansException;

 3.   Object getBean(String name, Object... args) throws BeansException;

 4.   <T> T getBean(Class<T> requiredType) throws BeansException;

 5.   <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

 常⽤的是上述1,2,4种, 这三种⽅式,获取到的bean是⼀样的

其中1,2种都涉及到根据名称来获取对象. bean的名称是什么呢?

3.1Bean 命名约定

程序开发⼈员不需要为bean指定名称(BeanId), 如果没有显式的提供名称(BeanId),Spring容器将为该 bean⽣成唯⼀的名称.

1.一般命名约定使⽤Java标准约定作为实例字段名. 也就是说,bean名称以⼩写字⺟开头,然后使⽤驼峰式 ⼤⼩写。

2.特殊情况,当有多个字符并且第⼀个和第⼆个字符都是⼤写时, 将保留原始的⼤⼩写. 这些规则 与java.beans.Introspector.decapitalize (Spring在这⾥使⽤的)定义的规则相同.

3.使用@Bean注解的话,对象名就是添加@Bean注解方法的名称。

3.2获取bean对象

 使⽤ @Controller 存储 bean 的代码如下:

@Controller
public class UserController {

    public void sayHi(){
        System.out.println("UserController Hi");
    }
}

启动类: 

import com.wh.ioc.Controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringIocApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
        UserController bean1 = context.getBean(UserController.class);
        bean1.sayHi();
        UserController bean2 = (UserController) context.getBean("userController");
        bean2.sayHi();
        UserController bean3 = context.getBean("userController", UserController.class);
        bean3.sayHi();
    }

}

4.⽅法注解 @Bean

类注解是添加到某个类上的, 但是存在两个问题:
1.使⽤外部包⾥的类, 没办法添加类注解
2.⼀个类, 需要多个对象, ⽐如多个数据源

⽅法注解 @Bean很好的解决了这两点。 

⽅法注解要配合类注解使⽤
在 Spring 框架的设计中,⽅法注解 @Bean 要配合类注解才能将对象正常的存储到 Spring 容器中。
外部类:Student
import lombok.Data;

@Data
public class Student {
    private String name;
    private Integer id;

    public Student() {

    }

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

    public Student(String name, Integer id) {
        this.name = name;
        this.id = id;
    }
}
BeanConfig类:
import com.wh.ioc.Model.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

    @Bean
    public Student StudentInfo() {
        return new Student("wh",01);
    }
    @Bean
    public Student StudentInfo2() {
        return new Student("Bob",02);
    }
}

启动类:

import com.wh.ioc.Controller.UserController;
import com.wh.ioc.Model.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringIocApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
        Student student = (Student) context.getBean("StudentInfo");
        System.out.println(student);
        Student student2 = (Student) context.getBean("StudentInfo2");
        System.out.println(student2);
    }
}

以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值