2010(初次接触EJB 小节)

初次体验了下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"/><!--utilid -->

<jee:jndi-lookup id="EJB的接口标识(起个有意义的名字)"

<!-- 专门提供给网站前端使用的Interface -->

 proxy-interface="xxxx.IWebXXService"

 jndi-name="WebXXService/remote" 

<!--jboss发布的服务名(你的实现类的类型)/远程 -->

 environment-ref="log.jndiEnvironment"/>

<!-- 依赖注入jndi-->

</beans>

相关文章
相关标签/搜索