问题:首先由一个FileUpLoadController,在该类中用Autowired注解注入FileUpUtils。 而类FileUpUtils须要一些配置参数,好比文件服务器接口的路径和方法名,而这些参数,咱们想要配置在properties文件中。 解决: 1.在properties中配置参数spring
#文件服务器 file.upload.url=http://100.10.0.10/fs-api/ upload.file.method=uploadFile.hc download.file.method=downloadFile.hc search.file.method=searchFile.hc remove.file.method=removeFile.hc
2.在xml中读取properties中的属性,并赋值到FileUpUtils的bean的property属性中。api
<!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:data-access.properties" /> </bean> <!-- 文件服务器工具类,由于上面已经读取properties文件,因此下面就能够直接取值 --> <bean id="fileUploadUtils" class="com.contract.service.utils.FileUploadUtils"> <property name="fileUploadUrl" value="${file.upload.url}"></property> <property name="uploadFile" value="${upload.file.method}"></property> <property name="downloadFile" value="${download.file.method}"></property> <property name="searchFile" value="${search.file.method}"></property> <property name="removeFile" value="${remove.file.method}"></property> </bean>
3.在FileUpUtils中直接用Autowired注解,获取fileUploadUrl,uploadFile等参数。 可是这种方法在项目启动的时候报错。这是由于在扫描组件的时候,初始化FileUploadController时候,要初始化FileUpUtils,可是这个时候,FileUpUtils中的fileUploadUrl等参数,尚未被初始化,获取不到所以,没有值。这个时候程序启动的时候被报错。由于Autowired属性默认的是required=true,也就是该参数是必须的,所以报错,这个时候咱们在@Autowired注解后面加一个required=false。也就是@Autowired(required=false) 这样就能够了,并且程序启动后进行程序操做的时候,FileUpUtils已经初始化成功,而且参数注入,也就是能够正常的进行使用了。服务器