这是我的一个配置文件中的两个bean
<bean id="loadBean" class="com.bytime.toss.framework.web.model.LoadBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean> <bean id="reportJob" class="com.bytime.toss.finance.charge.web.scheduler.job.ReportJob">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
其中loadBean注入的dataSource我可以正常使用,但是在reportJob中注入的dataSource却无法使用,总是告诉我,dataSource是null,这到底是为什么呀,高手们,注入的方式都是用的setDataSource呀为什么就不行呢?
<bean id="loadBean" class="com.bytime.toss.framework.web.model.LoadBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean> <bean id="reportJob" class="com.bytime.toss.finance.charge.web.scheduler.job.ReportJob">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
其中loadBean注入的dataSource我可以正常使用,但是在reportJob中注入的dataSource却无法使用,总是告诉我,dataSource是null,这到底是为什么呀,高手们,注入的方式都是用的setDataSource呀为什么就不行呢?
这是我的LoadBean对应的代码:
import javax.sql.DataSource;
public class LoadBean
{
private DataSource dataSource; public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
} public boolean execReportFile(String file, String licenceLoca,
Map paramMap, Map macroMap, String deslocal)
{
boolean execResult = false;
try
{
ExtCellSet.setLicenseFileName(licenceLoca); // 取得报表定义文件对象
ReportDefine rd = new ReportDefine(file); // 设置参数
String[] argumentNames = rd.getArguments().getArgumentNames(); if (argumentNames != null && paramMap != null)
{
for (int i = 0; i < argumentNames.length; i++)
{
rd.getArguments().get(i).value = (String)(paramMap.get(argumentNames[i])) == null ? "" :new String(((String)(paramMap.get(argumentNames[i]))).getBytes("iso-8859-1"));
System.out.println(rd.getArguments().get(i).value);
}
}
// 取得报表运算环境,并运算
Env env = new Env(rd);
env.setConnection(dataSource.getConnection(), "GBK", DBTypes.SQLSVR); Engine eg = new Engine(env); Report rp = eg.calc(); String outfilename = JobManager.resultdir+"\\" +"reval"+".xsl";
FileOutputStream out = new FileOutputStream(outfilename);
rp.exportToExcel(out,false); execResult = true;
}
catch (Exception e)
{
e.printStackTrace();
} return execResult;
}
}
这是我的ReportJob对应的代码:
import javax.sql.DataSource;public class ReportJob implements Job
{ public static final String KEY = "Report_TaskPeer"; private DataSource dataSource; public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
} public void execute(JobExecutionContext context)
throws JobExecutionException
{
Map paramMap=new HashMap();
/**获得任务*/
Task task = (Task) context.getJobDetail().getJobDataMap().get(KEY);
/** 获得要执行的报表文件名 */
String reportfile = task.getReportfilename();
/** 报表要生成的类型 */
String outtype = task.getResulttype();
String reportParamInfo=task.getParaminfo();
try
{
String filePathName=JobManager.filePath+"/"+reportfile;
ReportDefine rd = new ReportDefine(new FileInputStream(filePathName));
ExtCellSet.setLicenseFileName(JobManager.licensePath);
if(reportParamInfo!=null||!reportParamInfo.equals(""))
{
String[] params=split(reportParamInfo,";");
for(int i=0;i<params.length;i++)
{
String[] singleParam=split(params[i],"=");
paramMap.put(singleParam[0], singleParam[1]);
}
//设置参数
String[] argumentNames = rd.getArguments().getArgumentNames();
if (argumentNames != null && paramMap != null)
{
for (int i = 0; i < argumentNames.length; i++)
{
rd.getArguments().get(i).value = (String)(paramMap.get(argumentNames[i])) == null ? "" :(String)(paramMap.get(argumentNames[i]));
System.out.println(rd.getArguments().get(i).value);
}
}
}
Env env = new Env(rd);
Connection con=dataSource.getConnection();
env.setConnection(con, "GBK", DBTypes.SQLSVR);
Engine eg = new Engine(env); Report report = eg.calc();
if (outtype.equalsIgnoreCase("excel"))
{
String outfilename = JobManager.resultdir+"\\" +task.getResultname()+".xls";
report.exportToExcel(new FileOutputStream(outfilename), false);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String[] split(String string,String separator)
{
StringTokenizer st = new StringTokenizer(string, separator);
int size = st.countTokens();
String[] strings = new String[size];
for(int i=0;st.hasMoreTokens();i++){
strings[i] = st.nextToken();
}
return strings;
}
}
着两个文件loadbean可以执行,reportjob却不能,错误就出在Connection con=dataSource.getConnection();一运行就报null
{
this.dataSource = dataSource;
}
对应的再写一个 getter 方法
然后通过 Connection con=this.getDataSource().getConnection(); 调用