導航:首頁 > 文件教程 > spring配置資源文件

spring配置資源文件

發布時間:2021-04-13 14:14:39

A. spring配置載入maven項目裡面的src/main/resoures下的靜態資源,配置路徑是file:

src/main/resoures這個路徑下的文件是在classpath下的,比如說這下面有一個
spring.xml ,在web.xml 里注冊的話就是類似於

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring.xml</param-value>
</context-param>

B. spring 怎麼載入配置文件

1.利用

可以從classpath中讀取XML文件
(1)
ApplicationContext context = new ("applicationContext.xml"); UserDao userDao = (UserDao)context.getBean("userDao");

(2)
resource = new (new

String[]{"applicationContext-ibatis-oracle.xml","applicationContext.xml","applicationContext-data-oracle.xml"});
BeanFactory factory = resource; UserDao userDao = (UserDao)
factory.getBean("userDao");

2. 利用ClassPathResource

可以從classpath中讀取XML文件
Resource cr = new ClassPathResource("applicationContext.xml");
BeanFactory bf=new XmlBeanFactory(cr); UserDao userDao =
(UserDao)bf.getBean("userDao");

C. Spring配置文件的作用是什麼,舉一個例子加以說明

Spring 通俗來說是用來關聯兩個對象的,對象和對象之間不需要實例化只需要在專 Spring配置文件中配置一下屬就可確定對象之間的依賴關系。
Spring兩大特點就是依賴注入和控制反轉.簡單來說就是本來你在代碼中得實例化對象實例化以後你得調用對象的方法。 但是用了Spring後你就可以直接在代碼中指向你的對象和對象的方法。這樣做的好處就是只需要知道對象名稱不需要知道對象具體是干什麼。 一旦對象改變只需要簡單的改一下配置文件即可。
Spring說專業的解釋初學者是很難看懂的 通俗的只能這樣解釋了呵呵 不過一定要結合Spring給出例子實踐一下才能體會到其中的奧妙

D. spring有哪些配置文件

spring 一般不說有哪些配置文件,都是問有哪些配置項的,即配置哪些bean
比如datasource,sqlsessiontemplate等等。。。。
還有像spring-boot,推崇0配置了,除了一個yml或者properties其他的都是自動配置

E. spring的配置文件怎麼寫

標準的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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true">

<!-- 配置數據源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>
jdbc:mysql://localhost/ssh?characterEncoding=utf-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>

<!--配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/ssh/pojo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- 事務管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!-- hibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!-- 配置數據持久層 -->
<bean id="userDao"
class="com.ssh..impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>

<!-- 配置業務邏輯層 -->
<bean id="userService"
class="com.ssh.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>

<!-- 配置控制層 -->
<bean id="UserAction"
class="com.ssh.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<!-- 配置pojo -->
<bean id="User" class="com.ssh.pojo.User" scope="prototype"/>
</beans>

F. spring配置文件!

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<-- session工廠節點,將Hibernate的session工廠注入到Spring的配置文件中 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>

<-- 設置事務代理類,並將session工廠對象引入事務中 -->

<bean id="myHibTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<-- 引入事務代理對象及其相關配置常量,並通過abstract屬性將本節點設置為父類,子類子要繼承此類(設置parent屬性),就可直接使用此類的屬性也就是事務,無需每個節點再設置 -->
<bean id="base" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="myHibTransactionManager">
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
《-- DAO節點,需要注入session工廠對象 --》
<bean id="userDao" class="userDao.impl.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
《-- 具體對對象進行調用的使用類,直接在此通告ref引入userDao對象即可在該類中直接調用userDao對象,但是前提是在UserBiz類中要設置userDao的屬性聲明和setter方法 --》
<bean id="userBizTarget" class="UserBiz.impl.UserBiz">
<property name="userDao" ref="userDao"></property>
</bean>
《-- 設置此類繼承事務代理父類,通告parent屬性繼承父類屬性 --》
<bean id="userBiz" parent="base">
<property name="target" ref="userBizTarget"></property>
</bean>
《-- 對action節點的設置,通過class找到action的具體路徑,name屬性是struts配置文件中節點path,ref注入相關對象,在struts配置中要修改相關action節點的type屬性 --》
<bean name="/users" class="com.yourcompany.struts.action.UsersActionAction">
<property name="userBiz" ref="userBiz"></property>
</bean>
</beans>

G. spring配置文件的名字

來看一個標準的Spring配置文件 applicationContext.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:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">

H. spring 註解 通過什麼配置文件

一:在工程中建立config.properties配置文件
[java] view plain
#FTP的ip地址
address=192.168.25.133
#FTP的埠
port=21

二:在spring配置文件中配置載入上述的資源文件。(兩種方式)
方式1:s
[java] view plain
<context:property-placeholder location="classpath:config/*.properties"/>
方式2;
[java] view plain
<bean id="propertyBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>

I. spring 配置文件

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<-- session工廠節點,將Hibernate的session工廠注入到Spring的配置文件中 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>

<-- 設置事務代理類,並將session工廠對象引入事務中 -->

<bean id="myHibTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<-- 引入事務代理對象及其相關配置常量,並通過abstract屬性將本節點設置為父類,子類子要繼承此類(設置parent屬性),就可直接使用此類的屬性也就是事務,無需每個節點再設置 -->
<bean id="base" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="myHibTransactionManager">
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
《-- DAO節點,需要注入session工廠對象 --》
<bean id="userDao" class="userDao.impl.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
《-- 具體對對象進行調用的使用類,直接在此通告ref引入userDao對象即可在該類中直接調用userDao對象,但是前提是在UserBiz類中要設置userDao的屬性聲明和setter方法 --》
<bean id="userBizTarget" class="UserBiz.impl.UserBiz">
<property name="userDao" ref="userDao"></property>
</bean>
《-- 設置此類繼承事務代理父類,通告parent屬性繼承父類屬性 --》
<bean id="userBiz" parent="base">
<property name="target" ref="userBizTarget"></property>
</bean>
《-- 對action節點的設置,通過class找到action的具體路徑,name屬性是struts配置文件中節點path,ref注入相關對象,在struts配置中要修改相關action節點的type屬性 --》
<bean name="/users" class="com.yourcompany.struts.action.UsersActionAction">
<property name="userBiz" ref="userBiz"></property>
</bean>
</beans>
請採納。

J. spring的配置文件放在什麼位置

這個不是一定的,隨你自己的意思,你可以放在WEB-INF里,也可以放在classpath下。只需在配置web.xml時指定位置即可。

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:beans.xml
</param-value>
</context-param>

上面就是web.xml中對spring容器的初始化配置,<context-param>中<param-value>中的classpath:beans.xml 即是spring配置文件beans.xml的位置(classpath下,在myeclipse的工程中是src目錄下)

閱讀全文

與spring配置資源文件相關的資料

熱點內容
網路中常用的傳輸介質 瀏覽:518
文件如何使用 瀏覽:322
同步推密碼找回 瀏覽:865
樂高怎麼才能用電腦編程序 瀏覽:65
本機qq文件為什麼找不到 瀏覽:264
安卓qq空間免升級 瀏覽:490
linux如何刪除模塊驅動程序 瀏覽:193
at89c51c程序 瀏覽:329
怎麼創建word大綱文件 瀏覽:622
裊裊朗誦文件生成器 瀏覽:626
1054件文件是多少gb 瀏覽:371
高州禁養區內能養豬多少頭的文件 瀏覽:927
win8ico文件 瀏覽:949
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42

友情鏈接