乌龟开摩托
2022-03-07 15:22
采纳率: 100%
浏览 60

SSM框架 Controller无法自动注入Service,怎么解决?

问题遇到的现象和发生背景

applicationContext.xml文件中已经声明了使用注解配置
Controller中也使用了注解自动注入Service
Service也使用了@Service注解
但是自动注入不成功

问题相关代码,请勿粘贴截图

applicationContext.xml

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

    <!-- 加载配置文件-->
    <context:property-placeholder location="classpath:druid.properties"/>
    <!-- 配置Druid数据源-->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${druid.driver}"/>
        <property name="url" value="${druid.url}"/>
        <property name="username" value="${druid.username}"/>
        <property name="password" value="${druid.password}"/>
        <!-- 最大连接数-->
        <property name="maxActive" value="${druid.pool.maxActive}"/>
        <!-- 最大空闲连接数-->
        <property name="minIdle" value="${druid.pool.minIdle}"/>
        <!-- 初始化连接数-->
        <property name="initialSize" value="${druid.pool.initialSize}"/>
    </bean>

    <!-- 注入数据源-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="druidDataSource"/>
        <!-- 载入mapper文件 -->
        <property name="mapperLocations" value="classpath:Mappers/*.xml"/>
        <!-- 为了不给实体类取别名-->
        <property name="typeAliasesPackage" value="com.hrms.pojo"/>
        <!-- 加载mybatis配置文件-->
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>

    <!-- 配置dao-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.hrms.dao"/>
    </bean>

    <!-- 声明使用注解配置-->
    <context:annotation-config/>
    <!-- 配置@Service注解 即ServiceImpl-->
    <context:component-scan base-package="com.hrms.service.impl">
    </context:component-scan>

    <!-- 使用注解配置AOP-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

</beans>


Controller

package com.hrms.controller;

import com.hrms.pojo.Login;
import com.hrms.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    //登录方法
    @RequestMapping(value="/toLogin", method = RequestMethod.POST)
    public ModelAndView login(@RequestParam("L_ID") String L_ID, @RequestParam("L_Password")String L_Password,
                              @RequestParam("loginType") String loginType) {

        System.out.println("进入登录方法");
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("ID=" + L_ID + "\n" + "Password=" + L_Password);
        System.out.println("loginType=" + loginType);
        Login login = loginService.toLogin(L_ID, L_Password);

        return null;
    }

Service

package com.hrms.service;

import com.hrms.pojo.Login;

public interface LoginService {

    public Login toLogin(String L_ID,String L_Password);
}

ServiceImpl

package com.hrms.service.impl;

import com.hrms.dao.LoginDao;
import com.hrms.pojo.Login;
import com.hrms.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    public Login toLogin(String L_ID, String L_Password) {
        System.out.println("LoginService中toLogin方法");
        return null;
    }
}

运行结果及报错内容

三月 07, 2022 3:06:08 下午 org.apache.catalina.startup.TldConfig execute
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
三月 07, 2022 3:06:08 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
信息: Initializing Servlet 'SpringMVC'
三月 07, 2022 3:06:08 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'loginService' available
三月 07, 2022 3:06:08 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'loginService' available
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1230)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1174)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1066)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5409)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5707)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1017)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:993)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1900)
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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题