NoSuchBeanDefinitionException:没有可用的名为 'authService' 的 bean

NoSuchBeanDefinitionException: No bean named 'authService' available

尝试使用 api 时,出现此错误:

"org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'authService' available". 

如何让应用程序找到授权服务?我之前遇到过类似的错误,我试图通过在 Main class.

中添加您在下面看到的所有包来修复该错误

主要class:

package com.lvwangbeta.poplar.api;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.lvwangbeta.poplar.action.PoplarActionServiceApplication;
import com.lvwangbeta.poplar.feed.PoplarFeedServiceApplication;
import com.lvwangbeta.poplar.tag.PoplarTagServiceApplication;
import com.lvwangbeta.poplar.user.PoplarUserServiceApplication;

import java.util.List;

@SpringBootApplication(scanBasePackages= {"com.lvwangbeta.poplar.common","com.lvwangbeta.poplar.common.intr","com.lvwangbeta.poplar.action",
        "com.lvwangbeta.poplar.api.service",
        "com.lvwangbeta.poplar.feed","com.lvwangbeta.poplar.tag",
        "com.lvwangbeta.poplar.user", "com.lvwangbeta.poplar.api"})
//@ComponentScan({"com.lvwangbeta.poplar.common","com.lvwangbeta.poplar.user","com.lvwangbeta.poplar.feed.dao", "com.lvwangbeta.poplar.api"})
@ComponentScan({"com.lvwangbeta.poplar.action.dao.impl","com.lvwangbeta.poplar.feed.dao.impl"})
@MapperScan({"com.lvwangbeta.poplar.action.dao","com.lvwangbeta.poplar.feed.dao","com.lvwangbeta.poplar.tag.dao"})
public class PoplarApiApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        //Object[] sources = {PoplarApiApplication.class,PoplarTagServiceApplication.class};
        //SpringApplication.run(sources, args);
        System.out.println("Started");
        SpringApplication.run(PoplarApiApplication.class, args);
        //SpringApplication.run(PoplarTagServiceApplication.class, args);//I hope this works
        //SpringApplication.run(PoplarActionServiceApplication.class, args);
        //SpringApplication.run(PoplarFeedServiceApplication.class, args);
        //SpringApplication.run(PoplarUserServiceApplication.class, args);

    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(new RequestAttributeArgumentResolver());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(new APIAccessAuthRequiredInterceptor())
                .addPathPatterns("/api/v1/**")
                .excludePathPatterns("/api/v1/user/login/**")
                .excludePathPatterns("/api/v1/user/check/email/*")
                .excludePathPatterns("/api/v1/user/register/**")
                .excludePathPatterns("/api/v1/feed/of/user/**");
    }
}

授权服务class:

package com.lvwangbeta.poplar.api.service;

import com.lvwangbeta.poplar.common.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("authService")
public class AuthService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Resource(name="redisTemplate")
    private HashOperations<String, String, Object> mapOps;

    public User getUserByToken(String token) {
        return (User) mapOps.get("tokens:", token);
    }
}

我错过了什么?即使 api link 在那里,它也没有注册。我正在尝试将许多微服务合并为一个,所以有很多包。

@ComponentScan

中包含 com.lvwangbeta.poplar.api.service

像这样:

@ComponentScan({"com.lvwangbeta.poplar.action.dao.impl","com.lvwangbeta.poplar.feed.dao.impl", "com.lvwangbeta.poplar.api.service"})

建议:对于简单的应用,避免上述方式配置。

保持您的项目结构如本参考中所述,@SpringBootApplication 就足够了。

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-locating-the-main-class