您的位置:程序门 -> java -> j2ee / ejb / jms



小弟刚学 spring 有点小问题??请前辈指教..


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


小弟刚学 spring 有点小问题??请前辈指教..[已结贴,结贴人:]
发表于:2007-07-17 15:14:55 楼主

public   class   hellobean   {
            private   string   helloword   =   "helloword!! ";
            private   applicationcontext   applicationcontext;

            public   void   sethelloword(string   helloword){
this.helloword   =   helloword;
}
        public   string   gethelloword(){
27行:         applicationcontext.publishevent(new   propertygettedevent   ( "[ "+helloword+ "]   is   getted "));
       
        return   helloword;
        }
        public   void   setapplicationcontext(applicationcontext   context){
        this.applicationcontext   =   context;
        }  
}

public   class   propertygettedevent   extends   applicationevent   {
public   propertygettedevent(object   source){
super(source);;
}
}

public   class   propertygettedlistener   implements   applicationlistener   {
public   void   onapplicationevent(applicationevent   event)   {
        system.out.println(event.getsource().tostring());
}
}

public   class   springtest   {

public   static   void   main(string[]   args){
                applicationcontext   context   =   new   classpathxmlapplicationcontext( "bean.xml ");
               
                hellobean   hello   =   (hellobean)context.getbean( "hellobean ");
35行: system.out.println(hello.gethelloword());

}

}

<beans>  
      <bean   id= "propertygettedlistener "   class= "com.dl.bean.propertygettedlistener "/>
      <bean   id= "hellobean "   class= "com.dl.bean.hellobean ">
            <property   name= "helloword ">
                  <value> helloword!! </value>
            </property>
      </bean>
</beans>


报这个异常   空指针   就是搞不懂  
java.lang.nullpointerexception
at   com.dl.bean.hellobean.gethelloword(hellobean.java:27)
at   com.dl.test.springtest.main(springtest.java:35)
exception   in   thread   "main "
发表于:2007-07-17 17:18:051楼 得分:0
applicationcontext为空
这样改改:
public   static   void   main(string[]   args){
applicationcontext   context   =   new   classpathxmlapplicationcontext( "bean.xml ");

hellobean   hello   =   (hellobean)context.getbean( "hellobean ");
hello.setapplication(context);  
35行:   system.out.println(hello.gethelloword());

}
发表于:2007-07-17 17:24:082楼 得分:0
谢了..可以了..
发表于:2007-07-19 14:13:563楼 得分:0
该回复于2007-12-28 12:01:23被管理员或版主删除


快速检索

最新资讯
热门点击