这里不做详细的开发过程记录,主要记录一些开发过程遇到的问题和一些小技巧:
问题一:
spring配置 no matching editors or conversion strategy found 异常!
这是一个比较蛋疼的问题,笔者在自定义的shiro的Realm中调用了UserService,因为项目逻辑并不复杂,所以service层并没有抽象接口层,UserService是一个类。
然后在启动项目时报错:日志信息还算比较友好:对指定的一个类型解析错误,不是预计匹配的类型。
笔者理解是UserService调用时默认可能是接口,这里注入了类,所以不匹配,但笔者又不愿意加接口,所以百度了解有这样的配置:<aop:config proxy-target-class="true"></aop:config>,意思是打开类注入,然后……
然并卵,仍然报同一个错误,没有办法,最后硬生生多加了一个UserService接口,启动正常,有知道这么修改类注入的geek求指教!
配置文件如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置数据源 -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://localhost:3307/db_community?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations" value="classpath:mappings/**/*.xml"></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gcc.modules.*.dao" />
<!--<property name="annotationClass"-->
<!--value="com.gcc.modules.untils.annotation.MyBatisDao" />-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 自定义Realm -->
<bean id="myRealm" class="com.gcc.modules.shiro.MyRealm"/>
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm"/>
</bean>
<!-- Shiro过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,这个属性是必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 身份认证失败,则跳转到登录页面的配置 -->
<property name="loginUrl" value="/a.jsp"/>
<!-- Shiro连接约束配置,即过滤链的定义 -->
<property name="filterChainDefinitions">
<value>
/login=anon
/admin/**=authc
</value>
</property>
</bean>
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 开启Shiro注解 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!-- 配置事务通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="check*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<!--execution表达式-->
<!--第一个“*”符号表示返回值的类型任意;com.gcc.modules.*.service.* 表示AOP所切的服务的包名,即,需要进行横切的业务类-->
<!--.*表示所有类;.*(..)表示任何方法名,括号表示参数,两个点表示任何参数类型-->
<!--待解决:默认注入接口,这里开启注入类但没有生效,proxy-target-class="true"-->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.gcc.modules.*.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
<!-- 自动扫描 -->
<context:component-scan base-package="com.gcc.modules.*.service" />
<context:component-scan base-package="com.gcc.modules.*.dao" />
</beans>
感谢!
问题二:
org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.util.Map
项目通过mybatis方式调用ADao方法时报错,日志信息是:找不到指定的resultmap类型,而这个类型是B,B类型在ADao方法中并没有用到,不太了解mybatis对xml文件的处理过程,于是一步步排查。
是注解没有被扫描,还是mapper文件没有被读取,最后找到问题是BDao所在的另一个mapper文件的resultMap类型不是全路径名称,改为全路径即可。
或者还有一直原因也会报错,即返回类型是resultType,误写成resultMap