SpringBoot集成SAP的過程及本地IDEA啟動和Windows服務(wù)器部署
網(wǎng)上有關(guān)Springboot集成SAP有各種各樣的說法,我這里提供一個我調(diào)通的方案。如下pom配置,sapjco3.dll 放在"%JAVA_HOME%\bin\",否則報nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.sap.conn.jco.JCo,服務(wù)器部署時也是這樣放置,
sapjco3.dll和sapjco3.jar放在resources\lib下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.demo</groupId>
<artifactId>OPM</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>open-manager</artifactId>
<packaging>jar</packaging>
<name>open-manager</name>
<description>open-manager</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.demo</groupId>
<artifactId>common</artifactId>
<!--不加版本無法引用-->
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sapjco3</groupId>
<artifactId>sapjco</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/main/resources/lib/sapjco3.jar</systemPath>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<profiles>
<!-- 本地環(huán)境 -->
<profile>
<id>local</id>
<properties>
<spring.profiles.active>local</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 開發(fā)環(huán)境 -->
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<!--為false,取消本地的system的jar打入-->
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- true表示利用占位符進行替換 -->
<filtering>true</filtering>
<includes>
<!--Maven 資源過濾是為文本文件設(shè)計的(如屬性文件、XML 等)-->
<include>**/*.properties</include>
<include>application.yml</include>
<!-- ${spring.profiles.active}就是我們指定的環(huán)境,會被替換掉 -->
<include>application-${spring.profiles.active}.yml</include>
<include>**/*.xml</include>
<include>**/*.html</include>
<include>**/*.json</include>
<include>webapp/</include>
</includes>
<excludes>
<!-- 排除目錄 -->
<exclude>lib/**</exclude>
<exclude>**/*.jar</exclude>
<exclude>**/*.dll</exclude> <!--sapjco3.dll "%JAVA_HOME%\bin\"-->
<exclude>**/*.jks</exclude>
<exclude>**/*.png</exclude>
</excludes>
</resource>
<!-- 新增:單獨處理二進制文件(不進行過濾) -->
<resource>
<directory>src/main/resources/lib</directory>
<filtering>false</filtering>
<targetPath>BOOT-INF/lib</targetPath> <!-- 關(guān)鍵修改 -->
<includes>
<include>sapjco3.jar</include>
</includes>
</resource>
<!-- 處理其他資源 -->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<targetPath>BOOT-INF/classes</targetPath>
<excludes>
<exclude>lib/**</exclude> <!-- 排除已單獨處理的 lib 目錄 -->
</excludes>
<includes>
<include>**/*.jks</include>
<include>**/*.png</include>
</includes>
</resource>
</resources>
</build>
</project>package com.demo.openmanager.sap;
import com.demo.common.util.StringUtils;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
@Slf4j
public class XpJco {
@Value("${sap.host}")
private String SAP_HOST;
@Value("${sap.client}")
private String SAP_CLIENT;
@Value("${sap.sysno}")
private String SAP_SYS_NO;
@Value("${sap.user}")
private String SAP_USER;
@Value("${sap.pass}")
private String SAP_PASS;
@Value("${sap.lang}")
private String SAP_LANG;
@Value("${sap.poolCapacity}")
private String SAP_POOL_CAPACITY;
@Value("${sap.peakLimit}")
private String SAP_PEAK_LIMIT;
@Value("${sap.jcoSaprouter}")
private String SAP_JCO_SAPROUTER;
public synchronized JCoDestination getJCoDestination(String filename) {
JCoDestination destination = null;
try {
destination = JCoDestinationManager.getDestination(filename);
} catch (JCoException e) {
e.printStackTrace();
}
return destination;
}
private static boolean creatSapPros(String filename, String host, String sysno, String client, String user, String pass,
String lang, String pool_capacity, String peak_limit, String JCO_SAPROUTER) {
Properties pros = new Properties();
boolean iscreate = false;
pros.clear(); // 先清空
pros.setProperty(DestinationDataProvider.JCO_ASHOST, host);
pros.setProperty(DestinationDataProvider.JCO_SYSNR, sysno);
pros.setProperty(DestinationDataProvider.JCO_CLIENT, client);
pros.setProperty(DestinationDataProvider.JCO_USER, user);
pros.setProperty(DestinationDataProvider.JCO_PASSWD, pass);
pros.setProperty(DestinationDataProvider.JCO_LANG, lang);
pros.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, pool_capacity);
pros.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, peak_limit);
if (StringUtils.isNoneBlank(JCO_SAPROUTER)) {
pros.setProperty(DestinationDataProvider.JCO_SAPROUTER, JCO_SAPROUTER);
}
iscreate = createFiles(filename, pros);
if (iscreate) {
return true;
} else {
return false;
}
}
/**
* @param filename
* @return
*/
private static boolean isFileExist(String filename) {
File file = new File(filename + ".jcoDestination");
log.info("JCO path:" + file.getPath());
log.info("JCO AbsolutePath:" + file.getAbsolutePath());
// 最后會放在Q:\IdeaWork\OPM\tz02_ywk_jco.jcoDestination
// 環(huán)境上是放在啟動jar包所在同級目錄
return file.exists();
}
/**
* @param filename
* @param pros
* @return
*/
private static boolean createFiles(String filename, Properties pros) {
File file = new File(filename + ".jcoDestination");
FileOutputStream fos = null;
if (!file.exists()) {
try {
log.info("********* 正在寫入文件 **********");
fos = new FileOutputStream(file, false);
pros.store(fos, "Author: ljb");
fos.close();
return true;
} catch (FileNotFoundException e) {
log.info("-------- 找不到文件! ---------");
e.printStackTrace();
return false;
} catch (IOException e) {
log.info("-------- 內(nèi)容寫入失??! ---------");
e.printStackTrace();
return false;
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
return false;
}
}
public synchronized JCoDestination getJCoDestination() {
boolean isexist = false; // 判斷文件是否存在
boolean isCreate = false; // 創(chuàng)建pro文件
JCoDestination destination;
String filename = "tz02_ywk_jco";
isexist = XpJco.isFileExist(filename);
log.info("-- 獲取JCoDestination isexist==" + isexist);
if (isexist == true) {
try {
destination = JCoDestinationManager.getDestination(filename);
return destination;
} catch (JCoException e) {
log.info("-- 獲取JCoDestination失敗! --");
e.printStackTrace();
return null;
}
} else {
isCreate = XpJco.creatSapPros(filename, SAP_HOST, SAP_SYS_NO, SAP_CLIENT, SAP_USER, SAP_PASS, SAP_PASS,
SAP_POOL_CAPACITY, SAP_PEAK_LIMIT, SAP_JCO_SAPROUTER);
if (isCreate == true) {
try {
destination = JCoDestinationManager.getDestination(filename);
log.info("獲取JCoDestination!成功");
return destination;
} catch (JCoException e) {
log.info("無法獲取JCoDestination!");
e.printStackTrace();
return null;
}
} else { // 如果文件不存在
return null;
}
}
}
}到此這篇關(guān)于SpringBoot集成SAP,本地IDEA啟動和Windows服務(wù)器部署的文章就介紹到這了,更多相關(guān)SpringBoot集成SAP內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)
- IntelliJ?IDEA社區(qū)版2021.3配置SpringBoot項目詳細教程及失敗案例
- IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項目
- IntelliJ?IDEA運行SpringBoot項目的詳細步驟
- SpringBoot項目在IntelliJ IDEA中如何實現(xiàn)熱部署
- Intellij IDEA實現(xiàn)SpringBoot項目多端口啟動的兩種方法
- intellij IDEA配置springboot的圖文教程
- IntelliJ Idea SpringBoot 數(shù)據(jù)庫增刪改查實例詳解
相關(guān)文章
深入淺析Java Object Serialization與 Hadoop 序列化
序列化是指將結(jié)構(gòu)化對象轉(zhuǎn)化為字節(jié)流以便在網(wǎng)絡(luò)上傳輸或者寫到磁盤永久存儲的過程。下面通過本文給大家分享Java Object Serialization與 Hadoop 序列化,需要的朋友可以參考下2017-06-06
Spring實現(xiàn)Logback日志模板設(shè)置動態(tài)參數(shù)功能
本文介紹如何在Spring應(yīng)用中實現(xiàn)Logback日志模板的動態(tài)參數(shù)設(shè)置,通過設(shè)計traceId鏈路標識,將其動態(tài)添加到日志輸出中,從而實現(xiàn)簡易的鏈路追蹤功能,感興趣的朋友跟隨小編一起看看吧2024-11-11
spring項目如何配置多數(shù)據(jù)源(已上生產(chǎn),親測有效)
這篇文章主要介紹了spring項目如何配置多數(shù)據(jù)源(已上生產(chǎn),親測有效),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Mybatis分頁插件PageHelper配置及使用方法詳解
這篇文章主要介紹了Mybatis分頁插件PageHelper配置及使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08

