【深入JAVA EE】Spring配置文件解析
在阅读的过程中有不论什么问题,欢迎一起交流
QQ:1494713801
一、Spring头信息
Spring配置文件的头部信息通常是固定不变的。但每个标签都有自己的含义。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"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<!— xml配置内容 -->
</beans>
、XML Schema命名空间作用:
)、避免命名冲突,像Java中的package一样
)、将不同作用的标签分门别类(像context命名空间针对组件的标签)
、代码解释:
)、xmlns="http://www.springframework.org/schema/beans"
声明xml文件默认的命名空间,表示未使用其它命名空间的全部标签的默认命名空间。
)、xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
声明XML Schema
实例,声明后就能够使用 schemaLocation属性了
)、xmlns:context="http://www.springframework.org/schema/context"
4)、xmlns:cache="http://www.springframework.org/schema/cache"
5)、xmlns:p="http://www.springframework.org/schema/p"
给XML配置文件"减肥"的还有一个选择就是使用p名称空间。当我们採用了p名称空间。我们就能够在bean
元素中使用属性(attribute)来描写叙述bean的property值。
6)、 xmlns:task="http://www.springframework.org/schema/task"
)、xmlns:aop="http://www.springframework.org/schema/mvc"
声明前缀为mvc的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。
当命名空间被定义在元素的開始标签中时,全部带有同样前缀的子元素都会与同一个命名空间相关联。
)、xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
这个从命名能够看出个大概。指定Schema的位置这个属性必须结合命名空间使用。这个属性有两个值,第一个值表示须要使用的命名空间。第二个值表示供命名空间使用的 XML schema的位置
所以我们须要什么样的标签的时候,就引入什么样的命名空间和Schema定义就能够了。
二、Spring配置文件结构
beans标签中能够包括4个标签:
<alias>为一个定义过的bean起一个别名
<bean>向Spring容器中定义bean元素
<description>用来描写叙述Spring context或每一个bean元素。尽管他会被Spring容器所忽略,但<description>元素能够通过工具生成属于你的Spring
context文档
<import>导入其它Spring context的定义
1、bean命名空间:
1)、标签bean中能够包括例如以下元素:
<constructor-arg>向bean的构造函数注入值或引用,即构造函数注入
<description>同beans中作用同样。用来描写叙述Spring context或每一个bean元素,尽管他会被Spring容器所忽略,但<description>元素能够通过工具生成属于你的Spring
context文档
<lookup-method>用法来取代getter注入,指定一个方法。他会在执行被复写从而返回一个指定的bean,即getter注入
<meta>同意为你的bean进行meta配置,仅在一些特殊场合下实用
<property>为bean的特定属性注入一个值或者引用。这就是我们常说的setter注入
<replaced-method>用一个新的实现来取代bean的某个方法
2)、标签bean中能够包括例如以下属性:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="550" height="480">
2、Context命名空间
1)、Context标签:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="531" height="270">
<context:component-scan/>具体解释
.假设不想在xml文件里配置bean。能够给我们的类加上spring组件注解,仅仅需再配置下spring的扫描器<context:component-scan/>就能够实现bean的自己主动载入。
然后在程序中增加注解便可自己主动载入bean,@Component
是全部受Spring管理组件的通用形式;而@Repository
、@Service
和@Controller
则是@Component
的细化,用来表示更详细的用例(比如,分别相应了持久化层、服务层和表现层)。
也就是说,你能用@Component
来注解你的组件类,但假设用@Repository
、@Service
或@Controller
来注解它们,你的类或许能更好地被工具处理,或与切面进行关联。
<context:component-scan>提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表引入和排除的过滤。
有了<context:component-scan>,还有一个<context:annotation-config/>标签根本能够移除掉。由于已经被包括进去了。
3、AOP命名空间
1)、AOP标签
4、JEE命名空间
1)、JEE标签
最新文章
- JS 获取CSS属性值
- start WampServer如何关闭浏览目录
- java.sql.SQLException: Incorrect key file for table &#39;C:\Windows\TEMP\#sql578_6e2_68d.MYI&#39;; try to repair it
- 分区还原工具(DiskGenius)
- 未完结第八节 JBPM流程节点
- 关于Xib 需要注意的地方
- 使用my97datepicker控件实现日期范围选择
- java.lang.OutOfMemoryError: Java heap space错误和方法(集、转)
- Xamarin Android自定义文本框
- [LeetCode] Reverse String II 翻转字符串之二
- 【Debug】逻辑分析仪数据错乱,看波形为信号耦合导致数据错乱,实际上为逻辑分析仪地线没接上!
- Visual Studio 2017 and Swagger: Building and Documenting Web APIs
- python全栈开发 * 13知识点汇总 * 180619
- 绑定的jndi获得connection时,出的错,java.io.NotSerializableException
- [转]kaldi特征和模型空间转换
- Nginx基本
- 与服务器同步工程(expect脚本)
- 深度剖析:PHP中json_encode与json_decode
- 彻底弄懂JS的事件冒泡和事件捕获(不推荐阅读)
- Jupyter Notebook 快速入门[转]
热门文章
- 小米监控 open-falcon部署
- svn提示出错异常为remains in conflict
- BZOJ 4500: 矩阵
- 本机开发Native Development:Invalid path for NDK (转)
- 启动tomcat服务器自动执行一个方法
- ZOJ 3593.One Person Game-扩展欧几里得(exgcd)
- (19)python scrapy框架
- TopCoder SRM 675 Div1 Problem 500 LimitedMemorySeries1(分块)
- 牛客网暑期ACM多校训练营 记录
- [Cocoa]深入浅出Cocoa多线程编程之 block 与 dispatch quene