簡體   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