[英]Alternatives to distribute spring-boot application using maven (other than spring-boot:repackage)
[英]Deploying a Spring-Boot project using Maven and further importing it in other projects
所以目前,我有两个项目,比如说 Storefront 和 Dashboard,这两个项目都有相同的 POJO 类、服务和一些 API 端点。 例如,考虑一个 class学生,我们在店面和仪表板中都有这个 class 及其服务 class。此外,在不久的将来,我们将为客户实施另一个项目,一个客户仪表板,它将具有几乎 90% 的相同资源。
所以我想,如果我创建一个 maven 项目,其中包含我在所有项目中需要的所有库并根据需要导入它们会怎样。 这将减少冗余并使其他项目轻量化。 我以前使用项目内存储库和 Dropbox 作为 maven 存储库,所以这次对我来说会更容易一些。
现在的问题是:我有对应于Student的StudentRepository ,它进一步与 '@Autowired' 注释一起使用,对吗? 据我所知,当我运行“@SpringBootApplication”时,一切都会正常工作,但正如我之前提到的,我将导入这些包,这样做时,程序将通过 NullPointerException 导致 StudentRepository 的实例为 null。
@SpringBootApplication
@EnableAutoConfiguration
@EntityScan
public class DemoApplication implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
StudentRepository repository;
public static void main(String[] args) {
SpringApplication.run(PaperTrueLibrariesApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// This will work
logger.info("Inserting -> {}", repository.save(new Student("studentName", "primaryKey")));
}
// This won't
public void addAStudent(String studentName, String primaryKey) {
repository.save(new Student(studentName, primaryKey));
}
}
public class Test {
public static void main(String[] args) {
// This will throw a NullPointerException
new DemoApplication().addAStudent("yourNameProbably", "yourSocialSecurityNumber");
}
}
那么还有其他方法可以使这项工作吗? 任何建议将不胜感激,并提前致谢。
有两种方法可以实现你想要的 -
首先,在当前项目中添加您已经在构建文件中创建的所需的 jar,例如 pom.xml 为 maven。
如果 class 或您在不同项目中导入的代码不是 spring bean ,那么
在您的配置 class 中,使用 @Bean 注释将其作为 bean 返回。
@Configuration
public class YourConfigurationClass {
@Bean
SomeBean returnSomeBean() {
return new SomeBean();
}
如果您的 class已经是一个 spring bean ,您只需要求 spring 扫描所需的 package,因为您已经通过在 maven 中添加依赖项在您的 class 路径中拥有它。
@Configuration
@ComponentScan({"com.yourpackage.fromCommanCodeJar"})
public class YouApplicationConfigurationClass {....}
假设您有一个库FooLibrary
和主应用程序FooApplication
。 这个想法是在FooApplication
FooLibrary
让我们从FooLibrary
开始。 所以主要有2个重要文件。 它们是FooLibraryInterface
和FooLibraryConfiguration
。 在这个例子中我不会使用FooLibraryInterface
。
FooLibraryInterface
是一个接口,其中包含客户端可能需要override
的重要方法和FooLibraryConfiguration
以扫描和注入在FooLibrary
中找到的 bean。 所以,下面是
public interface FooLibraryInterface {
public abstract Datasource configureDatabaseConnection();
}
FooLibraryConfiguration
将如下所示:
@Configuration
@ComponentScan("package.to.scan")
public class FooLibraryConfiguration {
@Bean
public YourBean beanName() {
return new YourBean();
}
}
您可以在FooLibrary
中添加您需要的所有内容。 现在我们的库已准备好在FooApplication
的帮助下FooLibraryConfiguration
@SpringBootApplication
@Import(FooLibraryConfiguration.class) //this will import all the beans defined in the library
public class FooApplication {
public static void main(String[] args) {
SpringApplication.run(FooApplication.class, args);
}
}
注意:如果您有EntityManager
托管类(实体、存储库),您应该对主应用程序中的 lib 包进行额外扫描,因为我们不需要为单个应用程序使用不同的EntityManager
。 或者您可以一起扫描所有文件(留下@EnableJpaRepositories或em.setPackagesToScan("library.entities.package");和@ComponentScan("base.library.package")
的单独扫描)
@Configuration
@EnableJpaRepositories(basePackages = { "library.repository.package" })
@EnableTransactionManagement
public class PersistenceConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("library.entities.package");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
.....
}
更新
谢谢@keshavram-kuduwa ,您可以从https://spring.io/guides/gs/multi-module/#_create_the_library_project获得 spring 指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.