spring问题记录

问题1:

使用spring aop时有如下的错误:

三月 31, 2020 10:24:22 上午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: com.wang.service.impl [Xlint:invalidAbsoluteTypeName]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: com.wang.service.impl [Xlint:invalidAbsoluteTypeName]

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at com.wang.advice.AopTest.test(AopTest.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.IllegalArgumentException: warning no match for this type name: com.wang.service.impl [Xlint:invalidAbsoluteTypeName]
    at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:319)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:227)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.obtainPointcutExpression(AspectJExpressionPointcut.java:198)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:177)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:226)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:289)
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:321)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:126)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:95)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:76)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:347)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:299)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
    ... 33 more

\

 

配置文件写错了:execution()表达式写错了:

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="UserServiceImpl" class="com.wang.service.impls.UserServiceImpl"/>
    <bean id="userAdvice" class="com.wang.service.advice.ServiceAdvice" ></bean>

    <aop:config >
<!--        * 类.方法(..) 这是一个错误写法-->
        <aop:pointcut id="userService" expression="execution(*  com.wang.service.impls.*(..))"/>
        <aop:advisor advice-ref="userAdvice" pointcut-ref="userService"/>
    </aop:config>

</beans>

正确:

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="UserServiceImpl" class="com.wang.service.impls.UserServiceImpl"/>
    <bean id="userAdvice" class="com.wang.service.advice.ServiceAdvice" ></bean>

    <aop:config >
<!--        * 类.方法(..) 这是正确写法-->
        <aop:pointcut id="userService" expression="execution(*  com.wang.service.impls.*.*(..))"/>
        <aop:advisor advice-ref="userAdvice" pointcut-ref="userService"/>
    </aop:config>

</beans>

 

问题2  字符集问题

运行spring项目时报字符问题:

项目编码设置为utf-8就可以解决:

 

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字节的 UTF-8 序列的字节 2 无效。
 

com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字节的 UTF-8 序列的字节 2 无效。

问题3:数据源配置问题

由于配置数据源信息的配置文件中的变量名称是如下配置格式导致spring配置文件中没有成功获取到值:

driver= com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=1111

修改之后成功:

jdbc.driver= com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=1111

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'hp'@'localhost' (using password: YES)
### The error may exist in class path resource [com/wang/mapper/UserMapper.xml]
### The error may involve com.wang.mapper.UserMapper.queryUsers
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'hp'@'localhost' (using password: YES)

问题4:对象类型不匹配

为了测试事务,当把切入点配置为mapper包之后,后面其他类引用该包的类报错。

BlogManagerImpl类中使用了com.wang.mapper包中的类BlogMapperImpl
<!--    在此处配置mapper包下面所有类之后,类会被代理,导致下面再注入该类对象报错-->
    <aop:config >
        <aop:pointcut id="txManager" expression="execution(* com.wang.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txManager"></aop:advisor>
    </aop:config>

    <bean id="blogService" class="com.wang.service.impl.BlogManagerImpl">
        <property name="bmi" ref="blogMapperImpl"></property>
    </bean>

 

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogService' defined in class path resource [mybatis02.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy7 implementing com.wang.mapper.BlogMapper,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'com.wang.mapper.BlogMapperImpl' for property 'bmi'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy7 implementing com.wang.mapper.BlogMapper,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'com.wang.mapper.BlogMapperImpl' for property 'bmi': no matching editors or conversion strategy found

spring 配置文件: 

<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--    datasource和sqlsessionfactory配置一次基本不会改动,所以配置到另外一个配置文件,用到的时候引入-->
    <import resource="applicationContext.xml"></import>


    <!--     3 此处需要增加Mapper的实现类,该类通过继承SessionTemplateDaoSupport类得到sqlsession-->
    <!--     4 注册该实现类,注入sqlSessionFactory或SqlSessionTemplate-->
    <bean id="blogMapperImpl" class="com.wang.mapper.BlogMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>

    </bean>

    <tx:advice id="txAdvice"  transaction-manager="transactionManager" >
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>

    </tx:advice>

<!--    MapperTest只是用例测试的类,没有注册对象,配置在这里,事务并不生效-->
<!--    <aop:config >-->
<!--        <aop:pointcut id="txManager" expression="execution(* com.wang.mapper.MapperTest.*(..))"/>-->
<!--        <aop:advisor advice-ref="txAdvice" pointcut-ref="txManager"></aop:advisor>-->
<!--    </aop:config>-->

<!--    在此处配置mapper包下面所有类之后,类会被代理,导致下面再注入该类对象报错-->
    <aop:config >
        <aop:pointcut id="txManager" expression="execution(* com.wang.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txManager"></aop:advisor>
    </aop:config>

    <bean id="blogService" class="com.wang.service.impl.BlogManagerImpl">
        <property name="bmi" ref="blogMapperImpl"></property>
    </bean>

</beans>

 

 可以看到,当进一步限定成该包下面的其他类时,不会在报上面的错。

<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--    datasource和sqlsessionfactory配置一次基本不会改动,所以配置到另外一个配置文件,用到的时候引入-->
    <import resource="applicationContext.xml"></import>


    <!--     3 此处需要增加Mapper的实现类,该类通过继承SessionTemplateDaoSupport类得到sqlsession-->
    <!--     4 注册该实现类,注入sqlSessionFactory或SqlSessionTemplate-->
    <bean id="blogMapperImpl" class="com.wang.mapper.BlogMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>

    </bean>

    <tx:advice id="txAdvice"  transaction-manager="transactionManager" >
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>

    </tx:advice>

<!--    MapperTest只是用例测试的类,没有注册对象,配置在这里,事务并不生效-->
<!--    <aop:config >-->
<!--        <aop:pointcut id="txManager" expression="execution(* com.wang.mapper.MapperTest.*(..))"/>-->
<!--        <aop:advisor advice-ref="txAdvice" pointcut-ref="txManager"></aop:advisor>-->
<!--    </aop:config>-->

<!--    在此处配置mapper包下面所有类之后,类会被代理,改成该包下面的其他类之后,该报错不再出现-->
    <aop:config >
        <aop:pointcut id="txManager" expression="execution(* com.wang.mapper.UserMapperImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txManager"></aop:advisor>
    </aop:config>

    <bean id="blogService" class="com.wang.service.impl.BlogManagerImpl">
        <property name="bmi" ref="blogMapperImpl" >

        </property>
    </bean>

</beans>

 

当切面还是定义mapper包时,要能在其他类中引用该bean,那么必须把引用类型换成其上层类型,接口或者父类。

例如修改成如下:此时不在报bean实例化失败的错误

package com.wang.service.impl;

import com.wang.mapper.BlogMapper;
import com.wang.mapper.BlogMapperImpl;
import com.wang.pojo.Blog;
import com.wang.service.BlogManager;

public class BlogManagerImpl implements BlogManager {
//    引用BlogMapper时接口,而不是真实的类,这样注册该bean,才能成功注入
    private BlogMapper bmi;

    public void setBmi(BlogMapper bmi) {
        this.bmi = bmi;
    }

    @Override
    public void manageBlog(Blog blog) {
       addBlog(blog);
        blog.setViews(blog.getViews()+1);
       int i =  1/0;
        updateBlog(blog);


    }

    @Override
    public void addBlog(Blog blog) {
        bmi.addBlog(blog);
    }

    @Override
    public void updateBlog(Blog blog) {
        bmi.updateBlog(blog);
    }
}

 

问题5 spring mvc调试时tomcat 输出中文乱码

乱码:

Using CATALINA_HOME:   "E:\Toos\apache-tomcat-9.0.33-windows-x64\apache-tomcat-9.0.33"
Using CATALINA_TMPDIR: "E:\Toos\apache-tomcat-9.0.33-windows-x64\apache-tomcat-9.0.33\temp"
Using JRE_HOME:        "D:\Program Files\Java\jdk1.8.0_131"
Using CLASSPATH:       "E:\Toos\apache-tomcat-9.0.33-windows-x64\apache-tomcat-9.0.33\bin\bootstrap.jar;E:\Toos\apache-tomcat-9.0.33-windows-x64\apache-tomcat-9.0.33\bin\tomcat-juli.jar"
04-Apr-2020 11:22:20.100 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Server.鏈嶅姟鍣ㄧ増鏈�:     Apache Tomcat/9.0.33
04-Apr-2020 11:22:20.104 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log 鏈嶅姟鍣ㄦ瀯寤�:            Mar 11 2020 09:31:38 UTC
04-Apr-2020 11:22:20.104 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log 鏈嶅姟鍣ㄧ増鏈彿(锛�9.0.33.0
04-Apr-2020 11:22:20.104 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10
04-Apr-2020 11:22:20.104 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log OS.鐗堟湰:               10.0
04-Apr-2020 11:22:20.104 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log 鏋舵瀯:                  amd64
04-Apr-2020 11:22:20.104 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Java 鐜鍙橀噺:         D:\Program Files\Java\jdk1.8.0_131\jre
04-Apr-2020 11:22:20.104 淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM 鐗堟湰:              1.8.0_131-b11
 

解决办法:

1.apache-tomcat-9.0.33\conf\logging.properties

2.修改参数:java.util.logging.ConsoleHandler.encoding = GBK

3.重启tomcat server 之后解决。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值