解决Springboot中封装jar包里ComponentScan失效问题

  |   0 评论   |   0 浏览

最近在整改Junx-ep项目的时候,发现部分组件的@ComponentScan注解失效,导致controller等资源无法注入,整改之前的@ComponentScan是写在org.springframework.boot.autoconfigure.AutoConfiguration.imports文件定义的自动装配class中的,这个class的内容如下:

package io.github.junxworks.ep.codegen.infrastructure.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;

import io.github.junxworks.ep.core.utils.SpringContextUtils;
import io.github.junxworks.ep.pvc.PersistenceVersionController;
import io.github.junxworks.ep.sys.infrastructure.config.BaseModuleConfiguration;

@Configuration
@EnableConfigurationProperties({ EPCodegenConfig.class })
@ConditionalOnBean(BaseModuleConfiguration.class)
@ConditionalOnProperty(prefix = "junx.ep.codegen", name = "enabled", havingValue = "true", matchIfMissing = true)
@ComponentScan(value = "io.github.junxworks.ep.codegen")  #注解在这里
@Import({ SpringContextUtils.class })

public class CodegenConfiguration {

	/** 常量 PVC_PATH.持久化版本控制路径 */
	private static final String PVC_PATH = "/io/github/junxworks/ep/codegen/infrastructure/pvc";

	/** 常量 MODULE_NAME.pvc参数,模块名 */
	private static final String MODULE_NAME = "junx_ep_codegen";

	@DependsOn("JunxEpSpringContextUtils")
	@Bean(name = "junxEpCodegenPvc", initMethod = "start", destroyMethod = "stop")
	public PersistenceVersionController junxEpCodegenPvc() {
		PersistenceVersionController pvc = new PersistenceVersionController();
		pvc.setPvcPath(PVC_PATH);
		pvc.setModuleName(MODULE_NAME);
		return pvc;
	}
}

代码重构之前,这个注解是好使的,不知为何整改后ComponentScan注解失效,无法扫描到io.github.junxworks.ep.codegen包下任何Bean,最后可能是注解冲突,导致@ComponentScan注解失效,因此尝试把@ComponentScan注解放到其他类中,CodegenConfiguration相同目录下新增Scan类,在Scan类头上添加@ComponentScan注解,再通过Import方式把Scan类注入到Spring中,@ComponentScan注解失效问题得到解决。

package io.github.junxworks.ep.codegen.infrastructure.config;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan("io.github.junxworks.ep.codegen")
public class Scan {

}

调整后的CodegenConfiguration代码:

package io.github.junxworks.ep.codegen.infrastructure.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;

import io.github.junxworks.ep.core.utils.SpringContextUtils;
import io.github.junxworks.ep.pvc.PersistenceVersionController;
import io.github.junxworks.ep.sys.infrastructure.config.BaseModuleConfiguration;

@Configuration
@EnableConfigurationProperties({ EPCodegenConfig.class })
@ConditionalOnBean(BaseModuleConfiguration.class)
@ConditionalOnProperty(prefix = "junx.ep.codegen", name = "enabled", havingValue = "true", matchIfMissing = true)
@Import({ SpringContextUtils.class, Scan.class })
public class CodegenConfiguration {

	/** 常量 PVC_PATH.持久化版本控制路径 */
	private static final String PVC_PATH = "/io/github/junxworks/ep/codegen/infrastructure/pvc";

	/** 常量 MODULE_NAME.pvc参数,模块名 */
	private static final String MODULE_NAME = "junx_ep_codegen";

	@DependsOn("JunxEpSpringContextUtils")
	@Bean(name = "junxEpCodegenPvc", initMethod = "start", destroyMethod = "stop")
	public PersistenceVersionController junxEpCodegenPvc() {
		PersistenceVersionController pvc = new PersistenceVersionController();
		pvc.setPvcPath(PVC_PATH);
		pvc.setModuleName(MODULE_NAME);
		return pvc;
	}
}


标题:解决Springboot中封装jar包里ComponentScan失效问题
作者:michael
地址:https://blog.junxworks.cn/articles/2024/01/10/1704885777754.html