spring学习——xml文件的配置

在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>元素所扫描的包。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值