我们都知道IOC是spring框架的核心,主要作用是控制反转,把我们需要的对象从容器中提供给我们,但是IOC是如何加载我们所需要的对象的?
Spring容器是IOC容器的一种,它通过ApplicationContext接口将我们所需要的配置文件进行加载,然后提供给我们。其实这是一种Spring容器的具象化体现,直接通过ApplicationContext可以拿到我们需要的对象。
第一种可以选择用工程的xml配置文件配置我们需要的bean
<?xml version="1.0" encoding="UTF-8" ?>
<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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<bean id="boss" class="com.baobaotao.Boss"/>
<bean id="office" class="com.baobaotao.Office">
<property name="officeNo" value="001"/>
</bean>
<bean id="car" class="com.baobaotao.Car" scope="singleton">
<property name="brand" value=" 红旗 CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
然后在测试类中利用ApplicationContext对象加载配置文件获得对象
public class Test {
public static void main(String[] args) {
//加载项目中的spring配置文件到容器
ApplicationContext context = new ClassPathXmlApplicationContext("resouces/applicationContext.xml");
//加载系统盘中的配置文件到容器
//ApplicationContext context = new FileSystemXmlApplicationContext("E:/Spring/applicationContext.xml");
//从容器中获取对象实例
Man man = context.getBean(Boss.class);
man.driveCar();
}
}
第二种可以选择用注解加载配置
//同xml一样描述bean以及bean之间的依赖关系
@Configuration
public class ManConfig {
@Bean
public Man man() {
return new Man(car());
}
@Bean
public Car car() {
return new QQCar();
}
}
public class Test {
public static void main(String[] args) {
//从java注解的配置中加载配置到容器
ApplicationContext context = new AnnotationConfigApplicationContext(ManConfig.class);
//从容器中获取对象实例
Man man = context.getBean(Man.class);
man.driveCar();
}
}
以上是我们在spring学习中遇到的又被忽略掉的关于应用上下文加载配置的问题,现在很多框架已经完全不需要我们这么做。
附:
一、Spring三种对象装配机制:
1、在XML中进行显式配置。
2、在Java中进行显式配置。
3、隐式的bean发现机制和自动装配。
二、Spring从两个角度来实现自动化装配:
组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。
自动装配(@Autowired):Spring自动满足bean之间的依赖。
三、springbean命名
spring组件扫描会为上下文中创建的bean设置id,即类名首字母小写,或者@Component("自定义id")
或者使用@Named("自定义id")
,spring会根据id找到bean。
四、context命名空间
<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。
<context:component-scan/>
spring2.5之后对xml文件进行了简化,
五、xml文件配置和Java注解配置混合使用
1、config类导入另一个config类配置
(1)@import注解导入另一个配置类
@Configuration
@Import(CDConfig.class)
public class CDPlayerConfig {
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}
(2)或者创建一个更高级别的config把两个类拼装在一起
package soundsystem;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({CDPlayerConfig.class, CDConfig.class})
public class SoundSystemConfig {
}
2、在xml中导入另一个xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="soundsystem.CDConfig"/>
<import resource="cdplayer-config.xml"/>
</beans>
3、在xml中引入config类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
我们将 BlankDisc 配置在 JavaConfig中,CDPlayer 继续配置在 XML 中
这时,<import>元素只能导入其他的 XML 配置文件,为了将 JavaConfig 类导入到 XML 配置中,我们使用 <bean> 标签。
-->
<bean class="soundsystem.CDConfig"/>
<bean id="cdPlayer"
class="soundsystem.CDPlayer"
c:compactDisc-ref="compactDisc"/>
</beans>
4、在config中引入xml用@ImportResource
注解
package com.jiaobuchong.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
@Configuration
@Import(CDPlayerConfig.class)
@ImportResource("classpath:cons-injec.xml") //导入xml配置项
public class SoundSystemConfig {
}
也就是说我们需要用bean标签的形式把config引入到xml文件中来
参考博文:
https://blog.csdn.net/xyh820/article/details/7303330/
https://www.cnblogs.com/xiehang/p/9739290.html
https://www.cnblogs.com/piaxiaohui/p/7542841.html