Spring配置文件模板

模板:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean id="moocAppctx" class="imooc_spring.test.aware.MoocApplicationContext"
        init-method="hhhh">
    </bean>
</beans>

 

更全面的一个模板,20170327添加,

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans"
  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4     xmlns:aop="http://www.springframework.org/schema/aop"
  5 
  6     xsi:schemaLocation="
  7     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  8     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 10     <bean id="moocAppctx" class="imooc_spring.test.aware.MoocApplicationContext"
 11         init-method="hhhh">
 12     </bean>
 13 
 14     <!-- 引入db.properties -->
 15     <context:property-placeholder location="classpath:db.properties" />
 16 
 17     <!-- 配置C3P0数据源 -->
 18     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 19         <property name="jdbcUrl" value="${jdbc.url}"></property>
 20         <property name="driverClass" value="${jdbc.driverName}"></property>
 21         <property name="user" value="${jdbc.username}"></property>
 22         <property name="password" value="${jdbc.pwd}"></property>
 23     </bean>
 24 
 25     <!-- 配置 Spring 的 org.springframework.jdbc.core.JdbcTemplate -->
 26     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 27         <property name="dataSource" ref="dataSource"></property>
 28     </bean>
 29 
 30     <bean id="moocBeanNameAware" class="imooc_spring.test.aware.MoocBeanNameAware"></bean>
 31 
 32     <!-- 测试 SpEL: 可以为属性进行动态的赋值(了解) -->
 33     <bean id="girl" class="com.helloworld.User">
 34         <property name="userName" value="周迅"></property>
 35     </bean>
 36 
 37     <!-- <bean id="boy" class="com.helloworld.User" init-method="init" destroy-method="destroy"> 
 38         <property name="userName" value="高胜远"></property> <property name="wifeName" 
 39         value="#{girl.userName}"></property> </bean> -->
 40 
 41     <bean id="girl2" class="com.helloworld.User2">
 42         <property name="userName" value="Talor Swift"></property>
 43     </bean>
 44 
 45     <!-- autowired测试,自动装配测试 -->
 46     <bean id="people" class="test.spring.autowired.Person" scope="prototype"
 47         autowire="byName">
 48         <property name="name" value="小明"></property>
 49         <!-- <property name="cat" ref="cat222"></property> -->
 50         <!-- <property name="cat" ref="cat1"></property> -->
 51     </bean>
 52 
 53     <bean id="cat" class="test.spring.autowired.Cat" scope="prototype">
 54         <property name="name" value="波斯猫"></property>
 55     </bean>
 56     <!-- <bean id="cat222" class="test.spring.autowired.Cat"> <property name="name" 
 57         value="我是小喵喵"></property> </bean> -->
 58 
 59 
 60 
 61     <bean id="people2" class="test.spring.autowired.Person" scope="prototype"
 62         autowire="byName">
 63         <property name="name" value="小明"></property>
 64         <property name="cat" ref="cat222"></property>
 65     </bean>
 66 
 67     <bean id="cat222" class="test.spring.autowired.Cat" scope="prototype">
 68         <property name="name" value="波斯猫"></property>
 69     </bean>
 70 
 71     <!--context:component-scan 指定 扫描的包 -->
 72     <!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class" 
 73         的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
 74     <!-- <context:component-scan base-package="imooc_spring.test.anotation" 
 75         resource-pattern="myrepository/*.class"></context:component-scan> -->
 76 
 77     <!-- context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" 
 78         子节点指定排除哪些注解 context:include-filter type="annotation" 需要结合context:component-scan 
 79         标签的 use-default-filters="false"来使用 context:exclude-filter type="assignable" 
 80         这个expression指的是自己写的类,意思排除哪些类 expression="imooc_spring.test.anotation.TestObj" -->
 81     <context:component-scan base-package="imooc_spring.test.anotation">
 82         <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" 
 83             /> -->
 84 
 85         <!-- <context:exclude-filter type="assignable" expression="imooc_spring.test.anotation.TestObj" 
 86             /> -->
 87 
 88 
 89     </context:component-scan>
 90     <context:component-scan base-package="com.aop"></context:component-scan>
 91 
 92     <!-- aop测试,需要引入aop命名空间 -->
 93     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 94 
 95 
 96     <!-- aop annotationType, -->
 97 
 98 
 99     <!-- 切点的bean -->
100     <bean class="com.aop.xmltype.CalculatorImplxml" id="calImplxml"></bean>
101     <!-- 切面的bean -->
102     <bean class="com.aop.xmltype.MyAspectxml" id="myaspxml"></bean>
103     
104     <bean class="com.aop.xmltype.Diary" id="myDiary"></bean>
105     <!-- aop xmlType,用xml的形式配置AOP前置通知 -->
106     <aop:config>
107         <!--aop:pointcut 其实放在这儿也可以 -->
108         <!-- <aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))" 
109             id="pointcut1" /> -->
110 
111         <!-- 配置切面和通知 ,aop:aspect标签需要通过ref指定配置好的bean,id随便配置或者不配置,id的值可以随意起 -->
112         <aop:aspect id="myaspxml" ref="myaspxml" order="2">
113             <!-- 配置切点,即 要被记日记的对象, aop:pointcut 放在这儿也可以 ,切点不需要根对应的bean相关联,
114              只要expression指定的方法所在的类被Spring扫描得到就行,即只要所在的类配置了bean就可以 -->
115             <aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
116                 id="pointcut1" />
117             <!-- 切面里的具体的用于记录的方法就是一个通知,需要用通过pointcut-ref来指定具体的切点, -->
118             <aop:before method="logBefore" pointcut-ref="pointcut1" />
119             <aop:after method="logAfter" pointcut-ref="pointcut1" />
120         </aop:aspect>
121         
122         <aop:aspect ref="myDiary" order="3">
123             <!-- execution (* com.aop.*.*.*(..))  包含了 com.aop.xmltype.CalculatorImplxml.*(..)) 的这种情况  -->
124             <!-- <aop:pointcut expression="execution (* com.aop.*.*.*(..))" id="allPointcut"/> -->
125             <aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))" id="allPointcut"/>
126             <aop:before method="myEnd" pointcut-ref="allPointcut"/>
127         </aop:aspect>
128 
129     </aop:config>
130 
131 </beans>

所在项目:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值