bean属性名称和引用 - 不同的值 - 空指针异常。

时间:2013-10-31 14:37:10

标签: java spring spring-mvc

全部。随附所有文件。如果我为bean属性namereference指定了相同的名称,则返回该对象。但它返回null,以防两者都不同。请帮忙。非常感谢。

AuthenticateAction.java文件里面有详细的评论......

下面的

是我的文件列表:

WIRAdminBaseAction.java ---> my base action 
AuthenticateAction.java ---> my java file that calls the bean here 
applicationContext_MNC.xml 
  

以下称呼作品的方式......我们并不鼓励这样做   不应该始终使用applicationContext

ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletRequest().getSession().getServletConte xt()); 
ICacheDelegate cacheAction = (ICacheDelegate)applicationContext.getBean("BMOCacheDelegate"); 
  

以下调用方式不起作用....返回null值....   请帮助...我认为,因为我已经延长了   WIRAdminBaseAction,我应该可以调用getCacheDelegate   直接,它应该返回我的cacheDelegate对象...再次,   请注意.....如果我更改我的applicationContext_MNC.xml如下,   以下的通话方式工作正常。但是,我不想改变我的   applicationContext_MNC.xml如下所示,由于某些必要性。

<bean id="BMOWIRAdminBaseAction" class="com.symcor.wiradmin.web.action.WIRAdminBase Action"> 
<property name="cacheDelegate"> 
<ref bean="cacheDelegate" /> 
</property> 
</bean> 

<bean id="cacheDelegate" class="com.symcor.wiradmin.delegate.CacheDelegate" > 
<property name="statusDBDAO"><ref bean="BMOStatusDBDAO" /></property> 
</bean> 
  

是名称和bean应该具有相同的值.... ???没有   需要......我是对的吗?请指教。

getCacheDelegate().getActorAction(1); // this way of calling doesn't work and returns null value. please help. 

我的代码如下。


AuthenticateAction.java 

package com.symcor.wiradmin.web.action; 

import java.net.UnknownHostException; 
import java.sql.SQLException; 

import org.bouncycastle.crypto.CryptoException; 
import org.springframework.context.ApplicationContext; 
import org.springframework.dao.DataAccessException; 
import org.springframework.web.context.support.WebApplica tionContextUtils; 

import com.symcor.wiradmin.delegate.ICacheDelegate; 

public class AuthenticateAction extends WIRAdminBaseAction { 

private static final long serialVersionUID = 1L; 

public String authenticate() throws UnknownHostException, CryptoException, 
DataAccessException, SQLException{ 

/** This way of calling works...... This is not encouraged, as we should not use applicationContext always **/ 
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContex t(getServletRequest().getSession().getServletConte xt()); 
ICacheDelegate cacheAction = (ICacheDelegate) applicationContext.getBean("BMOCacheDelegate"); 

/** The below way of calling does NOT work .... returns null value.... Please help... 
* I assume that, since I have extended the WIRAdminBaseAction, i should be able to call the getCacheDelegate directly 
* and it should return my cacheDelegate object ... 
* Again, Please note.....if I change my applicationContext_MNC.xml as below, the below way of calling works fine... 
* but, i don't want to change my applicationContext_MNC.xml as below, due to some necessity. 

<bean id="BMOWIRAdminBaseAction" class="com.symcor.wiradmin.web.action.WIRAdminBase Action"> 
<property name="cacheDelegate"> 
<ref bean="cacheDelegate" /> 
</property> 
</bean> 

<bean id="cacheDelegate" class="com.symcor.wiradmin.delegate.CacheDelegate" > 
<property name="statusDBDAO"><ref bean="BMOStatusDBDAO" /></property> 
</bean> 

... is it that the name and bean should have the same value.... ??? No Need to be.....Am i right ? Please advise. 
* 
* **/ 


getCacheDelegate().getActorAction(1); // this way of calling doesn't work and returns null value. please help. 

return "success"; 
} 
} 


WIRAdminBaseAction.java 

package com.symcor.wiradmin.web.action; 


import java.util.Map; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.apache.struts2.interceptor.ParameterAware; 
import org.apache.struts2.interceptor.SessionAware; 

import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.Preparable; 
import com.opensymphony.xwork2.config.entities.Parameteri zable; 
import com.symcor.wiradmin.delegate.ICacheDelegate; 

public class WIRAdminBaseAction extends ActionSupport implements Preparable, ParameterAware, Parameterizable, SessionAware,RequestAware { 

private HttpServletRequest request; 
private static final long serialVersionUID = 1L; 
private HttpServletResponse response; 

private ICacheDelegate cacheDelegate; 

private Map session; 

private Map<String, String> params; 

private Map parameters; 

public void prepare() throws Exception { 
} 

public String execute() throws Exception { 
return SUCCESS; 
} 

public void setServletRequest(HttpServletRequest request) { 

this.request = request; 
} 

public HttpServletRequest getServletRequest() { 
return this.request; 
} 

public void setServletResponse(HttpServletResponse response) { 
this.response = response; 
} 

public HttpServletResponse getServletResponse() { 
return this.response; 
} 

public ICacheDelegate getCacheDelegate() { 
return cacheDelegate; 
} 

public void setCacheDelegate(ICacheDelegate cacheDelegate) { 
this.cacheDelegate = cacheDelegate; 
} 

public void addParam(final String key, final String value) { 
this.params.put(key, value); 
} 

public Map getParams() { 
return params; 
} 

public void setParams(final Map<String, String> params) { 
this.params = params; 
} 

public Map getSession() { 
return this.session; 
} 

public void setSession(final Map session) { 
this.session = session; 
} 

public void setParameters(final Map param) { 
this.parameters = param; 
} 

} 


applicationContext_MNC.xml 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans> 

<!-- database configuration from property file --> 
<bean id="BMOAdminDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
destroy-method="close" lazy-init="default" autowire="default" dependency-check="default"> 
<property name="driverClass" value="${jdbc.driver}" ></property> 
<property name="jdbcUrl" value="${admin.jdbc.url}" ></property> 
<property name="user" value="${admin.jdbc.user}" ></property> 
<property name="password" value="${admin.jdbc.password}" ></property> 
<property name="initialPoolSize" value="3" ></property> 
<property name="minPoolSize" value="3" ></property> 
<property name="maxPoolSize" value="25" ></property> 
<property name="acquireIncrement" value="1" ></property> 
<property name="acquireRetryDelay" value="1000" ></property> 
<property name="debugUnreturnedConnectionStackTraces" value="true" ></property> 
<property name="maxIdleTime" value="300" ></property> 
<property name="unreturnedConnectionTimeout" value="300000" ></property> 
<property name="preferredTestQuery" value="SELECT COUNT(*) FROM LOCALE_CODE" ></property> 
<property name="checkoutTimeout" value="300000" ></property> 
<property name="idleConnectionTestPeriod" value="600000" ></property> 
</bean> 

<bean id="BMOWIRAdminBaseAction" class="com.symcor.wiradmin.web.action.WIRAdminBase Action"> 
<property name="cacheDelegate"> 
<ref bean="BMOCacheDelegate" /> 
</property> 
</bean> 

<bean id="BMOCacheDelegate" class="com.symcor.wiradmin.delegate.CacheDelegate" > 
<property name="statusDBDAO"><ref bean="BMOStatusDBDAO" /></property> 
</bean> 

<bean id="BMOStatusDBDAO" class="com.symcor.wiradmin.dao.StatusDBDAO"> 
<property name="dataSource"> 
<ref bean="BMOAdminDataSource" /> 
</property> 
</bean> 

<!-- this bean is set to map the constants which needs to be configured as per 
the environment to the java constants file --> 
<bean id="envConstantsConfigbean" class="com.symcor.wiradmin.util.constants.Environm entConstantsSetter"> 
<property name="loginUrl" value="${login.url}"/> 
<property name="logoutIR" value="${logout.from.image.retrieval}"/> 
<property name="adminModuleUrl" value="${admin.url}"/> 
<property name="adminUrlSym" value="${admin.url.sym}"/> 
<property name="envProperty" value="${env.property}"/> 
</bean> 

</beans> 

0 个答案:

没有答案