Java Spring继承了带有构造函数参数的@Component
我有一个需要在运行时创建代理的服务。代理从基代理类继承。我想使用spring的自动连线功能,而不是自己进行依赖注入 但我遇到了这个问题,尽管我将组件标记为scope=prototype,甚至@Lazy以防止编译时发生任何事情Java Spring继承了带有构造函数参数的@Component,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个需要在运行时创建代理的服务。代理从基代理类继承。我想使用spring的自动连线功能,而不是自己进行依赖注入 但我遇到了这个问题,尽管我将组件标记为scope=prototype,甚至@Lazy以防止编译时发生任何事情 *************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.my.p
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.my.project.AgentType1 required a bean of type 'com.my.project.POJO' that could not be found.
这是尝试创建代理的服务:
@Service
public class ProjectMain {
@Autowired
ApplicationContext context;
List<IAgent> agents = new ArrayList<>();
void SetupAgents(List<POJO> agentPojos) {
for(POJO agentPojo: agentPojos) {
IAgent agent = AgentFactory.CreateAgent(agentPojo, context);
agents.add(agent);
}
}
}
这是一个我发现继承场景需要的自定义注释
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Component
@Inherited
@Lazy
@Scope("prototype")
public @interface AgentAnnotation {}
这些是基本代理类和子代理类,它们需要名为POJO的自定义数据结构才能工作
@AgentAnnotation
public class BaseAgent implements IAgent {
@Autowired
Environment env;
public BaseAgent(POJO agentPojo, String someotherdata) {
}
}
public class AgentType1 extends BaseAgent {
public AgentType1(POJO agentPojo) {
super(agentPojo, "mydata1");
...
}
}
public class AgentType2 extends BaseAgent {
public AgentType2(POJO agentPojo) {
super(agentPojo, "mydata2");
...
}
}
这是入门应用程序
@ComponentScan(basePackages = "com.my.project", includeFilters = @ComponentScan.Filter(AgentAnnotation.class))
@EnableScheduling
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
我还尝试了配置方法:
@Configuration
public class BaseAgentConfig {
@Bean
@Scope("prototype")
public AgentType1 agentType1(POJO agentPojo) {
return new AgentType1(agentPojo);
}
@Bean
@Scope("prototype")
public AgentType2 agentType2(POJO agentPojo) {
return new AgentType2(agentPojo);
}
}
在本例中,我从baseAgent类中删除了@AgentNotation,因为我们现在正在通过此配置进行实例化。还从主应用程序中删除了ComponentScan行
这一次,@Autowired不起作用。baseAgent类中的所有自动连线引用均为null
请建议解决此错误的最佳方法。谢谢。找到了问题和解决方案 基本上,我希望子类继承@Component和@Scope,但事实并非如此 所以本质上,我需要用@Component和@Scope(“prototype”)注释每个子类 另一个问题是,我希望构造函数中有自动连接项,这太早了。添加@PostConstruct解决了这个问题
因此,我最终删除了自定义注释和配置类,并进行了刚才描述的更改。我也遇到了类似的错误。我已经实现了自定义启动器,并在我的控制器类中自动连接它。如果我删除
@ComponentScan
,它会给我“fooCaller required as bean,类型为“”,找不到。”@Amit ComponentScan可以保留,与我面临的上述问题无关。我不确定你的问题是什么,但如果你进一步澄清,我也许可以参与进来。这是我的问题。我们的custom starter用户不想关心他们必须扫描的包,因此我们尝试了custom starter。我没有使用custom starter,但根据我的经验,Component扫描问题与包的组织方式有关。因此,如果主应用程序上有@ComponentScan,而其他所有内容都在内部包层次结构中,那么它就可以工作。
@Configuration
public class BaseAgentConfig {
@Bean
@Scope("prototype")
public AgentType1 agentType1(POJO agentPojo) {
return new AgentType1(agentPojo);
}
@Bean
@Scope("prototype")
public AgentType2 agentType2(POJO agentPojo) {
return new AgentType2(agentPojo);
}
}