Kar*_*ani 4 java maven-2 reactjs spring-boot
我想使用Spring Boot开发示例REST API项目.我很困惑,什么应该是正确的做法,因为我们有包装多个选项,如war,jar等.
我要求我有外部库文件夹,它有多个jar和资源文件,将在REST API和前端使用(使用React).
由于动态变化,我希望将jar和资源保留为外部依赖项,我不想将它们包含在项目中.我已经尝试使用loader.path使用的示例项目jar工作正常但相同的方法不使用war文件.我使用Maven作为构建工具.
war或jar?libSpring Boot的外部文件夹 - 我找不到解决方案.duf*_*ymo 13
你应该把它变成一个可执行的Spring Boot JAR.
如果必须在Java EE服务器上部署WAR,则只需要WAR.
你使用Maven很好.让它管理您的依赖项并构建包.
您想要找到创建可执行JAR的Maven插件,其中包含依赖项.
更新:
以下是我对您的四个问题的回答:
mvn install您声称需要的所有外部库放在本地.m2或Maven存储库中.我建议您考虑单独部署REST服务,让React前端调用它.将两者分开.让REST服务成为一个独立的微服务,没有UI.
是否选择jar或war取决于您是否需要独立的可执行应用程序,或者您希望在Weblogic等服务器上部署项目.假设我的应用程序是复杂项目的中间层或适配器(帮助应用程序),我会将其作为战争部署在WebLogic上.
在你的情况下我的建议是使用JAR而不是WAR.要构建jar,请使用mvn clean install命令.
为了加载外部属性文件,您需要做的就是将文件夹名称和属性名称作为命令行参数的一部分传递,如下所示:
java -jar myapp.jar --spring.config.name=application,myapp
-- spring.config.location=classpath:/data/myapp/config,classpath:/data/myapp/external/config
Run Code Online (Sandbox Code Playgroud)
为了从外部导入资源,您可以使用
Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");
Run Code Online (Sandbox Code Playgroud)
代码段
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class CustomResourceLoader implements ResourceLoaderAware
{
private ResourceLoader resourceLoader;
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public void showResourceData() throws IOException
{
//This line will be changed for all versions of other examples
Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");
InputStream in = banner.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while (true) {
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);
}
reader.close();
}
}
Run Code Online (Sandbox Code Playgroud)
此文件的applicationContext.xml文件条目如下:
<bean id="customResourceLoader" class="com.howtodoinjava.demo.CustomResourceLoader"></bean>
Run Code Online (Sandbox Code Playgroud)
附录-
| 归档时间: |
|
| 查看次数: |
2638 次 |
| 最近记录: |