Spring实现JNDI 报错了,请高手指教

james513215275 2009-08-27 01:28:34
因为书上只提供了部分代码,我也没弄大明白什么意思,按照自己的理解写了个类
这个是按书上写的例子,这个是配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="systemStartTime" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/systemStartTime"></property>
</bean>
</beans>




package bean;

import java.rmi.Remote;
import java.rmi.registry.LocateRegistry;
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringStart {
private static void bindJndi()throws Exception{
LocateRegistry.createRegistry(1099);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sum.jndi.rml.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
class RemoteDate extends Date implements Remote{};
InitialContext initialContext=new InitialContext();
initialContext.bind("java:comp/env/systemStartTime", new RemoteDate());
initialContext.close();
}
public static void main(String args[]) throws Exception{
ApplicationContext beanFactory = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println(beanFactory.getBean("systemStartTime"));
}
}


然后在执行ApplicationContext beanFactory = new ClassPathXmlApplicationContext(
"applicationContext.xml");方法初始化文件的时候报错

错误信息如下




log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'systemStartTime' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:123)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:85)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:121)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:146)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:93)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:197)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:184)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1198)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1167)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:427)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:285)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
at bean.SpringStart.main(SpringStart.java:24)
...全文
450 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingkangxu 2009-08-28
  • 打赏
  • 举报
回复
修改一下:
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sum.jndi.rml.registry.RegistryContextFactory");

这个类的包名中的rml是不是应该是rmi,我是从你帖子上copy的.
qingkangxu 2009-08-27
  • 打赏
  • 举报
回复
1楼的说法是不对的,不一定要在页面,单独的main也可以用jndi,只不过需要做一些配置和必要的jar而已。
jndi server已经运行着,客户端只是去链接一些而已。

第一步:
代码
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sum.jndi.rml.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");


改为
    Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sum.jndi.rml.registry.RegistryContextFactory");
env.put(Context.PROVIDER_URL, "rmi://localhost:1099");


第二步:
然后
InitialContext initialContext=new InitialContext(); 

改为
InitialContext initialContext=new InitialContext(env); 


就ok了

XZ_QIN 2009-08-27
  • 打赏
  • 举报
回复
算了。等LX的高手吧。呵。我也不明白了
james513215275 2009-08-27
  • 打赏
  • 举报
回复
哦,还是不大明白啊
XZ_QIN 2009-08-27
  • 打赏
  • 举报
回复
它说要指定class的运行环境,也就是你直界运行这个文件是没有经过tomcat这些容器的。二JNDI必须要通过容器才能初始化你的InitialContext 这个对象。反正我也就只知道这么多了。
如果你代码没问题。就放到页面然后启动服务器,访问页面。这样context才能被容器初始化..
james513215275 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xz_qin 的回复:]
就算你写对了。也运行不了。单独运行着个文件是不行的,JNDI必须要经过容器,你要把这个放到页面试试.否则不能初始化
[/Quote]

哦,不大明白,那你能告诉我大致什么意思吗?就是书上想告诉我们什么?
XZ_QIN 2009-08-27
  • 打赏
  • 举报
回复
就算你写对了。也运行不了。单独运行着个文件是不行的,JNDI必须要经过容器,你要把这个放到页面试试.否则不能初始化

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧