依赖注入
1、依赖:指bean对象创建于容器,bean对象创建依赖于资源
2、注入:指bean对象所依赖的资源,由容器来设置和装配
- 构造器注入
- set注入
DI依赖注入
IOC的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。在生产bean的过程中,需要解决bean之间的依赖问题,才引入了依赖注入(DI)这种技术。也就是说依赖注入是beanFactory生产bean时为了解决bean之间的依赖的一种技术而已。
对象如何生成(BeanFactory和ApplicationContext的区别)
刚说的IOC就是由Spring来接手对象的创建、生命周期和对象间的关系。
Spring的本质就是一个bean工厂或者说bean容器,它按照我们的需求创作出各种各样的bean。
beanFactory会在bean的生命周期的各个阶段中对bean进行各种管理,并且spring将这些阶段通过各种接口暴露给我们,让我们可以对bean进行各种处理,我们只要让bean实现对应的接口,那么spring就会在bean的生命周期调用我们实现的接口来处理该bean。
BeanFactory和ApplicationContext
在 spring 容器中,BeanFactory 接口是 IOC 容器要实现的最基础的接口,定义 了管理 bean 的最基本的方法。主要是面向spring框架本身,不支持AOP和web功能. 在获取bean时才会创建bean 。ApplicationContext 继承BeanFactory接口,对功能进行扩展. 支持aop,web等插件,面向开发层面, 可以在框架启动时,就可以创建bean。如果说BeanFactory是心脏,那么ApplicationContext就是躯体。
案例
1、创建一个Student类
public class Student {
}
2、创建一个School类
import java.util.*;
public class School {
private String name;
private Student student;
private String[] array;
private List<String> list;
private Map<String,String> map;
private Set<String> set;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", student=" + student +
", array=" + Arrays.toString(array) +
", list=" + list +
", map=" + map +
", set=" + set +
", info=" + info +
'}';
}
}
3、配置bean.xml的基本配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="pojo.Student">
<property name="student" value="华晨宇"></property>
</bean>
<bean id="school" class="pojo.School">
<property name="name" value="鞠婧祎"></property>
<property name="student" ref="student"></property>
<property name="array">
<array>
<value>湖北</value>
<value>湖南</value>
<value>北京</value>
</array>
</property>
<property name="list">
<list>
<value>天门</value>
<value>潜江</value>
<value>仙桃</value>
</list>
</property>
<property name="map">
<map>
<entry key="天" value="123"></entry>
<entry key="地" value="456"></entry>
</map>
</property>
<property name="set">
<set>
<value>Java</value>
<value>python</value>
<value>Android</value>
</set>
</property>
<property name="info">
<props>
<prop key="driver">com.mysql.cj.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>
4、测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.School;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
School school = (School) context.getBean("school");
System.out.println(school.toString());
}
}
5、测试结果
D:\.jdks\corretto-1.8.0_332\bin\java.exe "-javaagent:F:\IntelliJ IDEA Community Edition 20222.3\ideaIU-2020.2.3.win\lib\idea_rt.jar=55096:F:\IntelliJ IDEA Community Edition 20222.3\ideaIU-2020.2.3.win\bin" -Dfile.encoding=UTF-8 -classpath D:\.jdks\corretto-1.8.0_332\jre\lib\charsets.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\access-bridge-64.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\cldrdata.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\dnsns.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\jaccess.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\jfxrt.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\localedata.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\nashorn.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\sunec.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\sunjce_provider.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\sunmscapi.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\sunpkcs11.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\ext\zipfs.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\jce.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\jfr.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\jfxswt.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\jsse.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\management-agent.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\resources.jar;D:\.jdks\corretto-1.8.0_332\jre\lib\rt.jar;F:\Spring\Spring_Maven\target\test-classes;F:\Spring\Spring_Maven\target\classes;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\junit\junit\4.12\junit-4.12.jar;F:\Maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven-repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar MyTest
School{name='鞠婧祎', student=Student{student=华晨宇}, array=[湖北, 湖南, 北京], list=[天门, 潜江, 仙桃], map={天=123, 地=456}, set=[Java, python, Android], info={password=root, url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false, driver=com.mysql.cj.jdbc.Driver, username=root}}
User{name='胡九年', age=56}
Process finished with exit code 0
标签:依赖,spring,jar,332,maven,bean,jdks,注入
From: https://www.cnblogs.com/lovesz/p/16714682.html