bean属性的名称和参考 - 不同的值 - 空指针异常。(bean property name an

2019-10-18 21:00发布

所有。 现附上所有文件。 如果我给了同名字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"); 

调用不起作用以下方式....返回空值....请帮助...我认为,既然我已经扩展了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> 

难道名称和豆应该有同样的价值.... ??? 没必要.....是吗? 请指教。

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> 
文章来源: bean property name and reference - different values - null pointer exception.