gpt4 book ai didi

java - Spring Component 扫描 jar 中的类?

转载 作者:行者123 更新时间:2023-12-04 05:31:22 25 4
gpt4 key购买 nike

我们可以将带有 Spring Bean 注解的类生成为 jar 文件吗?
如何从那个 jar 的类中注入(inject)另一个 Spring Bean?
你能提供可能的方法吗?当我得到如下它不起作用?

在 Jar 文件中

    package org.java.support;

@Service("CommonService")
public class CommonService {
}

在项目中
    package com.java.test.app;

@Service(value = "OtherService")
public class OtherService {
@Resource(name = "CommonService")
private CommonService service;
}

在配置中
    <context:component-scan base-package="com.java.test.app, org.java.support">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

我收到以下错误;
13:32:12,331 DEBUG [org.springframework.context.annotation.AnnotationConfigApplicationContext] Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [or
g.springframework.context.support.DefaultLifecycleProcessor@32dcb03b]
13:32:12,331 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] Returning cached instance of singleton bean 'lifecycleProcessor'
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'CommonService' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at org.ace.java.support.delete.Test.main(Test.java:18)

最佳答案

如果您从 Eclipse 生成 Jar,请在导出对话框中确保选中“添加目录条目”复选框(在选项下)。

关于java - Spring Component 扫描 jar 中的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com