问题是这样的:在客户端提用EJB sessionBean 时,不管用lookup(JNDI) 还是在Spring配置文件里注入.都可以通过JNDI 把这个SessionBean 查出来.跑Junit test 也跑得通.但是用Spring 去调时就会出现把这个SessionBean 转换为它的接口类时出现转型错误:
ERROR [[springapp]] Servlet.service() for servlet springapp threw exception
java.lang.ClassCastException: $Proxy161代码如下:::::::::
接口类
public interface IAccountService{
/**
* Add a new account.
* @param account
* @return Account
*/
public Account add(Account account);
/**
* Edit a exits account.
* @param account
* @return Account
*/
public Account edit(Account account);
/**
* Find a account by id.
* @param id
* @return Account
*/
public Account findById(Integer id);
/**
* Find a account by username.
* @param username
* @return Account
*/
public Account findByUsername(String username);
/**
* Check account is vaild.
* @param username
* @param password
* @return Boolean
*/
public Boolean checkAccount(String username, String password);
/**
* Define the flexible selector for query.
* @param selector
* @return List<Account>
*/
public List findAll(RowRangeSelector selector);
/**
* Use the default selector for query, need provide current page.
* @param currentPage
* @return List<Account>
*/
public List findAll(int currentPage);
}
SessionBean ::
@Remote(IAccountService.class)
@Stateless(name = "AccountServiceBean")
@RemoteBinding(jndiBinding = "cims/AccountServiceBean/remote")
public class AccountServiceBean implements IAccountService { public static final String JNDI = "cims/AccountServiceBean/remote"; private static Log log = LogFactory.getLog(AccountServiceBean.class);
@EJB(beanName = "AccountService")
private IAccountService accountService; public Account add(Account account) {
if (log.isInfoEnabled()) {
log.info("cn.com.symbio.cims.service.bean.AccountServiceBean: add");
} return accountService.add(account);
} public Boolean checkAccount(String username, String password) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: checkAccount");
} return accountService.checkAccount(username, password);
} public Account edit(Account account) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: edit");
} return accountService.edit(account);
} public Account findById(Integer id) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: findAccountById");
} return accountService.findById(id);
} public Account findByUsername(String username) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: findByUsername");
}
return accountService.findByUsername(username);
} public List findAll(RowRangeSelector selector) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: findAll");
} return accountService.findAll(selector);
} public List findAll(int currentPage) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: findAll");
} return accountService.findAll(currentPage);
}
}
通过Context lookup(JNDI):::::::::::public class GetServices { private IAccountService accountServic; private IConsumerService consumerService; protected Context context; public IAccountService getAccountServic() {
try {
Object obj = this.getContext().lookup(
"cims/AccountServiceBean/remote");
System.out.println("-----------" + obj.getClass().getName());
System.out.println("============" + obj);
System.out.println("777777777" + (obj instanceof EJBObject));
accountServic = (IAccountService)obj;
accountServic.lookConsumer(); // Object ref =
// getContext().lookup("cims/AccountServiceBean/remote");
// accountServic = (IAccountService)
// PortableRemoteObject.narrow(ref,
// IAccountService.class);
} catch (NamingException e) {
e.printStackTrace();
}
return accountServic;
}
在这里ojb是我想要的SessionBean 就是在accountServic = (IAccountService)obj;这里出现转型错误. 很郁闷的是,Junit test 这里不会报错.就是在Spring 里要报错. 用的Spring2.0.6 ,JBoss-4.2.0.GA JDK1.5.0.8请高手赐教呀!!!!!!!!!!!
ERROR [[springapp]] Servlet.service() for servlet springapp threw exception
java.lang.ClassCastException: $Proxy161代码如下:::::::::
接口类
public interface IAccountService{
/**
* Add a new account.
* @param account
* @return Account
*/
public Account add(Account account);
/**
* Edit a exits account.
* @param account
* @return Account
*/
public Account edit(Account account);
/**
* Find a account by id.
* @param id
* @return Account
*/
public Account findById(Integer id);
/**
* Find a account by username.
* @param username
* @return Account
*/
public Account findByUsername(String username);
/**
* Check account is vaild.
* @param username
* @param password
* @return Boolean
*/
public Boolean checkAccount(String username, String password);
/**
* Define the flexible selector for query.
* @param selector
* @return List<Account>
*/
public List findAll(RowRangeSelector selector);
/**
* Use the default selector for query, need provide current page.
* @param currentPage
* @return List<Account>
*/
public List findAll(int currentPage);
}
SessionBean ::
@Remote(IAccountService.class)
@Stateless(name = "AccountServiceBean")
@RemoteBinding(jndiBinding = "cims/AccountServiceBean/remote")
public class AccountServiceBean implements IAccountService { public static final String JNDI = "cims/AccountServiceBean/remote"; private static Log log = LogFactory.getLog(AccountServiceBean.class);
@EJB(beanName = "AccountService")
private IAccountService accountService; public Account add(Account account) {
if (log.isInfoEnabled()) {
log.info("cn.com.symbio.cims.service.bean.AccountServiceBean: add");
} return accountService.add(account);
} public Boolean checkAccount(String username, String password) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: checkAccount");
} return accountService.checkAccount(username, password);
} public Account edit(Account account) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: edit");
} return accountService.edit(account);
} public Account findById(Integer id) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: findAccountById");
} return accountService.findById(id);
} public Account findByUsername(String username) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: findByUsername");
}
return accountService.findByUsername(username);
} public List findAll(RowRangeSelector selector) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: findAll");
} return accountService.findAll(selector);
} public List findAll(int currentPage) {
if (log.isInfoEnabled()) {
log
.info("cn.com.symbio.cims.service.bean.AccountServiceBean: findAll");
} return accountService.findAll(currentPage);
}
}
通过Context lookup(JNDI):::::::::::public class GetServices { private IAccountService accountServic; private IConsumerService consumerService; protected Context context; public IAccountService getAccountServic() {
try {
Object obj = this.getContext().lookup(
"cims/AccountServiceBean/remote");
System.out.println("-----------" + obj.getClass().getName());
System.out.println("============" + obj);
System.out.println("777777777" + (obj instanceof EJBObject));
accountServic = (IAccountService)obj;
accountServic.lookConsumer(); // Object ref =
// getContext().lookup("cims/AccountServiceBean/remote");
// accountServic = (IAccountService)
// PortableRemoteObject.narrow(ref,
// IAccountService.class);
} catch (NamingException e) {
e.printStackTrace();
}
return accountServic;
}
在这里ojb是我想要的SessionBean 就是在accountServic = (IAccountService)obj;这里出现转型错误. 很郁闷的是,Junit test 这里不会报错.就是在Spring 里要报错. 用的Spring2.0.6 ,JBoss-4.2.0.GA JDK1.5.0.8请高手赐教呀!!!!!!!!!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货