在web.xml中将BasicDataSource配置为bean

时间:2011-06-19 16:35:16

标签: spring tomcat java-ee javabeans web.xml

我正在尝试使用tomcat 6在tomcat项目下的web.xml中将org.apache.commons.dbcp.BasicDataSource配置为bean。(它是带有tomcat的red5,我们可以忽略主服务器实际上是red5因为我实际上在端口5080下运行jsp文件,并且不使用RTMP协议直接连接到red5)

我的web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/  j2ee/web-app_2_4.xsd" 
version="2.4"> 

<display-name>gamesisland-login-red5</display-name>

<context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>/[myapp]</param-value>
</context-param>

<listener>
    <listener-class>org.red5.logging.ContextLoggingListener</listener-class>
</listener>

<filter>
    <filter-name>LoggerContextFilter</filter-name>
    <filter-class>org.red5.logging.LoggerContextFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>LoggerContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>rtmpt</servlet-name>
    <servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rtmpt</servlet-name>
    <url-pattern>/fcs/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>rtmpt</servlet-name>
    <url-pattern>/open/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>rtmpt</servlet-name>
    <url-pattern>/close/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>rtmpt</servlet-name>
    <url-pattern>/send/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>rtmpt</servlet-name>
    <url-pattern>/idle/*</url-pattern>
</servlet-mapping>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Forbidden</web-resource-name>
        <url-pattern>/streams/*</url-pattern>
    </web-resource-collection>
    <auth-constraint/>
</security-constraint>

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/red5-web.properties" />
</bean>

   <bean id="idDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName"><value>${db.driver}</value></property>
            <property name="url"><value>${db.url}</value></property>
            <property name="username"><value>${db.username}</value></property>
            <property name="password"><value>${db.password}</value></property>
            <property name="poolPreparedStatements"><value>true</value></property>
            <property name="maxActive"><value>10</value></property>
            <property name="maxIdle"><value>10</value></property>
    </bean>


</web-app>

我的red5-web.properties:

webapp.contextPath=/myapp
webapp.virtualHosts=*
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/dbname
db.username=user
db.password=pass

tomcat会自动搜索WEB-INF / web.xml进行配置吗?

为什么我没有在创建idDataSource时遇到任何相关错误?

我真的不知道如何查明或调试问题。 任何援助将不胜感激。

谢谢你!

幼狮

1 个答案:

答案 0 :(得分:4)

我对Red5一无所知,但似乎你试图将Spring bean直接放在web.xml里面,这是错误的。您可以创建一个单独的Spring配置文件,该文件将由Springs ContextLoaderListener选取。首先,将其添加到web.xml

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

applicationContext.xml下创建/WEB-INF文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/WEB-INF/red5-web.properties" />
    </bean>

    <bean id="idDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>${db.driver}</value></property>
        <property name="url"><value>${db.url}</value></property>
        <property name="username"><value>${db.username}</value></property>
        <property name="password"><value>${db.password}</value></property>
        <property name="poolPreparedStatements"><value>true</value></property>
        <property name="maxActive"><value>10</value></property>
        <property name="maxIdle"><value>10</value></property>
     </bean>

</beans>

当然,所有<bean/>声明都应该远离web.xml

第二个想法:查看this文档,看起来Red5使用名为red5-web.xml的文件,请仔细阅读本文档。