Spring框架IoC模块
1)Bean
IoC容器管理的类对象。
2)配置文件
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- IoC容器管理的Bean -->
<bean id="对象ID(相当于对象引用)" class="类的全限定名">
<!-- 字段set赋值方法 -->
<property name="字段名称(引用类型)" ref="对象ID" />
<property name="字段名称(基本类型)" value="常量值" />
<!-- 字段构造器赋值方法 -->
<constructor-arg type="类的全限定名" ref="对象ID" />
<constructor-arg type="类的全限定名" value="常量值" />
</bean>
</beans>
3)控制反转和自动装配
“<bean”标签代表容器生成了某个对象,“<property”标签代表了容器为对象的字段赋值或者引用对象,实际上相当于这样的代码:
Person person = new Person;
person.setName("张三");
“控制反转”就是对象的生成销毁。
“自动装配”就是给字段赋值。
4)细节补充
Bean范围:单例、原型、请求、会话、全局。
配置注解:<context:annotation-config/>、<context:component-scan base-package="org.example"/>
控制反转注解:@Autowired、@Resource
自动装配注解:@Controller、@ component 、@ service 、@Controller 、@Repository