IOC和DI
IOC:反转控制,反转资源获取的方向。以前是组件向容器发起请求查找资源。IOC则是主动将资源推送给它所管理的组件,组件要做的就是选择一种合适的方式接收资源。
DI:依赖注入,依赖于容器把资源注入给我,(IOC的另一种表述方式)组件以预先定义好的方式(如setter)接收来自容器的资源。
Spring容器
Spring容器在读取配置文件创建Bean之前需要进行实例化,只有实例化之后才能从IOC容器里获取Bean的实例并使用。
Spring中提供了两种IOC容器的实现:
1、BeanFactory:IOC容器的基本实现,是Spring框架的基础设施,面向Spring本身。
2、ApplicationContext:提供了更多高级的特性,是BeanFactory的子接口,面向开发者,几乎所有的场景都直接使用,而非使用底层的BeanFactory。
ApplicationContext主要有两种实现类:
其中的ConfigurableApplicationContext扩展于ApplicationContext,新增了两个方法:refresh()和close(),让ApplicationContext具有启动、刷新和关闭上下文的功能。
ClassPathXMLApplicationContext是从类路径下加载配置文件;FileSystemXMLApplicationContext是从文件系统中加载配置文件。
public void test(){
//HelloWorld helloWorld = new HelloWorld();
//1、创建Spring的IOC容器——》调用构造器对配置文件中的bean进行初始化,
/**
* ApplicationContext:代表IOC容器
* ClassPathXmlApplicationContext:是ApplicationContext的实现类,用于加载类路径下的配置文件
*/
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//2、从IOC容器中获取bean的实例
//用id定位到IOC容器中的Bean
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
//3、调用方法
helloWorld.print();
}
Spring注入方式:
1、属性注入:通过Setter方法注入Bean的属性值或依赖对象,是最常用的注入方式。
<!--
配置bean
id:唯一识别符,外部引用
class:bean的全类名,通过反射的方式在IOC容器中创建Bean,要求Bean中必须要有无参构造器
-->
<bean id="helloWorld" class="bean.HelloWorld">
<property name="name" value="你厉害,你赚得多!"/>
</bean>
2、构造器注入:实体类Bean中必须要有构造函数,使用改方法可以设置注入的类型和位置,以区分重载的构造器
如果其中包含特殊字符可以使用 ![CDATA[ ]] 进行包装之后使用。
<!--构造器注入-->
<bean id="car" class="bean.Car">
<constructor-arg value="英菲尼迪" index="0"/>
<constructor-arg value="300" type="java.lang.Double"/>
</bean>
属性值还可以使用<value>子节点进行设置
<!--构造器注入-->
<!--如果包含特殊字符可以使用 ![CDATA[ ]]-->
<bean id="car" class="bean.Car">
<constructor-arg value="英菲尼迪" index="0"/>
<constructor-arg type="java.lang.Double">
<value><![CDATA[390]]></value>
</constructor-arg>
</bean>
3、工厂方法注入(很少用)
略
Bean之间的相互引用
<!--构造器注入-->
<!--如果包含特殊字符可以使用 ![CDATA[ ]]-->
<bean id="car1" class="bean.Car">
<constructor-arg value="英菲尼迪" type="java.lang.String"/>
<constructor-arg>
<value>390</value>
</constructor-arg>
</bean>
<bean id="person" class="bean.Person">
<property name="name" value="Tom"/>
<property name="age" value="24"/>
<property name="car" ref="car1"/>
</bean>
配置集合属性
<!--配置集合属性-->
<bean id="boss" class="bean.Boss">
<property name="name" value="Mike"/>
<property name="age" value="22"/>
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car1"/>
</list>
</property>
</bean>
properties属性
public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "DataSource{" +
"properties=" + properties +
'}';
}
}
<!--配置Properties属性-->
<bean id="dataSource" class="bean.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">123456</prop>
<prop key="url">jdbc:mysql://localhost:3306/----</prop>
<prop key="driver">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
配置单例的集合bean,供多个bean使用
<!--配置单例的集合bean,以供多个bean进行引用-->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car1"/>
</util:list>
P命名空间,对配置进行简化,相对于传统的方式更加简洁
<bean id="helloWorld" class="bean.HelloWorld">
<property name="name" value="你厉害,你赚得多!"/>
</bean>
<bean id="hello" class="bean.HelloWorld"
p:name="lallala"/>