当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

Jboss EJB 3.0--Stateless Beans


Jboss EJB 3.0



Stateless Beans




 


Calculator.java             // 计算器的接口:加、减



package org.jboss.tutorial.stateless.bean;




 


public interface Calculator



{



   int add(int x, int y);




 


   int subtract(int x, int y);



}




 



 


CalculatorBean.java



package org.jboss.tutorial.stateless.bean;




 


import javax.ejb.Stateless;




 


@Stateless           //标记CalculatorBean 为 stateless bean,为本地和远程接口的实现



public class CalculatorBean implements CalculatorRemote, CalculatorLocal



{



   public int add(int x, int y)



   {



      return x + y;



   }




 


   public int subtract(int x, int y)



   {



      return x - y;



   }



}




 



 


CalculatorLocal.java



package org.jboss.tutorial.stateless.bean;




 


import javax.ejb.Local;




 


@Local                    //标记为本地接口,继承了计算器接口



public interface CalculatorLocal extends Calculator



{



}




 


CalculatorRemote.java



package org.jboss.tutorial.stateless.bean;




 


import javax.ejb.Remote;




 


@Remote               //标记为远程接口,继承了计算器接口



public interface CalculatorRemote extends Calculator



{



}




 


客户端




 


Client.java



package org.jboss.tutorial.stateless.client;




 


import org.jboss.tutorial.stateless.bean.Calculator;



import org.jboss.tutorial.stateless.bean.CalculatorRemote;




 


import javax.naming.InitialContext;




 


public class Client



{



   public static void main(String[] args) throws Exception



   {



      InitialContext ctx = new InitialContext();



      //查找远程接口



      Calculator calculator = (Calculator) ctx.lookup(CalculatorRemote.class.getName());



                           



      System.out.println("1 + 1 = " + calculator.add(1, 1));



      System.out.println("1 - 1 = " + calculator.subtract(1, 1));



   }



}




 



 



 



 



 



 



 


*****************************简化版********************************




 


只需三个程序即可实现StatelessBean功能。



Calculator.java



package org.jboss.tutorial.stateless.bean;




 


import javax.ejb.Remote;




 


@Remote             //直接将计算器标记为远程接口



public interface Calculator



{



   int add(int x, int y);




 


   int subtract(int x, int y);



}




 



 



 


CalculatorBean.java



package org.jboss.tutorial.stateless.bean;




 


import javax.ejb.Stateless;




 


@Stateless



public class CalculatorBean implements Calculator        //这里实现了计算器接口



{



   public int add(int x, int y)



   {



      return x + y;



   }




 


   public int subtract(int x, int y)



   {



      return x - y;



   }



}




 



 


客户端:




 


Client.java



package org.jboss.tutorial.stateless.client;




 


import org.jboss.tutorial.stateless.bean.Calculator;



import javax.naming.InitialContext;




 


public class Client



{



   public static void main(String[] args) throws Exception



   {



      InitialContext ctx = new InitialContext();



      Calculator calculator = (Calculator) ctx.lookup(Calculator.class.getName());



                                     //只要查找标记为remote的接口就行了



      System.out.println("1 + 1 = " + calculator.add(1, 1));



      System.out.println("1 - 1 = " + calculator.subtract(1, 1));



   }



}




 



 


这里附上log4j.properties 在jboss-EJB-3.0_Preview_5.zip 里面没有这个老是显示缺少appender




 


log4j.properties



log4j.appender.R=org.apache.log4j.RollingFileAppender




 


log4j.appender.R.File=record.log




 


log4j.appender.R.layout=org.apache.log4j.PatternLayout




 


log4j.appender.R.layout.ConversionPattern=%p  %d{hh:mm:ss} %t %c{1} -%m%n




 


log4j.appender.R.MaxBackupIndex=1




 


log4j.appender.R.MaxFileSize=100KB




 


log4j.appender.stdout.layout=org.apache.log4j.PatternLayout




 


log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n




 


log4j.appender.stdout=org.apache.log4j.ConsoleAppender




 


log4j.rootLogger=stdout,R




 



 



 


运行:参考installing.html



Windows下



打开命令提示符cmd,到  jboss_home/bin



 Run.bat –c all



用ant



先build后run 就行了。




 



 


讨论:



简化版是看到jboss-EJB-3.0_Preview_5.zip里面的statefull源码而改写的,在EJB spe 3.0说好像是删除了home这个接口,我觉得简化很多接口更方面,有效的开发j2ee application


相关内容
赞助商链接