spring Java web 配置jndi

1、错误提示:Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 9 in XML document from ServletContext resource [/WEB-INF/appcontext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The content of element type "property" must match "(description?,(bean|ref|idref|value|null|list|set|map|props)?)".
解决办法:
再web.xml文件中加入如下语句:
<listener>
      
  
<listener-class>

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

2.正确的做法:
在servel.xml加入如下代码:
<Context path="/POS" docBase="POS"
	         debug="5" reloadable="true" crossContext="true"   workDir="">
	   <Resource   name="jdbc/pos"
	              auth="Container"
	     type="javax.sql.DataSource"
	              maxActive="100" maxIdle="30" maxWait="10000"
	              username="root" password="1234"
	     driverClassName="org.gjt.mm.mysql.Driver"
	     url="jdbc:mysql://localhost/pos"/>
</Context>  

3.在appcontext.xml加入如下信息:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
     <!--通过JNDI获得数据源的引用-->
     <bean id="MyDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
         <property name="jndiName">
              <value>java:comp/env/jdbc/pos</value>
         </property>
     </bean>
     
     <!--配置Hibernate-->
     <bean
       id="sessionFactory" 
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
     >
     	<!--指定数据源-->
     	<property name="dataSource">
     		<ref local="MyDataSource" />
     	</property>
     	
     	<!--给出映射文件资源-->
     	<property name="mappingResources">
     		<list>
     			<value>AdminInfo.hbm.xml</value>
     			<value>GoodsInfo.hbm.xml</value>
     			<value>GoodsClassInfo.hbm.xml</value>
     			<value>ConsumerInfo.hbm.xml</value>
     			<value>ProviderInfo.hbm.xml</value>
     			<value>StockInfo.hbm.xml</value>
     			<value>StockDetail.hbm.xml</value>
     			<value>SellInfo.hbm.xml</value>
     			<value>SellDetail.hbm.xml</value>
     			<value>ConsumerBack.hbm.xml</value>
     			<value>ConsumerBackDetail.hbm.xml</value>
     			<value>ProviderBack.hbm.xml</value>
     			<value>ProviderBackDetail.hbm.xml</value>
     		</list>
     	</property>
     	
     	<!--定义hibernate配置属性-->
     	<property name="hibernateProperties">
     		<props>
     			<prop key="hibernate.dialect">
     			    org.hibernate.dialect.MySQLDialect
     			</prop>
     			<prop key="hibernate.show_sql">
     			    true
     			</prop>     		
     		</props>
     	</property>     
     </bean>
     <bean id="DButil" class="wyf.zrk.DButil">
         <property name="sf">
              <ref local="sessionFactory" />
         </property>
     </bean> 
     <bean id="DBupdate" class="wyf.zrk.DBupdate">
         <property name="sf">
              <ref local="sessionFactory" />
         </property>
         <property name="db">
         			<ref local="DButil" />
         </property>
     </bean>  
     <bean id="DBinsert" class="wyf.zrk.DBinsert">
         <property name="sf">
              <ref local="sessionFactory" />
         </property>
         <property name="db">
         			<ref local="DButil" />
         </property>
     </bean>   
     <bean id="DBdelete" class="wyf.zrk.DBdelete">
         <property name="sf">
              <ref local="sessionFactory" />
         </property>
         <property name="db">
         			<ref local="DButil" />
         </property>
     </bean>  
</beans>

4.web.xml文件内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<!--web¼¯³ÉSpringÅäÖÃ==== begin -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/appcontext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--web¼¯³ÉSpringÅäÖÃ==== end -->


	<resource-ref>
		<description>DB Connection</description>
		<res-ref-name>jdbc/pos</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

	<servlet>
		<servlet-name>ManageServlet</servlet-name>
		<servlet-class>wyf.zrk.ManageServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ManageServlet</servlet-name>
		<url-pattern>/ManageServlet</url-pattern>
	</servlet-mapping>




	<welcome-file-list>
		<welcome-file>
            login.jsp
        </welcome-file>
	</welcome-file-list>
</web-app>

5.加入相应的类库,添加一些相应的类,添加一些映射文件 。

你可能感兴趣的:(java,spring)