spring对bean的生命周期管理的比较精细,并不是单纯的new()实例化.
1,找到class配置信息并将其实例化
2,受用依赖注入,按照配置信息,配置bean的所有属性;
在一个开始使用前可以用配置初始化一些参数.
方法一:用注解@PostConstruct,也就是相当于init-method
package com.weitoo.server.task; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.qianmi.open.api.DefaultOpenClient;
import com.qianmi.open.api.OpenClient;
import com.weitoo.server.mapper.PropertyMapper; /**
* 授权和充值消息推送父类
*
* @author 钟政 暂时将这些字段值写死
*/
@Component
public class BaseTask {
@Resource
private PropertyMapper propertyMapper; private static String API_URL = ""; protected static String APP_KEY = ""; protected static String APP_SECRET = ""; protected static String accessToken = "";
protected static String refreshToken = "";
// 创建一个客户端,默认连接超时时间为3秒,请求超时时间为15秒。
protected static OpenClient client = new DefaultOpenClient(API_URL, APP_KEY, APP_SECRET); //方法二:是利用spring实例化和注入依赖后会执行的注解.
@PostConstruct
private void initMethod(){
if("".equals(accessToken)){
accessToken = propertyMapper.getValue("access_token").getData();
}
if("".equals(refreshToken)){
refreshToken = propertyMapper.getValue("refresh_token").getData();
}
if("".equals(APP_KEY)){
APP_KEY=propertyMapper.getValue("app_key").getData();
}
if("".equals(APP_SECRET)){
APP_SECRET=propertyMapper.getValue("app_secret").getData();
}
API_URL=propertyMapper.getValue("api_url").getData();
//
client = new DefaultOpenClient(API_URL, APP_KEY, APP_SECRET);
} }
上面的方式是通过数据库配置一些初始化参数.
方法二:通过spring-config 直接配置.所配置的属性必须有set方法
如果是想从maven中的pom.xml文件中获取,需要中间文件*.properties文件.
如获取pom属性如下
<properties> <wxpay-scanpay-scancode-url>https://api.mch.weixin.qq.com/pay/unifiedorder</wxpay-scanpay-scancode-url> <wxpay-scanpay-pay-api-url>https://api.mch.weixin.qq.com/pay/micropay</wxpay-scanpay-pay-api-url> <wxpay-scanpay-pay-query-url>https://api.mch.weixin.qq.com/pay/orderquery</wxpay-scanpay-pay-query-url> <wxpay-scanpay-refund-url>https://api.mch.weixin.qq.com/secapi/pay/refund</wxpay-scanpay-refund-url> <wxpay-scanpay-refund-query-url>https://api.mch.weixin.qq.com/pay/refundquery</wxpay-scanpay-refund-query-url> <wxpay-scanpay-reverse-api-url>https://api.mch.weixin.qq.com/secapi/pay/reverse</wxpay-scanpay-reverse-api-url> <wxpay-scanpay-download-bill-url>https://api.mch.weixin.qq.com/pay/downloadbill</wxpay-scanpay-download-bill-url> <wxpay-scanpay-report-api-url>https://api.mch.weixin.qq.com/payitil/report</wxpay-scanpay-report-api-url> </properties>
*.properties的文件如下:wxpay.properties
scanpay.scancode.url=${wxpay-scanpay-scancode-url}
scanpay.pay.api.url=${wxpay-scanpay-pay-api-url} scanpay.pay.query.url =${wxpay-scanpay-pay-query-url}
scanpay.refund.url=${wxpay-scanpay-refund-url} scanpay.refund.query.url=${wxpay-scanpay-refund-query-url} scanpay.reverse.api.url=${wxpay-scanpay-reverse-api-url}
scanpay.download.bill.url=${wxpay-scanpay-download-bill-url}
scanpay.report.api.url=${wxpay-scanpay-report-api-url}
在spring-config.xml关键配置如下:
<bean id="propertyConfigurer" class="com.weitoo.server.security.SecurityPropertyConfigurer">
<property name="locations">
<list> <!-- 微信支付初始化参数 -->
<value>classpath:property/wxpay.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
其中com.weitoo.server.security.SecurityPropertyConfigurer类是继承了org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.直接用PropertyPlaceholderConfigurer类也行.主要是解析properties文件
spring-config.xml 初始化类参数如下,ps:scope="singleton" 代表该类生命周期属于spring管理.
<bean id="configure" class = "com.tencent.common.Configure" scope="singleton"> <property name="REFUND_API" value="${scanpay.refund.url}"/>
<property name="REFUND_QUERY_API" value="${scanpay.refund.query.url}"/>
<property name="REVERSE_API" value="${scanpay.reverse.api.url}"/>
<property name="DOWNLOAD_BILL_API" value="${scanpay.download.bill.url}"/>
<property name="REPORT_API" value="${scanpay.report.api.url}"/>
</bean>