模板:
<?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>
所在項目: