繁体   English   中英

Java Spring bean 创建和从外部访问 jar

[英]Java Spring bean creation and access from external jar

我有 spring jar 用于创建为 export--java--JARfile 的实体 bean,其中我的 bean 定义如下:

package com.my.beans;
@Component("expBean")
public class ExpBean {
}

我在这个 jar 中也有配置

package com.my;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class pBeanConfig {

    @Bean
    public ExpBean getExpBean() {
        return new ExpBean();
    }
}

I have client spring application where i am adding above jar having only beans as by external dependency and trying to get my beans when spring app starts using following code in main code in client spring app.

package com.my;

import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication()
@ComponentScan("com.my")
public class Application {

    public static void main (String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {         
            System.out.println(beanName);
        }
          WMyBean bean = (WMyBean) ctx.getBean("expBean");
          bean.doSomething();
}
}

但是当检查打印的 bean 定义列表时,我没有从外部 jar 看到我的 bean,而且我也收到以下错误。

"Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'expBean' available"

我尝试了很多选项,但不确定缺少什么。 选项1

@ComponentScan("com.my")
@SpringBootApplication
public class Application {

选项 2

@ComponentScan({"com.my"})
@SpringBootApplication
public class Application {

在我错过的步骤上需要有价值的输入。

谢谢

首先,我假设构建工具是 Gradle,但您可以将 jar 作为本地文件导入,以便进行实验:

dependencies {
implementation files('libs/something_local.jar')}

然后,假设您使用 Eclipse 您可以右键单击项目文件夹并执行“Gradle refresh”

In your config class you can either autowire in the bean for the class as a whole, or choose to create it whenever an instance of the class is created by declaring it within the class constructor:

    import org.springframework.beans.factory.annotation.Autowired;

//I also have config in this jar

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class pBeanConfig {

    //Autowire in the bean for the whole class.
    @Autowired
    ExpBean expBean;
    
    public pBeanConfig() {
    
    //Or, you may want to have a bean instance whenever the pBeanConfig class is instantiated.
    ExBean exBean;
   }
}

我对这个答案做了一些假设,比如它是一个 Gradle 项目,但是您可以通过修改 POM 文件以包含该文件来为 Maven 设置类似的依赖项。

除了构建工具,在 Eclipse 中,您可以通过右键单击项目和构建路径 => 配置构建路径 => 添加外部 jar 文件来简单地添加 jar 文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM