在Spring的学习中,对于其xml文件的配置是必不可少的。在Spring的多种装配Bean的方式中,采用XML配置也是最常见的。以下是一个简单的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="" class=""/>
</beans>
如果想要给某个类(比如:com.qh.spring.User)创建一个user的Bean。那么在上述代码中做如下填写:
<bean id="user" class="com.qh.spring.User"/>
除了依赖注入外,Spring还提供了AOP(面向切面编程),那么如何在XML中配置AOP呢?我们可以在上述简单的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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="user" class="com.qh.spring.User"/>
<aop:config>
<aop:aspect ref="uesr"> ——定义切面
<aop:pointcut id="" ——定义切点
expression=""/>
<aop:before pointcut-ref="" method=""/> ——声明前置通知
<aop:after pointcut-ref="" method=""/> ——声明后置通知
</aop:aspect>
</aop:config>
</beans>
在Spring中很多时候会用到注解,那么就要在XML文件中进行注解装配,我们可以继续在上述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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config /> ——配置到这里,我们就可以使用基于注解的装配了
<bean id="user" class="com.qh.spring.User"/>
<aop:config>
<aop:aspect ref="uesr">
<aop:pointcut id=""
expression=""/>
<aop:before pointcut-ref="" method=""/>
<aop:after pointcut-ref="" method=""/>
</aop:aspect>
</aop:config>
</beans>
另外,Spring中还有一个技巧,<context:component-scan>元素除了完成于<context:annotation-config>一样的工作,还允许Spring自动检测Bean和定义Bean。这意味着不使用<bean>元素,Spring应用中的大多数Bean都能够实现定义装配:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="com.qh.spring">
</context:component-scan>
</beans>
<context:component-scan>元素会扫描指定的包及其所有子包,并查找出能够自动注册为Spring Bean 的类。base-package属性标识了<context:component-scan>元素所扫描的包。