配置文件yml # phantomjs的位置地址 phantomjs: binPath: windows: binPath-win linux: binPath-linux jsPath: windows: jsPath-win linux: jsPath-linux imagePath: wind ...
配置文件yml
# phantomjs的位置地址
phantomjs:
binPath:
windows: binPath-win
linux: binPath-linux
jsPath:
windows: jsPath-win
linux: jsPath-linux
imagePath:
windows: imagePath-win
linux: imagePath-linux
phantomjs2:
binPath2: I‘m binPath2
binPath3: I‘m binPath3
一、@Value
1、常規方式
- 註入(需要把類交給spring)
@Data
@Component
public class PhantomPath {
@Value("${phantomjs.binPath.windows}")
private String binPathWin;
@Value("${phantomjs.jsPath.windows}")
private String jsPathWin;
@Value("${phantomjs.binPath.linux}")
private String binPathLinux;
@Value("${phantomjs.jsPath.linux}")
private String jsPathLinux;
@Value("${phantomjs.imagePath.windows}")
private String imagePathWin;
@Value("${phantomjs.imagePath.linux}")
private String imagePathLinux;
//下麵可以直接在方法中使用
}
- 使用(可以直接在註入的類中使用)
@Resource
private PhantomPath phantomPath;
@Test
public void test03()throws Exception{
System.out.println(phantomPath.getBinPathWin());
System.out.println(phantomPath.getJsPathWin());
System.out.println(phantomPath.getBinPathLinux());
System.out.println(phantomPath.getJsPathLinux());
System.out.println(phantomPath.getImagePathWin());
System.out.println(phantomPath.getImagePathLinux());
}
- 測試
2、註入到靜態屬性上
-
解釋
不能這樣直接註入到靜態屬性上
這樣是獲取不到值的
-
註入(需要註入到非靜態set方法上,再複製給靜態屬性)
package com.cc.urlgethtml.utils;
import lombok.Data;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* <p>根據不同系統獲取不同路徑</p>
*
* @author CC
* @since 2023/11/3
*/
@Component
public class PhantomPathStatic {
public static String binPathWin;
public static String jsPathWin;
//必須是非靜態的set方法
@Value("${phantomjs.binPath.windows}")
public void setBinPathWin(String binPathWin) {
PhantomPathStatic.binPathWin = binPathWin;
}
@Value("${phantomjs.jsPath.windows}")
public void setJsPathWin( String jsPathWin) {
PhantomPathStatic.jsPathWin = jsPathWin;
}
public static String getBinPathWin() {
return binPathWin;
}
public static String getJsPathWin() {
return jsPathWin;
}
}
- 使用(有兩種方式:靜態屬性方式、get方式)
@Resource
private PhantomPathStatic phantomPathStatic;
@Test
public void test04()throws Exception{
System.out.println(phantomPathStatic.getBinPathWin());
System.out.println(PhantomPathStatic.binPathWin);
System.out.println(phantomPathStatic.getJsPathWin());
System.out.println(PhantomPathStatic.jsPathWin);
}
- 測試
一、@ConfigurationProperties
1、常規方式
- 註入
@Data
@Component
@ConfigurationProperties(prefix = "phantomjs2")
public class PhantomConPro {
private String binPath2;
private String binPath3;
}
- 使用、測試
2、獲取map方式
- 註入
package com.cc.urlgethtml.utils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* <p></p>
*
* @author CC
* @since 2023/11/7
*/
@Data
@Component
@ConfigurationProperties(prefix = "phantomjs")
public class PhantomConProMap {
private Map<String, String> binPath;
private Map<String, String> jsPath;
private Map<String, String> imagePath;
}
- 使用、測試
3、註入到靜態屬性上
- 註入
package com.cc.urlgethtml.utils;
import lombok.Data;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* <p></p>
*
* @author CC
* @since 2023/11/7
*/
@Component
@ConfigurationProperties(prefix = "phantomjs")
public class PhantomConProMapStatic {
@Getter
public static Map<String, String> binPath;
@Getter
public static Map<String, String> jsPath;
@Getter
public static Map<String, String> imagePath;
//必須是非靜態的set方法
public void setBinPath(Map<String, String> binPath) {
PhantomConProMapStatic.binPath = binPath;
}
public void setJsPath(Map<String, String> jsPath) {
PhantomConProMapStatic.jsPath = jsPath;
}
public void setImagePath(Map<String, String> imagePath) {
PhantomConProMapStatic.imagePath = imagePath;
}
}
- 使用、測試(三種使用方式)
三、總結
參考:https://zhuanlan.zhihu.com/p/639448969