Idea springBoot配置器中 @Configuration 注解中使用 Autowired 注入报错

引子:

        在自己使用写小demo的过程中,使用到拦截器来实现JWT鉴权,编写了拦截器JwtFilter以及配置拦截器类ApplicationConfig.其中类JwtFilter类使用了注解@Component , 配置拦截器类ApplicationConfig使用了注解@Configuration。在将JwtFilter注入到类ApplicationConfig中的时候报错,找不到JwtFilter的bean.

正文:

因为在SB项目的启动的最初阶段,IDE加载注入配置器bean,还没有扫描到工程的bean,无法发现对应的 bean ,于是就需要我们手动的给其指定需要扫描的包

使用 @ComponentScan注解并指定所需model类的包地址就可以解决整个问题了

@Configuration
@ComponentScan({"com.wch.user.filter"})


这里介绍另一种注入bean方法,但是不能解决上面的这个问题,原因暂时未理清
1,使用springframework相关jar

用自定义工具类注入

SpringContextUtils.getBean(ItsmSystemResourceService.class);
 

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
 * Spring Context工具类.
 *
 * @author chengyuebin
 */
@Component
@Scope("singleton")
public class SpringContextUtils implements ApplicationContextAware {

    /**
     * Spring应用上下文环境.
     */
    private static ApplicationContext applicationContext;

    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     *
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    /**
     * 获取ApplicationContext.
     *
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 获取对象.
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws BeansException
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) applicationContext.getBean(name);
    }

    /**
     * 获取类型为requiredType的对象.
     *
     * @param clz
     * @return
     * @throws BeansException
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        return (T)applicationContext.getBean(clz);
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
     * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     * @param name
     * @return boolean
     * @throws NoSuchBeanDefinitionException
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws NoSuchBeanDefinitionException
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws NoSuchBeanDefinitionException
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getAliases(name);
    }

}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可能出现这种情况的原因有很多,以下是一些可能的解决方案: 1. 确保在配置文件正确配置了需要自动装配的对象,例如: ``` @Configuration @PropertySource("classpath:application.properties") public class AppConfig { @Autowired private Environment env; @Bean public MyConfig myConfig() { MyConfig config = new MyConfig(); config.setUrl(env.getProperty("my.url")); config.setUsername(env.getProperty("my.username")); config.setPassword(env.getProperty("my.password")); return config; } } ``` 2. 确保配置文件的路径和文件名是正确的,例如: ``` @PropertySource("classpath:application.properties") ``` 这里的classpath表示classpath下的根目录,如果配置文件在子目录下,需要在路径添加相应的目录。 3. 确保项目的classpath正确配置,例如: 在IDEA,可以在项目的“Run/Debug Configurations”查看项目的classpath,确保配置文件在classpath下。 4. 确保需要自动装配的对象类上使用了@Component、@Configuration、@Service等注解,例如: ``` @Component public class MyConfig { private String url; private String username; private String password; // getter and setter methods } ``` 这里使用了@Component注解,表示这是一个Spring组件,可以被自动扫描并装配。 5. 确保使用了正确的@Autowired注解,例如: ``` @Autowired private MyConfig myConfig; ``` 这里使用了@Autowired注解,表示需要自动装配MyConfig对象。 6. 如果使用了多个配置文件,需要确保它们没有冲突,例如: ``` @PropertySources({ @PropertySource("classpath:application.properties"), @PropertySource("classpath:other.properties") }) public class AppConfig { // ... } ``` 这里使用了@PropertySources注解,可以配置多个配置文件。需要确保它们没有相同的属性名,否则会出现冲突。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值