Sometimes the properties of a configuration file need to be injected into a static field of a class in a spring project
Example: File Upload
// file upload specified upload location // Resource-dev.properties has the following parameters #uploadUPLOAD_PATH=d:\\home\\imagesstatic_url=http://127.0.0.1/images/upload/
If you want to use it directly in a class, you can get the static field of the constant class by defining the
@Component @propertysource ("classpath:dev/resource-dev.properties") Public classConstantsfilename {//Upload Constants Public StaticString Upload_path; Public StaticString Static_url; PublicString Getuploadpath () {returnUpload_path; }//remove the static of the Set method@Value ("${upload_path}") Public voidSetuploadpath (String uploadpath) {Upload_path=Uploadpath; } PublicString Getstaticurl () {returnStatic_url; }//remove the static of the Set method@Value ("${static_url}") Public voidSetstaticurl (String staticurl) {Static_url=Staticurl; }}
At this point, you can use Constantsfilename.static_url and Constantsfilename.upload_path directly in your class.
Inject static fields into a class in a spring project