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目录下)