更新時間:2021-06-16 11:45:41 來源:動力節(jié)點 瀏覽1123次
模板:
<?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添加
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
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>
<!-- 引入db.properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置C3P0數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverName}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.pwd}"></property>
</bean>
<!-- 配置 Spring 的 org.springframework.jdbc.core.JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="moocBeanNameAware" class="imooc_spring.test.aware.MoocBeanNameAware"></bean>
<!-- 測試 SpEL: 可以為屬性進行動態(tài)的賦值(了解) -->
<bean id="girl" class="com.helloworld.User">
<property name="userName" value="周迅"></property>
</bean>
<!-- <bean id="boy" class="com.helloworld.User" init-method="init" destroy-method="destroy">
<property name="userName" value="高勝遠"></property> <property name="wifeName"
value="#{girl.userName}"></property> </bean> -->
<bean id="girl2" class="com.helloworld.User2">
<property name="userName" value="Talor Swift"></property>
</bean>
<!-- autowired測試,自動裝配測試 -->
<bean id="people" class="test.spring.autowired.Person" scope="prototype"
autowire="byName">
<property name="name" value="小明"></property>
<!-- <property name="cat" ref="cat222"></property> -->
<!-- <property name="cat" ref="cat1"></property> -->
</bean>
<bean id="cat" class="test.spring.autowired.Cat" scope="prototype">
<property name="name" value="波斯貓"></property>
</bean>
<!-- <bean id="cat222" class="test.spring.autowired.Cat"> <property name="name"
value="我是小喵喵"></property> </bean> -->
<bean id="people2" class="test.spring.autowired.Person" scope="prototype"
autowire="byName">
<property name="name" value="小明"></property>
<property name="cat" ref="cat222"></property>
</bean>
<bean id="cat222" class="test.spring.autowired.Cat" scope="prototype">
<property name="name" value="波斯貓"></property>
</bean>
<!--context:component-scan 指定 掃描的包 -->
<!--可以通過 resource-pattern 指定掃描的資源, resource-pattern="myrepository/*.class"
的含義: 只掃描 base-package 對應包下的 目錄為 myrepository 的所有java Bean -->
<!-- <context:component-scan base-package="imooc_spring.test.anotation"
resource-pattern="myrepository/*.class"></context:component-scan> -->
<!-- context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
子節(jié)點指定排除哪些注解 context:include-filter type="annotation" 需要結合context:component-scan
標簽的 use-default-filters="false"來使用 context:exclude-filter type="assignable"
這個expression指的是自己寫的類,意思排除哪些類 expression="imooc_spring.test.anotation.TestObj" -->
<context:component-scan base-package="imooc_spring.test.anotation">
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
/> -->
<!-- <context:exclude-filter type="assignable" expression="imooc_spring.test.anotation.TestObj"
/> -->
</context:component-scan>
<context:component-scan base-package="com.aop"></context:component-scan>
<!-- aop測試,需要引入aop命名空間 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- aop annotationType, -->
<!-- 切點的bean -->
<bean class="com.aop.xmltype.CalculatorImplxml" id="calImplxml"></bean>
<!-- 切面的bean -->
<bean class="com.aop.xmltype.MyAspectxml" id="myaspxml"></bean>
<bean class="com.aop.xmltype.Diary" id="myDiary"></bean>
<!-- aop xmlType,用xml的形式配置AOP前置通知 -->
<aop:config>
<!--aop:pointcut 其實放在這兒也可以 -->
<!-- <aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" /> -->
<!-- 配置切面和通知 ,aop:aspect標簽需要通過ref指定配置好的bean,id隨便配置或者不配置,id的值可以隨意起 -->
<aop:aspect id="myaspxml" ref="myaspxml" order="2">
<!-- 配置切點,即 要被記日記的對象, aop:pointcut 放在這兒也可以 ,切點不需要根對應的bean相關聯,
只要expression指定的方法所在的類被Spring掃描得到就行,即只要所在的類配置了bean就可以 -->
<aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" />
<!-- 切面里的具體的用于記錄的方法就是一個通知,需要用通過pointcut-ref來指定具體的切點, -->
<aop:before method="logBefore" pointcut-ref="pointcut1" />
<aop:after method="logAfter" pointcut-ref="pointcut1" />
</aop:aspect>
<aop:aspect ref="myDiary" order="3">
<!-- execution (* com.aop.*.*.*(..)) 包含了 com.aop.xmltype.CalculatorImplxml.*(..)) 的這種情況 -->
<!-- <aop:pointcut expression="execution (* com.aop.*.*.*(..))" id="allPointcut"/> -->
<aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))" id="allPointcut"/>
<aop:before method="myEnd" pointcut-ref="allPointcut"/>
</aop:aspect>
</aop:config>
</beans>
所在項目:
以上就是動力節(jié)點小編介紹的"Spring配置文件模板",希望對大家有幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為您服務。
0基礎 0學費 15天面授
有基礎 直達就業(yè)
業(yè)余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習