在阅读的过程中有任何问题,欢迎一起交流
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>
1、XML Schema命名空间作用:
1)、避免命名冲突,像Java中的package一样
2)、将不同作用的标签分门别类(像context命名空间针对组件的标签)
2、代码解释:
1)、xmlns="http://www.springframework.org/schema/beans"
声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间。
2)、xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
声明XML Schema 实例,声明后就可以使用 schemaLocation属性了
3)、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"
7)、xmlns:aop="http://www.springframework.org/schema/mvc"
声明前缀为mvc的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。
8)、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中可以包含如下属性:
2、Context命名空间
1)、Context标签:
<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标签