总结SSH阶段常见异常系列之五spring

来源:互联网 发布:查杀木马软件 编辑:程序博客网 时间:2024/04/30 10:14

总结SSH阶段常见异常系列之五spring(10条)

异常一

异常描述: 没有可写的属性异常: bean对象com.itheima.demo2.UserServiceImpl的属性’userDao’ 不是一个有效的setter 方法。
异常信息: org.springframework.beans.NotWritablePropertyException: Invalid property ‘userDao’ of bean class [com.itheima.demo2.UserServiceImpl]: Bean property ‘userDao’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
异常原因: com.itheima.demo2.UserServiceImpl注入属性’userDao’ 需要调用set方法,而 UserServiceImpl中没有userDao的set方法
解决方案:
检查你userServiceImpl中注入的userDao是否有set方法,并且检查你的spring配置文件中bean的property属性是否和你的dao名字一样

异常二

异常描述: 约束引入错误异常:使用p名称空间获取值时,获取不到。

异常信息: 控制台无报错
异常原因: 配置文件中约束引入错误
解决方案: 配置文件中约束正确引入p名称空间的约束为xmlns:p=”http://www.springframework.org/schema/p”

异常三

异常描述: 没有匹配的bean异常:在配置文件中找不到id为userService的bean
异常信息: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘userService’ is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
异常原因: 在配置文件中没有定义或名称写错

解决方案: 如果在配置文件中没有定义userService的bean,重新定义一个,如果已经定义了检查是否是单词写错了。

异常六

异常描述: 无效的数据访问接口使用异常:在只读模式下不允许写操作。
异常信息: ERROR DefaultDispatcherErrorHandler:42 - Exception occurred during processing request: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove ‘readOnly’ marker from transaction definition.
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove ‘readOnly’ marker from transaction definition.
异常原因: 开启了事务注解,在业务层的实现类里面没有加事务注解。
解决方案: 在业务层的实现类中加上事务注解@Transactional

异常七

异常描述: 空指针异常:加载spring的监听器异常。
异常信息: Exception starting filter struts2
Class: com.opensymphony.xwork2.spring.SpringObjectFactory
File: SpringObjectFactory.java
Method: getClassInstance
Line: 245 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:245:-1
……
Caused by: java.lang.NullPointerException
异常原因: 配置了struts2的黑心过滤器,引入了struts-spring-plugin.jar,却没有 配置spring的核心监听器。
解决方案: 在web.xml中配置spring的核心监听器并指定位置,如下:

org.springframework.web.context.ContextLoaderListener


contextConfigLocation
classpath:applicationContext.xml

异常八

异常描述: 没有可写的属性异常:在cn.guoqiang.injection.Demo类中存在非法的name属性,name属性没有set方法
异常信息: org.springframework.beans.NotWritablePropertyException: Invalid property ‘name’ of bean class [cn.guoqiang.injection.Demo]: Bean property ‘name’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)

异常原因: 在cn.guoqiang.injection.Demo类中只定义了name成员变量,但是没有提供get/set方法
解决方案: 在cn.guoqiang.injection.Demo类提供name属性的get/set方法即可:
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

异常九

异常描述: 映射没有找到异常:没有找到cn/itcast/domain/Role.hbm.xml映射文件,在HibernateUtils.java的14行
异常信息: org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : cn/itcast/domain/Role.hbm.xml : origin(cn/itcast/domain/Role.hbm.xml)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
cn.itcast.utils.HibernateUtils.(HibernateUtils.java:14)
异常原因: 在HibernateUtils中初始化Hibernate,加载了Hibernate.cfg.xml配置文件,在Hibernate.cfg.xml文件中定义了cn/itcast/domain/Role.hbm.xml,但是此路径下没有该映射文件
解决方案: 将Role.hbm.xml移动到cn.itcast.domain路径下即可

异常十

异常描述: IllegalArgumentException:非法数据异常,警告不能匹配service这个类型名称,cn.itcast.ssh.service这个类型名称不匹配。
异常信息: Caused by: java.lang.IllegalArgumentException: warning no match for this type name: cn.itcast.ssh.service [Xlint:invalidAbsoluteTypeName]
异常原因: 在applicationContext.xml配置文件里配置切点时,表达式错误:expression=”execution(* cn.itcast.ssh.service.UserServiceImpl(..))”少个类里的方法。
解决方案: 修改applicationContext.xml配置文件里的切点的表达式修改为expression=”execution(* cn.itcast.ssh.service..(..))或者expression=”execution(* cn.itcast.ssh.service.UserServiceImpl.save(..))

0 0
原创粉丝点击