初次體驗了下EJB的開發 今天整理了下前端
企業中就我本身公司裏面對於EJB的使用作一次切身的體會java
配置數據源,由於Ejb工程 是要部署到jboss的 因此在jboss裏面配置數據源和tomcat裏面多少有些出入mysql
① 配置數據源的信息web
mysql數據庫 spring
你最好在JBoss\server\default\deploy的deploy文件夾下sql
新建一個專門的配置數據源的xml文件 內容以下:數據庫
<?xml version="1.0" encoding="UTF-8"?>、windows
<datasources>後端
<local-tx-datasource>tomcat
<jndi-name>MySqlDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/lxzq</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>用戶名</user-name>
<password>密碼</password>
<min-pool-size>10</min-pool-size>
<max-pool-size>100</max-pool-size>
</local-tx-datasource>
</datasources>
② 在ejb的工程中改如何獲取到MySqlDS數據源
使用了JTA機制來獲取數據源
須要在工程的META-INF文件夾下創建一個名爲persistence的xml文件
內容以下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="ActivityCore"> <!--工程名 -->
<jta-data-source>java:/MySqlDS</jta-data-source><!--MySqlDS是你所配置的數據源的名稱-->
<!--關於數據庫操做信息的配置 -->
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
③一個EJB組件如何給web工程調用(使用到了spring和hibernate)
在分佈式的應用開發中 若是你的工程前端是用的J2EE 後端的邏輯在EJB中作處理的時候
在EJB的開發中都是提供Entity ,Interface ,Impl,Bean 問題的重點是這個Interface怎麼能讓前端的工程調到
一樣的經過配置文件來解決
前端的工程裏面寫上一個資源文件
暫且叫作jndi.properties
內容
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://jboss:1099 指定你的jboss服務器的ip地址 我這裏是在hosts文件中作了域名的映射127.0.0.1 jboss
Hosts文件存在的位置C:\windows\system32\drivers\etc下面
這還只是指定服務器 建立jndi的環境
在工程的src或者它的下級目錄的文件夾下
創建一個xml文件
內容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<!-- 建立JNDI環境 -->
<!--id隨便起的 -->
<util:properties id="log.jndiEnvironment" location="classpath:xx/xx/jndi.properties"/>
<!-- 初始化EJB存根 -->
<jee:jndi-lookup id="EJB的接口標識(起個有意義的名字)"
proxy-interface="xxxx.IAdminxxxService"
<!-- 專門提供給後臺管理使用的Interface -->
jndi-name="AdminXXService/remote"
environment-ref="log.jndiEnvironment"/><!--util的id -->
<jee:jndi-lookup id="EJB的接口標識(起個有意義的名字)"
<!-- 專門提供給網站前端使用的Interface -->
proxy-interface="xxxx.IWebXXService"
jndi-name="WebXXService/remote"
<!--jboss發佈的服務名(你的實現類的類型)/遠程 -->
environment-ref="log.jndiEnvironment"/>
<!-- 依賴注入jndi-->
</beans>