容器:servlet 容器、集合
线程池、常量池、数据库连接池的作用类似于容器
但是他们属于设计模式中的享元模式,即从池子中取东西,如果取不到,再创建
spring 中的核心容器,类似于集合
作用
- 存放对象
- 取出来使用
在 spring 中,有些对象是由容器来创建的,并且复制管理对象间的依赖
对于对象间的依赖:A 对象的属性为 B 对象,A 和 B 就产生了依赖关系
概念:IOC,(Inversion of Control,控制反转) —— 以前自己 new 对象以及组织对象间的关系,现在全部交给容器来统一管理,控制权发送了反转,因此称为控制反转。
实现方式:使用 IOC 容器来实现
在 spring 在,称为 spring bean 容器、spring 容器或 IoC 容器
概念:DI, (Dependency Injection,依赖注入) —— 由 IoC 容器在运行期间,动态的将某种依赖关系注入到对象之中
注入
把 B 对象注入到 A ,称为 A 的属性
把 A 注入 IoC 容器中
作用
解耦
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>spring01artifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<java.version>1.8java.version>
<maven.compiler.source>${java.version}maven.compiler.source>
<maven.compiler.target>${java.version}maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring-framework.version>5.2.10.RELEASEspring-framework.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring-framework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring-framework.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.16version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-jar-pluginartifactId>
<version>3.2.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-site-pluginartifactId>
<version>3.3version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.2version>
plugin>
plugins>
build>
project>
<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
https://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="org.example"/>
beans>
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//根据Spring配置文件路径创建容器:应用上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//关闭容器
((ClassPathXmlApplicationContext) context).close();
}
}
1.使用 Controller 注解、Bean 注解 + 方法注入
bean 注解使用在方法上,该方法所在的类也需要注解
从容器中获取 Bean 对象的方法:
- 通过类型获取(需要容器中只有一个对象)
- 通过名称、id 获取
package org.example.controller;
import org.example.model.Duck;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
// 使用 Controller 注解将该类注入到 Bean 容器中
@Controller
public class LoginController {
// 方法前使用 Bean 注解, 也能够注入对象
@Bean
public Duck d1() {
Duck duck = new Duck();
duck.setName("唐老鸭");
return duck;
}
@Bean
public Duck d2() {
Duck duck = new Duck();
duck.setName("香酥鸭");
return duck;
}
}
package org.example.dao;
import org.springframework.stereotype.Repository;
@Repository
public class LoginRepository {
}
package org.example.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Duck {
private String name;
}
package org.example.service;
import org.springframework.stereotype.Service;
@Service
public class LoginService {
// 如果注入的对象不止一个 那么需要使用 @Qualifier 注解来确定使用的是哪个对象
@Autowired
@Qualifier("luYa")
public Duck duck;
}
package org.example.service;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
// Scope 注解每次都能获取新的 Bean 对象
@Service
@Scope("prototype")
public class LoginServicePrototype {
}
package org.example;
import org.example.controller.LoginController;
import org.example.dao.LoginRepository;
import org.example.model.Duck;
import org.example.service.LoginService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//根据Spring配置文件路径创建容器:应用上下文对象
// 类加载路径下的 beans.xml 文件
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 1. 通过类型获取
LoginRepository login1 = context.getBean(LoginRepository.class);
// 2. 通过类名获取, 注意, 类名首字母小写
LoginRepository login2 = (LoginRepository) context.getBean("loginRepository");
System.out.println(login1);
System.out.println(login2);
// 通过类型获取
LoginController login3 = context.getBean(LoginController.class);
// 通过类名获取
LoginController login4 = (LoginController) context.getBean("loginController");
System.out.println(login3);
System.out.println(login4);
LoginService login5 = context.getBean(LoginService.class);
// 通过类名获取
LoginService login6 = (LoginService) context.getBean("loginService");
System.out.println(login5);
System.out.println(login6);
Duck d1 = (Duck) context.getBean("d1");
Duck d2 = (Duck) context.getBean("d2");
System.out.println(d1);
System.out.println(d2);
// 同一类型注册了两个以上对象, 不能通过类型来获取
// Duck d3 = context.getBean(Duck.class);
// System.out.println(d3);
//关闭容器
((ClassPathXmlApplicationContext) context).close();
}
}
// 打印结果
// org.example.dao.LoginRepository@76a4d6c
// org.example.dao.LoginRepository@76a4d6c
// org.example.controller.LoginController@517cd4b
// org.example.controller.LoginController@517cd4b
// org.example.service.LoginService@6cc7b4de
// org.example.service.LoginService@6cc7b4de
// Duck(name=唐老鸭)
// Duck(name=香酥鸭)
BeanFactory 和 FactoryBean 的区别
- BeanFactory 是 Spring 的顶级接口
- FactroyBean 是工厂 Bean,本身是一个 Bean,且主要是生成 Bean 的工厂
把 getObject() 方法的返回值注册到容器中