spring的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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
<bean id="helloWorld" class="springDemo.com.mc.app.HelloWorld">
	<property name="messageString" value="Hello World By http://blog.csdn.net/unix21!"/>
</bean>

<!--  <constructor-arg value="Audi" index="0"></constructor-arg> 构造器赋值 -->

 <!-- 使用 property的ref属性建立Bean之间的引用关系
 <property name="car" ref="car"></property>-->
 
<bean id="car3" class="com.hzg.spring.beans.Car">
     <constructor-arg value="Toyota" type="java.lang.String"></constructor-arg>
     <constructor-arg value="95" type="int"></constructor-arg>
     <constructor-arg value="black" type="java.lang.String"></constructor-arg>
</bean>
<bean id="person" class="com.hzg.spring.beans.Person">
     <property name="name" value="hzg"></property>
     <property name="age" value="32"></property>
    <!-- 集合属性(List),使用ref来配置子节点信息 -->
   <property name="cars">
        <list>
            <ref bean="car1"></ref>
           <ref bean="car2"></ref>
           <ref bean="car3"></ref>
        </list>
    </property>
</bean>


<!--  
<bean id="person" class="com.hzg.spring.beans.Person">
    <property name="name" value="hzg"></property>
    <property name="age" value="32"></property>
  
    <property name="cars" ref="cars"></property>
</bean>
<util:list id="cars">
    <ref bean="car1"></ref>
    <ref bean="car2"></ref>
</util:list>
-->
<!-- 导入属性文件 -->
<context:property-placeholder location="db.properties"></context:property-placeholder><!--xmlns:context="http://www.springframework.org/schema/context" -->
<bean id="dataSource" class="com.hzg.MyDataSource">
  <!-- 使用外部属性文件的属性值 -->
  <property name="user" value="${user}"></property>
  <property name="password" value="${password}"></property>
   <property name="diverClass" value="${driverClass}"></property>
   <property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>
二

<util:properties id="config" location="classpath:db.properties" />
<!-- 配置连接池 -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{config.driver}" />
<property name="url" value="#{config.url}" />
<property name="username" value="#{config.username}" />
<property name="password" value="#{config.password}" />
</bean>

  <!-- 切面Bean(日志切面) -->
    <bean id="logAspect" class="com.hzg.LogAspect"></bean>

    <!-- 切面Bean(时间切面) -->
    <bean id="timeAspect" class="com.hzg.TimeAspect"></bean>

    <!-- AOP配置 -->
    <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut id="pointcut" expression="execution(* com.hzg.IHelloWorld.*(..))"></aop:pointcut>
        <!-- 配置切面(日志切面)切面的优先级需要通过order定义 -->
        <aop:aspect id="log" ref="logAspect" order="1">
            <!-- 配置通知(前置通知、后置通知、返回通知、异常通知、环绕通知) -->
            <aop:before method="beforeLog" pointcut-ref="pointcut"></aop:before>
            <aop:after method="afterLog" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
        <!-- 配置切面(时间切面)切面的优先级需要通过order定义 -->
        <aop:aspect id="time" ref="timeAspect" order="2">
            <!-- 配置通知(前置通知、后置通知、后置返回通知、异常通知、环绕通知) -->
            <aop:before method="beforeTime" pointcut-ref="pointcut"></aop:before>
            <aop:after method="afterTime" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值