Spring容器启动配置
2011-04-28 15:23
381 查看
什么时候spring容器启动?其实就是程序中执行加载 web.xml配置文件的时候。
Spring可以通过(Servlet)org.springframework.web.context.ContextLoaderServlet和Listener(org.springframework.web.context.ContextLoaderListener)两个类作为Spring启动的入口。
1.应用程序下加载
ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
applicationContext.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- dataSource config -->
<bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/game" />
<property name="username" value="root" />
<property name="password" value="root"/>
</bean>
<!-- SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation">
<value>classpath:com\sterning\bean\hibernate\hibernate.cfg.xml</value>
</property>
</bean>
<!-- TransactionManager 不过这里暂时没注入-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- DAO -->
<bean id="booksDao" class="com.sterning.books.dao.hibernate.BooksMapDao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- Services -->
<bean id="booksService" class="com.sterning.books.services.BooksService">
<property name="booksDao">
<ref bean="booksDao"/>
</property>
</bean>
<bean id="pagerService" class="com.sterning.commons.PagerService"/>
<!-- view -->
<bean id="bookAction" class="com.sterning.books.web.actions.BooksAction" singleton="false">
<property name="booksService">
<ref bean="booksService"/>
</property>
<property name="pagerService">
<ref bean="pagerService"/>
</property>
</bean>
</beans>
2.web模式下加载
web.xml:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- ContextConfigLocation -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
</context-param>
<!-- Listener contextConfigLocation -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Listener log4jConfigLocation -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
另外参考:
http://hi.baidu.com/aimy_xmu/blog/item/1c2a969b715d4cb0c9eaf4ee.html
Spring可以通过(Servlet)org.springframework.web.context.ContextLoaderServlet和Listener(org.springframework.web.context.ContextLoaderListener)两个类作为Spring启动的入口。
1.应用程序下加载
ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
applicationContext.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- dataSource config -->
<bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/game" />
<property name="username" value="root" />
<property name="password" value="root"/>
</bean>
<!-- SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation">
<value>classpath:com\sterning\bean\hibernate\hibernate.cfg.xml</value>
</property>
</bean>
<!-- TransactionManager 不过这里暂时没注入-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- DAO -->
<bean id="booksDao" class="com.sterning.books.dao.hibernate.BooksMapDao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- Services -->
<bean id="booksService" class="com.sterning.books.services.BooksService">
<property name="booksDao">
<ref bean="booksDao"/>
</property>
</bean>
<bean id="pagerService" class="com.sterning.commons.PagerService"/>
<!-- view -->
<bean id="bookAction" class="com.sterning.books.web.actions.BooksAction" singleton="false">
<property name="booksService">
<ref bean="booksService"/>
</property>
<property name="pagerService">
<ref bean="pagerService"/>
</property>
</bean>
</beans>
2.web模式下加载
web.xml:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- ContextConfigLocation -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
</context-param>
<!-- Listener contextConfigLocation -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Listener log4jConfigLocation -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
另外参考:
http://hi.baidu.com/aimy_xmu/blog/item/1c2a969b715d4cb0c9eaf4ee.html
相关文章推荐
- 关于web.xml启动spring容器的配置文件的建议
- Spring两个数据源配置在容器启动出错,No unique bean of type [org.springframework.jdbc.core.JdbcTemplate] is defined:
- 系统启动PropertyPlaceholderConfigurer加载配置到spring容器中
- spring容器配置及启动步骤
- web.xml中配置启动spring容器的参数
- 通过web.xml文件自动启动spring容器加载对应的配置文件
- Web容器启动时加载Spring的配置
- 流程图解Spring Framework(二) spring 如何启动容器的---XML配置
- Spring配置文件-(别名)、spring启动容器方式
- web.xml中配置Spring容器随项目启动
- spring源码:web容器启动(li)
- spring容器启动的加载过程(一)
- Spring之基于注解的容器配置
- 浅谈Spring的两种配置容器
- 手动启动spring容器
- Spring容器、Bean配置信息。Bean实现类及应用程序的关系
- Java opencsv--使用 Spring 的 PostConstruct机制,项目启动时,将CSV配置文件读取到内存 map 中
- Spring 容器的启动方式
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(2) - 静态资源、视图和消息器
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换