EJB 企業級應用的架構(3)





WebService






WebService是一個SOA(面向服務的編程)的架構,它是不依賴於語言,不依賴於平臺,可以實現不同的語言間的相互調用,通過Internet進行基於Http協議的網絡應用間的交互。






WebService實現不同語言間的調用,是依託於一個標準,webservice是需要遵守WSDL(web服務定義語言)/SOAP(簡單請求協議)規範的。






WebService=WSDL+SOAP+UDDI(webservice的註冊)






Soap是由Soap的part和0個或多個附件組成,一般只有part,在part中有Envelope和Body






EJB中使用WebService






@WebService(serviceName="",portName=""),使用這個標註可以將SessionBean中用@WebMethod標註來表示的方法發佈成WebService






@Stateless


@WebService(serviceName="Greeter",portName="GreeterPost")


public class HelloSessionBean implements HelloSessionRemote {


    @WebMethod


    public String hello(String name) {


        return "Hello world "+name+"!";


    }


}






soap(Service的請求者)






<?xml version="1.0" encoding="UTF-8"?>


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://ejb/">


<soapenv:Body>


<ns1:hello>


<arg0>li</arg0>


</ns1:hello>


</soapenv:Body>


</soapenv:Envelope>






<?xml version="1.0" encoding="UTF-8"?>


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://ejb/">


<soapenv:Body>


<ns1:helloResponse>


<return>Hello world li!</return>


</ns1:helloResponse>


</soapenv:Body>


</soapenv:Envelope>














UDDI (註冊Service)


WSDL (Service的提供者)






<?xml version="1.0" encoding="UTF-8" ?> 


<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ejb/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://ejb/" name="Greeter">


  <types>


  <xsd:schema>


  <xsd:import namespace="http://ejb/" schemaLocation="http://tarena- 59db236e:8088/Greeter/HelloSessionBean/__container$publishing$subctx/META- INF/wsdl/Greeter_schema1.xsd" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" /> 


  </xsd:schema>


  </types>


  <message name="hello">


  <part name="parameters" element="tns:hello" /> 


  </message>


  <message name="helloResponse">


  <part name="parameters" element="tns:helloResponse" /> 


  </message>


  <portType name="HelloSessionBean">


  <operation name="hello">


  <input message="tns:hello" /> 


  <output message="tns:helloResponse" /> 


  </operation>


  </portType>


  <binding name="GreeterPostBinding" type="tns:HelloSessionBean">


  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 


  <operation name="hello">


  <soap:operation soapAction="" /> 


  <input>


  <soap:body use="literal" /> 


  </input>


  <output>


  <soap:body use="literal" /> 


  </output>


  </operation>


  </binding>


  <service name="Greeter">


  <port name="GreeterPost" binding="tns:GreeterPostBinding">


  <soap:address location="http://tarena- 59db236e:8088/Greeter/HelloSessionBean" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" /> 


  </port>


  </service>


</definitions>






實體編程






EJB3.0的JPA(Java持久化API)






O/R Mapping(對象關係映射)


TopLink,JDO,Hibernate


類型對應表,屬性對應字段,關係對應引用


BO(商業對象,操作數據對象)


DO(數據對象)


持久化的數據對象,也就是已將對象信息同步到數據庫中的對象,持久化對象也叫實體。


操作實體也就使操作實體在數據庫中所對應的數據。


實體和SessionBean的區別


實體本身不支持遠程訪問,他的生命週期是比較長的。


實體有唯一性標識,也就對應數據庫表中的主鍵。


注意:在實體中不要寫商業方法






實體的唯一標識,可以使用標籤@Id(標識屬性可以使用public描述,也可以完全封裝爲其提供set,get方法),也可以使用XML文件來進行配置。






@Entity(name="Account"),實體類標註,其屬性name是指定實體名,在EJB-QL中使用,默認是類的全名


@Id,指定實體的唯一標識屬性,默認這個屬性會合數據庫中對應表的主鍵對應。


@GeneratedValue(strategy = GenerationType.AUTO)指定主鍵的生成策略。


@Colum(name="...",unique="true|false",nullable="true|false",insertable="true|false",


updateable="true|false",table="..."),


指定類中屬性對應的列名以及約束,


name屬性指定類中屬性對應的列名,默認爲屬性名


unique屬性指定類中屬性對應的列是否唯一,默認爲false


nullable屬性指定類中屬性對應的列是否可空,默認爲true


insertable="true|false"屬性指定類中該屬性是否會出現在insert語句中,也就是會不會被同步到數據庫,默認爲true,也就數會同步到數據庫


updateable="true|false"屬性指定類中該屬性是否會出現在update語句中,也就是會不會被修改,默認爲true可以被修改。


table屬性指定類中屬性的列所對應的表,默認爲實體類所對應的表。






在使用實體同步到數據庫時,SessionBean中要寫EntityManager類型的屬性,這個屬性在Bean部署在容器中後,在運行時會容器依賴注入,如果沒有容器也可以使用,但需要爲其賦值。


EntityManager是一個接口,也就是規則,可以有不同的實現,Hibernate3.2就實現了這些JPA的接口。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章