Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用@ServerEndpoint修饰但不用@Component时,其它Bean注入该Bean时会报 bean could not be found 异常 #252

Open
niezhiliang opened this issue Jun 27, 2023 · 2 comments

Comments

@niezhiliang
Copy link

image
image
image
我看了源码,ServerEndpointExporter 实现自SmartInitializingSingleton这个后置处理器,该处理器是在所有的Bean都初始化完成后,再执行的,如果不用@component修饰,只要其它Bean注入都会报这个异常。

如果搭配@component一起使用,就不会有该问题,我看大佬本身想剔除对@component的依赖,只需要单独使用@serverendpoint注解。所以提了该issue。

@niezhiliang
Copy link
Author

niezhiliang commented Jun 27, 2023

我尝试的改了一下这个问题,ServerEndpointExporter 再实现一个后置处理器接口 BeanDefinitionRegistryPostProcessor,registerEndpoints中的scanPackage方法放到postProcessBeanFactory方法中执行,让扫描提前执行,就不会再报异常
`public class ServerEndpointExporter extends ApplicationObjectSupport implements SmartInitializingSingleton, BeanFactoryAware, ResourceLoaderAware,BeanDefinitionRegistryPostProcessor {

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {

}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
    ApplicationContext context = getApplicationContext();
    scanPackage(context);
}

protected void registerEndpoints() {
    ApplicationContext context = getApplicationContext();
    // scanPackage(context);

    String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class);
    Set<Class<?>> endpointClasses = new LinkedHashSet<>();
    for (String beanName : endpointBeanNames) {
        endpointClasses.add(context.getType(beanName));
    }

    for (Class<?> endpointClass : endpointClasses) {
        if (ClassUtils.isCglibProxyClass(endpointClass)) {
            registerEndpoint(endpointClass.getSuperclass());
        } else {
            registerEndpoint(endpointClass);
        }
    }

    init();
}

}`

@liliexuan
Copy link

我怎么感觉你的用法跟我的相反了,不是应该是MyWebSocket注入其他bean吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants