第一种通过注解方式实现初始化,这种方法初始化行为发生在所有构造函数之后进行,
可参考:http://blog.youkuaiyun.com/yaerfeng/article/details/8447530
java代码:
@Resource(name = "sqlSessionTemplate")
private SqlSessionTemplate sst;
@PostConstruct
protected void setp(){
String namespace = "bis.domain.RoleSettingPageMapping.";
CONFZZ.put("zz",sst.selectList(namespace + "getRole"));
System.out.println("============================================");
System.out.println(CONFZZ);
System.out.println("============================================");
}
xml代码:
<bean id="commonMethod" class="com.xxx.fbi.rps.util.CommonMethod"></bean>
第二和第三中方法使用注入来实现,由于依赖sqlSessionTemplate的bean,所以需要配置依赖,如下:
1、使用property属性来配置依赖注入:
java代码:
public void setCommonMethod(SqlSessionTemplate sqlSessionTemplate) {
String namespace = "bis.domain.RoleSettingPageMapping.";
this.sst = sqlSessionTemplate;
try {
CONFZZ.put("zaaxxz", sst.selectList(namespace + "getRole"));
}catch(Exception e){
}
System.out.println(namespace);
}
xml代码:
<bean id="commonMethod" class="com.xxx.fbi.rps.util.CommonMethod">
<property name="commonMethod" ref="sqlSessionTemplate"></property>
</bean>
2、使用构造函数来配置依赖注入:
java代码:
public CommonMethod(SqlSessionTemplate sqlSessionTemplate) {
String namespace = "bis.domain.RoleSettingPageMapping.";
this.sst = sqlSessionTemplate;
try {
CONFZZ.put("zaaxxz", sst.selectList(namespace + "getRole"));
}catch(Exception e){
}
System.out.println(namespace);
}
xml代码:
<bean id="commonMethod" class="com.xxx.fbi.rps.util.CommonMethod"> <constructor-arg index="0" ref="sqlSessionTemplate" /> </bean>