子罐中的SpringBoot,Maven和bean

时间:2018-09-11 15:14:30

标签: spring maven spring-boot

我有几个行家文物:

- server (springboot application)
 - api
  - api-impl-01
  - api-impl-02

有两种api实现:api-impl-01和api-impl-02。在每个人造物中,都有四季豆。 api-impl-01 / api-impl-02中的Bean是使用Condition / @ Conditional弹簧支持加载的。最终的jar结构是这样的:

springboot-app.jar
 - BOOT-INF
  - classes
   - server artefact classes
  - lib
   - api.jar
   - api-impl-01.jar
   - api-impl-02.jar

服务器jar在BOOT-INF / lib目录中包含api.jar,api-impl-01.jar和api-impl-02.jar,服务器中的类在BOOT-INF / classes目录中。

这里的问题是未加载来自内部jars(api * .jar)的bean。应用程序无法启动,并显示以下错误:pat.RestEndpoint中的dbBean字段需要找不到类型为“ pat.ApiBean”的bean。

请,有人可以提出解决方案吗?我不想将所有源代码都放到一个大型Maven工件中。

谢谢

'api'伪像:

package pat.multidemo.dal.spi;
public interface DalSpi {
    public void print(String msg);
}

package pat.multidemo.dal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pat.multidemo.dal.spi.DalSpi;
@Service
public class Dal {
    @Autowired
    private DalSpi dalSpi;
    public void test(String msg) {
        dalSpi.print(msg);
    }
}

“ impl01”伪像:

package pat.multidemo.dal.spi.impl01;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class Condition01 implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return "impl01".equals(System.getProperty("spi.impl"));
    }
}


package pat.multidemo.dal.spi.impl01;
import pat.multidemo.dal.spi.DalSpi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config01 {
    @Bean(name = "dal.spi.provider")
    @Conditional(value = Condition01.class)
    public DalSpi dalSpiProvider() {
        return new DalSpi01();
    }
}

package pat.multidemo.dal.spi.impl01;
import pat.multidemo.dal.spi.DalSpi;
public final class DalSpi01 implements DalSpi {
    @Override
    public void print(String msg) {
        System.err.println(this.getClass().getName() + " -> " + msg);
    }
}

“ impl02”伪像(与“ impl01”基本相同):

package pat.multidemo.dal.spi.impl02;
...
public class Condition02 implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return "impl02".equals(System.getProperty("spi.impl"));
    }
}


package pat.multidemo.dal.spi.impl02;
...
@Configuration
public class Config02 {
    @Bean(name = "dal.spi.provider")
    @Conditional(value = Condition02.class)
    public DalSpi dalSpiProvider() {
        return new DalSpi02();
    }
}

package pat.multidemo.dal.spi.impl02;
import pat.multidemo.dal.spi.DalSpi;
public final class DalSpi02 implements DalSpi {
    @Override
    public void print(String msg) {
        System.err.println(this.getClass().getName() + " -> " + msg);
    }
}

“服务器”伪像(应用程序):

package pat.multidemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MultiDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MultiDemoApplication.class, args);
    }
}


package pat.multidemo;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
@Component
public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        registerEndpoints();
    }
    private void registerEndpoints() {
        register(Endpoint.class);
    }
}

package pat.multidemo;
import pat.multidemo.dal.Dal;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Path("/test")
public final class Endpoint {
    @Autowired
    private Dal dal;
    @GET
    @Path("/ping")
    @Produces("text/plain")
    public String ping(@Context HttpServletRequest servletRequest) {
        dal.test("rest endpoint");
        return "done";
    }
}

1 个答案:

答案 0 :(得分:0)

谢谢达菲。我必须这样做:

将此类添加到“ api”

package pat.multidemo.dal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DalConfig {
    @Bean(name = "dal.provider")
    public Dal dalProvider() {
        return new Dal();
    }
}

在“ api”,“ impl01”和“ impl02”项目的META-INF目录中,使用以下行创建“ spring.factories”文件:

  • 'api'- org.springframework.boot.autoconfigure.EnableAutoConfiguration = pat.multidemo.dal.DalConfig

  • 'impl01'- org.springframework.boot.autoconfigure.EnableAutoConfiguration = pat.multidemo.dal.spi.impl01.Config01

  • 'impl02'- org.springframework.boot.autoconfigure.EnableAutoConfiguration = pat.multidemo.dal.spi.impl02.Config02

这对我有用。

谢谢