使用Maven部署Spring-Boot项目,并将其进一步导入其他项目中 [英] Deploying a Spring-Boot project using Maven and further importing it in other projects
问题描述
因此,目前,我有两个项目,比如说Storefront和Dashboard,这两个项目都具有相同的POJO类,服务和一些API端点.例如,考虑一个 Student 类,我们在Storefront和Dashboard中都有该类及其服务类.此外,在不久的将来,我们将为客户实施另一个项目,即客户仪表板",该项目将拥有几乎90%的相同资源.
So currently, I have two projects, let's say Storefront and Dashboard, and both of these projects have the same POJO classes, Services, and some API Endpoints. For example, consider a class Student, we have this class in Storefront as well as in the Dashboard along with its service class. Further, in near future, we'll be implementing another project for clients, a Client Dashboard, that'll have almost 90% of the same resources.
所以我想,如果我创建一个包含所有项目所需要的所有库的maven项目,然后根据需要将其导入.这将减少冗余,并使其他项目的权重降低.我以前曾使用项目内存储库和Dropbox作为Maven存储库,所以这次对我来说会更容易一些.
So I thought, what if I create a maven project with all the libraries I need across all projects and import them as needed. This would reduce the redundancy and would also make other projects light weighted. I've previously used the in-project repository and Dropbox as a maven repository so it'll be a bit easier for me this time.
现在的问题是:我有与 Student 相对应的 StudentRepository ,还可以与'@Autowired'注释一起使用吗?据我所知,当我运行'@SpringBootApplication'时,一切都会正常工作,但是正如我之前提到的,我将导入这些包,并且这样做,程序将通过NullPointerException导致StudentRepository的实例为null.
Now the problem is: I have the StudentRepository corresponding to the Student, which is further used with the '@Autowired' annotation right? As per my knowledge, everything will work when I run the '@SpringBootApplication' but as I previously mentioned, I'll be importing these packages, and doing so, the program will through a NullPointerException cause the instance of the StudentRepository will be 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");
}
}
那么还有其他方法可以使这项工作吗?任何建议将不胜感激,并在此先感谢.
So is there any other way to make this work? Any suggestions will be appreciated and thanks in advance.
推荐答案
假定您有一个库FooLibrary
和主应用程序FooApplication
.这个想法是在FooApplication
中导入FooLibrary
.
Assume that you have a library FooLibrary
and main application FooApplication
. The idea is importing FooLibrary
in FooApplication
.
让我们从FooLibrary
开始.因此,主要有2个重要文件.它们是FooLibraryInterface
和FooLibraryConfiguration
.在此示例中,我将不使用FooLibraryInterface
.
Let's begin with FooLibrary
. So mainly there are 2 important files. These are FooLibraryInterface
and FooLibraryConfiguration
. In this example I will not use FooLibraryInterface
.
FooLibraryInterface
是一个接口,其中包含客户端可能需要使用override
和FooLibraryConfiguration
来扫描和注入在FooLibrary
中找到的bean的重要方法.所以,这里是
FooLibraryInterface
is an interface that holds important methods that the client might need to override
and FooLibraryConfiguration
to scan and inject beans found in FooLibrary
. So, here it follows
public interface FooLibraryInterface {
public abstract Datasource configureDatabaseConnection();
}
FooLibraryConfiguration
将如下所示:
@Configuration
@ComponentScan("package.to.scan")
public class FooLibraryConfiguration {
@Bean
public YourBean beanName() {
return new YourBean();
}
}
您可以在FooLibrary
中添加所有需要的内容.现在我们的库已准备好在FooLibraryConfiguration
You can add all that you need in FooLibrary
. Now our library is ready to be imported in FooApplication
with the help of 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")
一起保留单独的扫描)>
NOTE: If you have EntityManager
managed classes(entity, repository), you should do an additional scan of the lib packages in the main application, since we don't need to have different EntityManager
for a single application. Or you may scan all files together (leaving individual scans from @EnableJpaRepositories or em.setPackagesToScan("library.entities.package"); with @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
这篇关于使用Maven部署Spring-Boot项目,并将其进一步导入其他项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!