javaweb ssm整合异常

时间:2022-04-23 22:20:34
以下是错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authorityaction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.cj.ssm.service.impl.AuthorityServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(type=class java.lang.Object, shareable=true, mappedName=, lookup=, authenticationType=CONTAINER, description=, name=)}
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1113)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1671)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.cj.ssm.service.impl.AuthorityServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(type=class java.lang.Object, shareable=true, mappedName=, lookup=, authenticationType=CONTAINER, description=, name=)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
... 33 more

以下是代码:采用自动扫描注解的方式
package cn.cj.ssm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.cj.ssm.po.Authority;
import cn.cj.ssm.po.Prompt;
import cn.cj.ssm.service.impl.AuthorityServiceImpl;

@Controller
public class Authorityaction {
@Resource
public AuthorityServiceImpl authorityServiceImpl;
@Resource
public Prompt prompt;

@RequestMapping(value = "/test.do", method = RequestMethod.POST)
public ModelAndView test(Authority authority) throws Exception {
prompt.setPrompt(authorityServiceImpl.selectUser(authority));
Map<String,Object> map=new HashMap<String, Object>();
map.put("p", prompt);
return new ModelAndView("dealers",map);
}

@RequestMapping("/login.do")
public String login() {
return "login";
}

}

package cn.cj.ssm.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.cj.ssm.mapper.AuthorityMapper;
import cn.cj.ssm.po.Authority;
import cn.cj.ssm.service.AuthorityService;
@Service
public class AuthorityServiceImpl implements AuthorityService {
@Resource
public AuthorityMapper authorityMapper;

public String selectUser(Authority authority){
Authority a = authorityMapper.selectUser(authority);

if (a.getPassword().equals(authority.getPassword())) {
System.out.println("登录成功");
return "登录成功";
}
return "账号或密码错误";
}

}

package cn.cj.ssm.po;

import org.springframework.stereotype.Repository;

@Repository
public class Authority {
private Integer id;

private String username;

private String password;

private Integer level;



public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}

public Integer getLevel() {
return level;
}

public void setLevel(Integer level) {
this.level = level;
}
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.cj.ssm.mapper.AuthorityMapper" >
  <resultMap id="BaseResultMap" type="cn.cj.ssm.po.Authority" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="level" property="level" jdbcType="INTEGER" />
  </resultMap>
  <select id="selectUser" parameterType="cn.cj.ssm.po.Authority" resultType="cn.cj.ssm.po.Authority">
   select username,password from authority where username=#{authority.username}
  </select>
 </mapper>

4 个解决方案

#1


本帖最后由 wlwlwlwl015 于 2016-09-27 10:27:21 编辑
cn.cj.ssm.service.impl.AuthorityServiceImpl

这个service没注入好~ 检查一下扫描配置之类的

#2


 Spring的配置贴出来看看~怎么我感觉是没有在配置中打开注解

#3


看下你的配置文件信息。

#4


找到错误了,谢谢大家。是bean名称错误。。。。

#1


本帖最后由 wlwlwlwl015 于 2016-09-27 10:27:21 编辑
cn.cj.ssm.service.impl.AuthorityServiceImpl

这个service没注入好~ 检查一下扫描配置之类的

#2


 Spring的配置贴出来看看~怎么我感觉是没有在配置中打开注解

#3


看下你的配置文件信息。

#4


找到错误了,谢谢大家。是bean名称错误。。。。