Code Bye

关于sessionFactory在bean.xml中注入失败的问题

bean.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:tx=”http://www.springframework.org/schema/tx”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd”>
<!– 自动扫描 –>
<context:component-scan base-package=”ktouch.util,ktouch.server,ktouch.base”/>
<!– 数据源 –>
<bean id=”dataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource”>
<property name=”driverClass” value=”com.mysql.jdbc.Driver” />
<property name=”jdbcUrl” value=”jdbc:mysql://localhost:3306/tyanalutics”/>
<property name=”user” value=”root” />
<property name=”password” value=”root” />
<property name=”maxPoolSize” value=”80″ />
<property name=”minPoolSize” value=”1″/>
<property name=”initialPoolSize” value=”1″ />
<!–   <property name=”maxIdleTime” value=”2500″ />  –>
</bean>
<!–配置sessionFactory–>
<bean id=”sessionFactory”  class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<!–<property name=”dataSource” ref=”dataSource”/>–>
<property name=”dataSource”>
<ref local=”dataSource”/>
</property>
<!– 映射文件的目录 –>
<property name=”mappingDirectoryLocations”>
<list>
<value>ktouch/base</value>
</list>
</property>
<!– 其他的hibernate常用配置 –>
<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</prop>
<prop key=”show_sql”>true</prop>
<prop key=”hibernate.hbm2ddl.auto”>update</prop>
</props>
</property>
</bean>
<!– hibernate事务管理器,在service层上实现事务管理,实现平台无关 –>
<bean id=”txManager” class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory” />
</bean>
<bean id=”userdao” class=”ktouch.util.UserDaoImpl”>
<property name=”sessionFactory”>
<ref bean=”sessionFactory”/>
</property>
</bean>
<!– <bean id=”UserDao” class=”ktouch.util.UserDaoImpl”>
<property name=”userdao” ref=”userdao”/>
</bean>–>
<!– 事务通知 –>
<tx:advice id=”txAdvice” transaction-manager=”txManager”>
<tx:attributes>
<tx:method name=”add*” propagation=”REQUIRED” isolation=”DEFAULT” />
<tx:method name=”updae*” propagation=”REQUIRED” isolation=”DEFAULT” />
<tx:method name=”delete*” propagation=”REQUIRED” isolation=”DEFAULT” />
<tx:method name=”batch*” propagation=”REQUIRED” isolation=”DEFAULT” />
<tx:method name=”load*” propagation=”REQUIRED” isolation=”DEFAULT” read-only=”true” />
<tx:method name=”get*” propagation=”REQUIRED” isolation=”DEFAULT” read-only=”true” />
<tx:method name=”find*” propagation=”REQUIRED” isolation=”DEFAULT” read-only=”true” />
<tx:method name=”*” propagation=”REQUIRED” isolation=”DEFAULT” />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref=”txAdvice” pointcut=”execution(* ktouch.server.*.*(..))” />
</aop:config>
<!– 激活@Controller模式 –>
<mvc:annotation-driven />
<mvc:resources mapping=”/style/**” location=”/WEB-INF/style/” />
<mvc:resources mapping=”/image/**” location=”/WEB-INF/image/” />
<mvc:resources mapping=”/script/*” location=”/WEB-INF/script/” />
<mvc:resources mapping=”*.jsp” location=”/WEB-INF/jsp/” />
<bean id=”viewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”prefix” value=”/WEB-INF/jsp/” />
<property name=”suffix” value=”.jsp” />
</bean>
<!–Spring3.1开始的注解 HandlerMapping,使Controller中类上的@RequestMapping 生效 –>
<bean class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping” />
<!–Spring3.1开始的注解 HandlerAdapter,使Controller中的方法上的@RequestMapping 生效 –>
<bean class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter”>
<property name=”messageConverters”>
<list>
<bean class=”org.springframework.http.converter.StringHttpMessageConverter”>
<property name=”supportedMediaTypes”>
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<ref bean=”jsonConverter” />
</list>
</property>
</bean>
<!– json转换器 –>
<bean id=”jsonConverter” class=”org.springframework.http.converter.json.MappingJacksonHttpMessageConverter”>
<property name=”supportedMediaTypes” value=”application/json” />
<property name=”objectMapper”>
<bean class=”org.codehaus.jackson.map.ObjectMapper”>
<property name=”dateFormat”>
<bean class=”java.text.SimpleDateFormat”>
<constructor-arg type=”java.lang.String” value=”yyyy-MM-dd HH:mm:ss” />
</bean>
</property>
</bean>
</property>
</bean>
</beans>
spring-server.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:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd”
default-lazy-init=”true”>
<context:annotation-config />
<!– 使Spring支持自动检测组件,如注解的Controller –>
<import resource=”classpath:/bean.xml” />
<context:component-scan base-package=”ktouch.controler”>
<!–<context:include-filter type=”regex” expression=”.util.*”/>
<context:include-filter type=”regex” expression=”.server.*”/>
<context:include-filter type=”regex” expression=”.base.*”/>–>
</context:component-scan>
<context:component-scan base-package=”ktouch.server”/>
<bean id=”viewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”
p:prefix=”/WEB-INF”
p:suffix=”.jsp” />
<bean class=”org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping” />
<!– 启动 Spring MVC 的注解功能,完成请求和注解 POJO 的映射 –>
<bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”>
<property name=”messageConverters”>
<list>
<bean class=”org.springframework.http.converter.StringHttpMessageConverter”>
</bean>
</list>
</property>
</bean>
</beans>
错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name “userdao”: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name “sessionFactory” defined in class path resource [bean.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Mapping directory location [ServletContext resource [/ktouch/base]] does not denote a directory
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
解决方案

70

试试加个 classpath 行不行
<property name=”mappingDirectoryLocations”>
<list>
<value>classpath:/ktouch/base</value>
</list>
</property>

30

确实要加classpath的,原因是你的配置文件在类路径下
速度慢是启动速度还是运行速度?启动速度慢是正常的,启动太快一般都是报错了~  运行慢就和项目无关了~ 机器内存的问题

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于sessionFactory在bean.xml中注入失败的问题