at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1216)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1135)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bolo.examples.base.service.RoleService com.bolo.examples.base.controller.RoleController.roleService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bolo.orm.mybatis.MyBatisDao com.bolo.examples.base.service.RoleService.myBatisDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBatisDao' defined in URL [file:/D:/apache-tomcat-7.0.19-windows-x86/apache-tomcat-7.0.19/webapps/ssm3_2/WEB-INF/classes/service-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/D:/apache-tomcat-7.0.19-windows-x86/apache-tomcat-7.0.19/webapps/ssm3_2/WEB-INF/classes/service-context.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mapper/UserMapper.xml
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
... 27 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bolo.orm.mybatis.MyBatisDao com.bolo.examples.base.service.RoleService.myBatisDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBatisDao' defined in URL [file:/D:/apache-tomcat-7.0.19-windows-x86/apache-tomcat-7.0.19/webapps/ssm3_2/WEB-INF/classes/service-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/D:/apache-tomcat-7.0.19-windows-x86/apache-tomcat-7.0.19/webapps/ssm3_2/WEB-INF/classes/service-context.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mapper/UserMapper.xml
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:266)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:844)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:786)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 29 more
5 个解决方案
#1
sertvice没有注入controller,贴配置文件看看呗
#2
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<description>Spring-database配置</description>
<context:component-scan base-package="com.bolo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- mybatis Dao -->
<bean id="myBatisDao" class="com.bolo.orm.mybatis.MyBatisDao" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.bolo.examples..*Manager.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<description>Spring-database配置</description>
<context:component-scan base-package="com.bolo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- mybatis Dao -->
<bean id="myBatisDao" class="com.bolo.orm.mybatis.MyBatisDao" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.bolo.examples..*Manager.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
</beans>
#3
<?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: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">
<description>Spring-web MVC配置</description>
<!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.bolo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 当请求应用程序首页时,将执行/hello请求,还可以设定成redirect -->
<mvc:view-controller path="/" view-name="forward:/hello" />
<!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<!-- 模板后缀,指定html页面为模板 -->
<property name="suffix" value=".html" />
<!-- 使用这个模板类来解析视图 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="exposeSpringMacroHelpers" value="true" />
<!-- 允许访问请求属性,默认为false -->
<property name="exposeRequestAttributes" value="true" />
<!-- 允许访问会话属性,默认为false -->
<property name="exposeSessionAttributes" value="true" />
<!-- 页面上下文,类似于request.contextPath -->
<property name="requestContextAttribute" value="rc" />
<!-- 模板输出内容编码,此处应与defaultEncoding保持一致 -->
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 模板文件所在目录 -->
<property name="templateLoaderPath" value="/pages/" />
<!-- 指定FreeMarker模板文件的编码格式 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- FreeMarker属性配置 -->
<property name="freemarkerSettings">
<props>
<!--
每隔5小时检查模板是否更新,单位为秒
如果不经常更新模板可将更新的延迟时间设定长一点
-->
<prop key="template_update_delay">18000</prop>
<!-- 指定地区语言环境,我们的语言是中文 -->
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
</beans>
<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: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">
<description>Spring-web MVC配置</description>
<!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.bolo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 当请求应用程序首页时,将执行/hello请求,还可以设定成redirect -->
<mvc:view-controller path="/" view-name="forward:/hello" />
<!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<!-- 模板后缀,指定html页面为模板 -->
<property name="suffix" value=".html" />
<!-- 使用这个模板类来解析视图 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="exposeSpringMacroHelpers" value="true" />
<!-- 允许访问请求属性,默认为false -->
<property name="exposeRequestAttributes" value="true" />
<!-- 允许访问会话属性,默认为false -->
<property name="exposeSessionAttributes" value="true" />
<!-- 页面上下文,类似于request.contextPath -->
<property name="requestContextAttribute" value="rc" />
<!-- 模板输出内容编码,此处应与defaultEncoding保持一致 -->
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 模板文件所在目录 -->
<property name="templateLoaderPath" value="/pages/" />
<!-- 指定FreeMarker模板文件的编码格式 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- FreeMarker属性配置 -->
<property name="freemarkerSettings">
<props>
<!--
每隔5小时检查模板是否更新,单位为秒
如果不经常更新模板可将更新的延迟时间设定长一点
-->
<prop key="template_update_delay">18000</prop>
<!-- 指定地区语言环境,我们的语言是中文 -->
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
</beans>
#4
roleController这个控制器里面注入roleService的时候出错,你确定mybatis和spring 整合起来了?有没有测试过数据库连接?以及注入?
#5
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 加载数据源配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:data-source.properties</value>
</list>
</property>
</bean>
<!-- 定义 主数据源 -->
<bean id="cmsOracle" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${cms_DriverClassName}" />
<property name="url" value="${cms_url}" />
<property name="username" value="${cms_username}" />
<property name="password" value="${cms_password}" />
<property name="initialSize" value="${cms_initialSize}" />
<property name="maxActive" value="${cms_maxActive}"/>
<property name="maxWait" value="${cms_maxWait}"/>
<property name="maxIdle" value="${cms_maxIdle}"/>
<property name="minIdle" value="${cms_minIdle}"/>
<property name="removeAbandoned" value="${cms_removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${cms_removeAbandonedTimeout}"/>
<property name="testWhileIdle" value="${cms_testWhileIdle}"/>
<property name="testOnBorrow" value="${cms_testOnBorrow}"/>
<property name="testOnReturn" value="${cms_testOnReturn}"/>
<property name="validationQuery" value="${cms_validationQuery}"/>
<property name="validationQueryTimeout" value="${cms_validationQueryTimeout}"/>
<property name="timeBetweenEvictionRunsMillis" value="${cms_timeBetweenEvictionRunsMillis}"/>
<property name="numTestsPerEvictionRun" value="${cms_numTestsPerEvictionRun}"/>
</bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 开启注解配置 -->
<context:annotation-config />
<context:component-scan base-package="com.xx.service" />
<!-- 开启事务注解驱动 -->
<tx:annotation-driven />
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="cmsOracle" />
</bean>
<!-- 为每个数据源单独定义 SqlSessionFactory -->
<bean id="cmsSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="cmsOracle" />
</bean>
<!-- 为每个数据源单独定义 SqlMapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao层路径" />
<property name="sqlSessionFactory" ref="cmsSqlSessionFactory" />
</bean>
</beans>
基于注解的配置,你可以参考一下
#1
sertvice没有注入controller,贴配置文件看看呗
#2
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<description>Spring-database配置</description>
<context:component-scan base-package="com.bolo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- mybatis Dao -->
<bean id="myBatisDao" class="com.bolo.orm.mybatis.MyBatisDao" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.bolo.examples..*Manager.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<description>Spring-database配置</description>
<context:component-scan base-package="com.bolo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<!-- mybatis Dao -->
<bean id="myBatisDao" class="com.bolo.orm.mybatis.MyBatisDao" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.bolo.examples..*Manager.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
</beans>
#3
<?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: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">
<description>Spring-web MVC配置</description>
<!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.bolo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 当请求应用程序首页时,将执行/hello请求,还可以设定成redirect -->
<mvc:view-controller path="/" view-name="forward:/hello" />
<!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<!-- 模板后缀,指定html页面为模板 -->
<property name="suffix" value=".html" />
<!-- 使用这个模板类来解析视图 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="exposeSpringMacroHelpers" value="true" />
<!-- 允许访问请求属性,默认为false -->
<property name="exposeRequestAttributes" value="true" />
<!-- 允许访问会话属性,默认为false -->
<property name="exposeSessionAttributes" value="true" />
<!-- 页面上下文,类似于request.contextPath -->
<property name="requestContextAttribute" value="rc" />
<!-- 模板输出内容编码,此处应与defaultEncoding保持一致 -->
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 模板文件所在目录 -->
<property name="templateLoaderPath" value="/pages/" />
<!-- 指定FreeMarker模板文件的编码格式 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- FreeMarker属性配置 -->
<property name="freemarkerSettings">
<props>
<!--
每隔5小时检查模板是否更新,单位为秒
如果不经常更新模板可将更新的延迟时间设定长一点
-->
<prop key="template_update_delay">18000</prop>
<!-- 指定地区语言环境,我们的语言是中文 -->
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
</beans>
<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: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">
<description>Spring-web MVC配置</description>
<!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.bolo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 当请求应用程序首页时,将执行/hello请求,还可以设定成redirect -->
<mvc:view-controller path="/" view-name="forward:/hello" />
<!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<!-- 模板后缀,指定html页面为模板 -->
<property name="suffix" value=".html" />
<!-- 使用这个模板类来解析视图 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="exposeSpringMacroHelpers" value="true" />
<!-- 允许访问请求属性,默认为false -->
<property name="exposeRequestAttributes" value="true" />
<!-- 允许访问会话属性,默认为false -->
<property name="exposeSessionAttributes" value="true" />
<!-- 页面上下文,类似于request.contextPath -->
<property name="requestContextAttribute" value="rc" />
<!-- 模板输出内容编码,此处应与defaultEncoding保持一致 -->
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 模板文件所在目录 -->
<property name="templateLoaderPath" value="/pages/" />
<!-- 指定FreeMarker模板文件的编码格式 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- FreeMarker属性配置 -->
<property name="freemarkerSettings">
<props>
<!--
每隔5小时检查模板是否更新,单位为秒
如果不经常更新模板可将更新的延迟时间设定长一点
-->
<prop key="template_update_delay">18000</prop>
<!-- 指定地区语言环境,我们的语言是中文 -->
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
</beans>
#4
roleController这个控制器里面注入roleService的时候出错,你确定mybatis和spring 整合起来了?有没有测试过数据库连接?以及注入?
#5
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 加载数据源配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:data-source.properties</value>
</list>
</property>
</bean>
<!-- 定义 主数据源 -->
<bean id="cmsOracle" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${cms_DriverClassName}" />
<property name="url" value="${cms_url}" />
<property name="username" value="${cms_username}" />
<property name="password" value="${cms_password}" />
<property name="initialSize" value="${cms_initialSize}" />
<property name="maxActive" value="${cms_maxActive}"/>
<property name="maxWait" value="${cms_maxWait}"/>
<property name="maxIdle" value="${cms_maxIdle}"/>
<property name="minIdle" value="${cms_minIdle}"/>
<property name="removeAbandoned" value="${cms_removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${cms_removeAbandonedTimeout}"/>
<property name="testWhileIdle" value="${cms_testWhileIdle}"/>
<property name="testOnBorrow" value="${cms_testOnBorrow}"/>
<property name="testOnReturn" value="${cms_testOnReturn}"/>
<property name="validationQuery" value="${cms_validationQuery}"/>
<property name="validationQueryTimeout" value="${cms_validationQueryTimeout}"/>
<property name="timeBetweenEvictionRunsMillis" value="${cms_timeBetweenEvictionRunsMillis}"/>
<property name="numTestsPerEvictionRun" value="${cms_numTestsPerEvictionRun}"/>
</bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 开启注解配置 -->
<context:annotation-config />
<context:component-scan base-package="com.xx.service" />
<!-- 开启事务注解驱动 -->
<tx:annotation-driven />
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="cmsOracle" />
</bean>
<!-- 为每个数据源单独定义 SqlSessionFactory -->
<bean id="cmsSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="cmsOracle" />
</bean>
<!-- 为每个数据源单独定义 SqlMapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao层路径" />
<property name="sqlSessionFactory" ref="cmsSqlSessionFactory" />
</bean>
</beans>
基于注解的配置,你可以参考一下