文章摘要: 摘要内容。
命名空间
==待完善==
<beans>
- 功能:定义IoC容器,存放所有bean对象。
- 在spring的xml中最外层的标签。
格式
<beans></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 http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
<bean>
- 功能:定义单个具体的bean对象。
格式
<bean></bean>
属性
id:唯一ID,容器可以通过id获取对应的bean对象。class:路径,配置bean的全路径类名。name:别名,允许同时添加多个,支持使用逗号,、分号;、空格``进行分割。autowire:自动注入,byType根据类型<默认>、byName根据set方法的名字、constructor根据构造器。depends-on:控制加载顺序,指定若干个bean在自身之前先加载,depends-on="<bean对象名称>"。lazy-init:懒加载,是否在被调用时才将bean加载到IoC容器中。true懒加载、false非懒加载<默认>。scope:bean的作用域,singleton单例<默认>、prototype多例、request、session、application、websocket。- ==待完善==,生命周期回调,设置创建或销毁时执行哪个方法。
案例
<beans>
<!--将dao添加到IoC容器中-->
<bean id="bookDao" name="book bookDaoImap" class="com.cykj.dao.impl.BookDaoImpl"></bean>
<!--将service添加到IcO容器中-->
<bean id="bookService" class="com.cykj.service.impl.BookServiceImpl"></bean>
</beans>
<property>和<constructor-arg>
- 功能:依赖注入,配置单个具体的bean对象。
- 依赖注入的标签可以嵌套使用,来限制作用域范围。
格式
<property></property>:实例化无参构造方法创建对象,并传入数据修改get/set方法完成创建bean对象,(提示:需要有无参构造方法才能使用)。<constructor-arg></constructor-arg>:通过实例化有参构造方法进行创建bean对象。
属性
name:参数名。(提示:这里的名字是类提供的Setter和Getter方法,并自动将前面的字母剔除掉了)。value:数据。ref:关联其他bean对象。
案例
<beans>
<!--关联其他bean对象 - 01方案-->
<bean class="com.cykj.wellness.beans.Wife" id="wife">
</bean>
<bean class="com.cykj.wellness.beans.User" id="user">
<!--外部引入-->
<property name="wife" ref="wife"></property>
</bean>
<!--关联其他bean对象 - 02方案-->
<bean class="com.cykj.wellness.beans.User" id="user">
<!--内部引入-->
<property name="wife">
<bean class="com.cykj.wellness.beans.Wife" id="wife">
</bean>
</property>
</bean>
<!--<constructor-arg>,通过实例化**有参构造方法**进行创建对象-->
<bean class="com.cykj.wellness.beans.User" id="user">
<!--写法1-->
<constructor-arg name="id" value="18"></constructor-arg>
<!--写法2-->
<constructor-arg value="18"></constructor-arg>
<!--写法3-->
<constructor-arg index="0" value="18"></constructor-arg>
</bean>
</beans>
存储数据的标签
格式
<value></value>
- 具体的基本类型的数据。
<list></list>
- 定义列表数据。
<map></map>
- 定义字典数据。
<entry></entry>
- 定义字典数据中的数据。
<entry key="键" value="值"><entry>:存储基本数据类型。<entry value-ref="bean对象"><entry>:存储对象类型。
案例
<!--list类型的数据-->
<list>
<value></value>
</list>
<!--字典类型-->
<map>
<entry key="" value=""><entry>
</map>
<context:component-scan>
- 配置IoC容器扫描的包路径
格式
<context:component-scan></context:component-scan>:配置扫描的包路径
属性
base-package:指定扫描的包路径。use-default-filters:设置是否扫描注解,true扫描注解<默认>、false不扫描注解。
案例
<?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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--指定包扫描路径,通过查找注解的方式来确定bean对象,并添加到IoC容器中进行管理-->
<!--通过指定扫描路径,用于告知spring的扫描位置-->
<context:component-scan base-package="<需要扫描的包路径>"></context:component-scan>
</beans>
<context:exclude-filter>和<context:include-filter>
- 排除扫描,设置需要排除扫描的选项。
格式
<context:exclude-filter />:排除扫描,设置需要排除扫描的选项。<context:include-filter />:包含扫描,设置需要包含扫描的选项。
属性
type:排除/包含的方式,annotation注解<默认>、assignable类名、aspectj切面表达式、regex正则表达式、custom根据接口自定义expression:完整限定名。
案例
<beans>
<context:component-scan base-package="<需要扫描的包路径>">
<!--排除扫描,设置需要排除扫描的选项-->
<context:exclude-filter type="<排除方式>" expression="<完整限定名/规则>"/>
<!--包含扫描,设置需要包含扫描的选项-->
<context:include-filter type="<包含方式>" expression="<完整限定名/规则>"/>
</context:component-scan>
</beans>
<imput>
- 功能:导入其他xml配置文件。
格式
<imput></imput>
案例
<imput resource="spring-mvc.xml"></import>
<context:property-placeholder>
- 在xml配置文件中引入外部属性资源文件。
格式
<context:property-placeholder></context:property-placeholder>
属性
location:文件位置。
案例
<?xml version="1.0" encoding="UTF-8"?>
<!--添加命名空间-->
<!--提示 添加内容:xmlns:context="http://www.springframework.org/schema/context"-->
<!--提示 添加内容:http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd-->
<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.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--引入外部属性资源文件-->
<context:property-placeholder location="db.properties"/>
</beans>