功能 | 依赖 | 约束 | 约束的具体作用 |
---|---|---|---|
Spring环境 | spring-context(webmvc可替代) | beans | Spring总体环境配置 |
IOC | 不需要额外依赖 | context | 注解时使用,主要用来读取包或者引入外部文件 |
DI | 不需要额外依赖 | 不需要额外约束 | |
AOP | aspectjweaver、aop(context中包含) | AOP | 用来配置切面和通知,以及使用注解时使用加载aop的注解驱动 |
事务 | spring-tx(可用spring-jdbc代替) | tx | 注解时主要用来开启事务管理者 |
jdbcTemplate | spring-jdbc | 无 |
1.什么是约束
约束,顾名思义——对所考察物体起限制作用的其他物体,称为约束
在实际的Java Spring框架代码中,我们常常会编写 applicationContext.xml配置文件。例如:
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
2.约束的作用
我们看到在生成的spring 配置上面会有约束头的存在,他的作用就是对配置文件以及代码起到一定的限制作用,有了他们的存在,代码才能够正常运行。其底层代码可以直接打开给到的网址查看,为了让大家节约时间,我附上一张截图。网址是http://www.springframework.org/schema/beans也可以自己打开看一下,但是基础不是超级雄厚的,估计也是看不懂的。
3.约束的类型
Spring的约束类型主要有四种:
1》beans: 最基本,最根本
创建spring文件的时候,在生成的约束头中,除了第一行有个声明版本以及字符编码除外,之后就是<beans></beans> 一对标签了,它是整个整个Spring配置中最基础的。也是最根本的约束。
2》context:读取properties配置
这个标签也是除了<beans>之外使用频率最高的标签了,它是在注解时使用,主要用来读取包或者引入外部文件
这个图片是上面给到的代码,框到的部分就是属于context 约束头的部分。
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!--这里的就是用来读取包引入外部文件的!!!!-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
3》aop:配置aop
待更新
4>tx:配置事务通知
待更新
IOC:控制反转 用来创建对象 并存入核心容器
DI:依赖注入 将核心容器里面的东西赋值给变量
AOP:面向切面编程 使用切面中的通知去增强切入点的功能
参考其他博客点击跳转
整个Spring需要的依赖(spring环境):spring-context 依赖以及beans约束
使用IOC注解时需要引入context约束,主要用来读取包
使用AOP时需要依赖:切面依赖 aspectjweaver、aop依赖 以及AOP约束
用来配置切面和通知 以及注解时加载aop的注解驱动<aop:aspectj-autoproxy>
使用事务管理时需要依赖:tx依赖:spring-tx 以及tx约束(由于jdbc依赖包含了tx,
可以直接使用spring-jdbc,jdbc也需要用来简化原生jdbc)
主要用来开启事务管理者
<tx:annotation-driven transaction-manager="transactionManager"/>
使用jdbcTemplate需要的依赖,jdbc依赖,spring-jdbc。对原生的
JDBC提供了支持,对原始Jdbc API对象的简单封装。