Hello, everyone, I am Yang Yang, recently, in the integration of OSS tool class, which used some static methods, methods need to use custom attributes, here or quite interesting, no more nonsense to say, open
First configure the custom properties
And over here, these are all custom values that we’re going to use
Then configure properties to read the property value
/ * * *@author yanglei
* @descOss Custom attribute configuration classes *@date2020/9/14 * /
@Component
@Data
public class OssConfiguration {
@Value("${aLiYun.oss.endpoint}")
private String endpoint;
@Value("${aLiYun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aLiYun.oss.accessKeySecret}")
private String accessKeySecret;
@Value("${aLiYun.oss.bucket}")
private String bucket;
@Value("${aLiYun.oss.path}")
private String path;
}
Copy the code
Note: Lombok is used here. If not, remove @data and produce your own get set method
Finally, the utility class is used inside
/ * * 1.@author yanglei
2. @descOss tools 3. <p> 4. Upload functions: images can be returned to online preview, files should not be too large, too large late fragment upload 5. Download function: Simple download 6.@date2020/9/14 * /
@Component
public class OssUtils {
private static final Logger log = LoggerFactory.getLogger(OssUtils.class);
/** Image format * for these formats when returning the preview interface */
private static final String FILE_SUFFIX_NAME = ".GIF|.JPG|.PNG";
private static String endpoint;
private static String accessKeyId;
private static String accessKeySecret;
private static String bucketName;
private static String path;
@Autowired
private OssConfiguration ossConfiguration;
@PostConstruct
public void init(a) {
endpoint = ossConfiguration.getEndpoint();
accessKeyId = ossConfiguration.getAccessKeyId();
accessKeySecret = ossConfiguration.getAccessKeySecret();
bucketName = ossConfiguration.getBucket();
path = ossConfiguration.getPath();
}
Copy the code
- Inject the OssConfiguration you just created
- In the utility class write the corresponding static variable corresponding to your custom property
- Write an init method for load initialization
- Using the @postconstruct method, which is executed when the project starts, you can also implement ApplicationRunner to initiate calls to the init method
Note: If you have questions about point 4, refer to my article: Initialize execution methods
conclusion
So, here on the good, attention points also write out, recently more trivial, update a little slow, as much as possible update it