Spring 配置文件:
- 对放置位置无要求,因此需要指定配置文件的路径,一般放置在 src/main/resources 路径下,通过 classpath:springContext.xml 进行导入
- 对命名无特定要求,一般按照约定俗成的方式进行命名
- spring-context.xml
- beans.xml
- applicationContext.xml
可以通过 idea 开发工具自动生成:在 src/main/resources 文件夹上右键,【new】->【XML Configuration File】->【Spring Config】
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"></beans>
Spring 配置文件常见的配置项
<?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"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- Spring的配置文件,配置主要业务逻辑 --><!-- 数据源,事务控制,mybatis整合 --><!-- 组件扫描 --><context:component-scan base-package="com.example"><!-- 除外Controller的类 --><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 引入数据库连接的配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 配置Druid数据源 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!-- 配置MyBatis整合 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 指定mybatis的全局配置文件的位置 --><property name="configLocation" value="classpath:mybatis-config.xml"/><!-- 指定数据源 --><property name="dataSource" ref="dataSource"/><!-- mapper文件位置 --><property name="mapperLocations" value="classpath:mapper/*.xml"/></bean><!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 扫描所有dao接口的实现,加入到ioc容器中 --><property name="basePackage" value="com.example.dao"/></bean><!-- 配置一个可以批量执行的SqlSession --><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/><constructor-arg name="executorType" value="BATCH"/></bean><!-- 配置事务管理 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 数据源 --><property name="dataSource" ref="dataSource" /></bean><!-- 开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) --><aop:config><!-- 切入点表达式 --><aop:pointcut id="txPoint" expression="execution(* com.example.service..*(..))"/><!-- 配置事务增强 --><aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/></aop:config><!-- 配置事务增强,事务如何切入 --><!-- 即存在多个事务管理器时,需要使用transaction-manager指定 --><tx:advice id="txAdvice"><tx:attributes><!-- 所有方法都是事务方法 --><tx:method name="*"/><!-- 以get开始的所有方法 --><tx:method name="get*" read-only="true"/></tx:attributes></tx:advice></beans>
Spring 核心工厂
ApplicationContext,接口,用于屏蔽实现的差异,属于重量级资源,创建该对象时会消耗大量资源,因此,一个应用只会创建一个工厂对象,且线程安全的(多线程并发访问),主要的实现有如下几种:
Main和junit测试:
- xml配置:ClassPathXmlApplicationContext
- javaConfig配置:AnnotationConfigApplicationContext
Web:
- xml配置:XmlWebApplicationContext
- javaConfig配置:AnnotationConfigWebApplicationContext
配置细节:
- 只配置bean标签中的class属性:
- 如
<bean class="top.songfang.User"/>,则Spring会自动分配 id 值为top.songfang.User#0 - 应用场景:该类为单例,且不被其他 bean 引用
- 如
- bean 的 name 属性与 id 属性的异同点:name,用来配置 bean 的别名
- 相同点:都可以用来获取 bean
- 不同点:
- 同一 bean 中 name 属性可以定义多个,即
name="name1,name2,...",id 只能定义一个 - xml的 id 属性值,命名要求:必须以字母开头,且只能包含字母、下划线、数字、连字符(现在已经没有这种限制);name属性的值无特殊要求
- 同一 bean 中 name 属性可以定义多个,即
- 其他细节:Spring反射创建对象时,一定会调用对象的无参构造方法,不管是否是public,因此,在使用Spring管理bean时,一定要提供无参构造方法
