`
yangzb
  • 浏览: 3494657 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring 2.5.1 + EJB3

阅读更多

I've gotten Spring 2.5.1 + EJB3 to work on both JBoss and Glassfish. Though, I've also at times run into issues similar to the above (see my thread on this).

Like you, I also placed spring.jar into ${JBOSS_HOME}/server/default/lib.

Here's my current (trivial) setup that works on JBoss 4.2.0.GA (minus the import statements).

My bean interface:

Code:
package foo.ejb;

public interface Service {

  public String getVersion();

}

My Bean implementation:

Code:
package foo.ejb;

@Stateless(name = "service")
public class ServiceBean implements Service {

  @Autowired
  private Delegate delegate;

  public String getVersion() {
    return delegate.getVersion();
  }

}

Delegate is a dependency of ServiceBean :

Code:
package foo.ejb;

public class Delegate {
  public String getVersion() {
    return "1.0";
  }
}

My beanRefContext.xml

:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg type="java.lang.String" value="ejbApplicationContext.xml" />
  </bean>
  
</beans>

And my ejbApplicationContext.xml :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean id="delegate" class="foo.ejb.Delegate" />
  
</beans>

And finally, to configure Spring to inject into my EJBs, I used Spring 2.5.1's SpringBeanAutowiringInterceptor . Here's my /META-INF/ejb-jar.xml

:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
  <interceptors>
    <interceptor>
      <interceptor-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</interceptor-class>      
    </interceptor>    
  </interceptors>
  <assembly-descriptor>
    <interceptor-binding>
      <ejb-name>*</ejb-name>
      <interceptor-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</interceptor-class>
    </interceptor-binding>
  </assembly-descriptor>
</ejb-jar>

That all goes into foo-ejb.jar .

Next the stuff that goes into foo-web.war , starting with my controller:

Code:
package foo.web;

@Controller
public class IndexController {

  /**
   * Or "service/remote" on JBoss. For Glassfish, remove the suffix.
   */
  @EJB(mappedName = "service/local")
  private Service service;
  
  @RequestMapping("/index.html")
  public void index(ModelMap model) {
    model.addAttribute("version", service.getVersion());
  }

}

The Freemarker template that comprises the view for the above:

Code:
<html>
<body>
<p>Version: ${version}</p>
</body>
</html>

Now, an important point : I had problems on both Glassfish and JBoss getting the JNDI lookup correct because of prefixes and suffixes. The most relevant part of my foo-servlet.xml shows how I avoid the "java:comp/env" prefix:

Code:
<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <!--
    Use a custom JNDI bean factory configured not to prepend lookups with "java:comp/env"
  -->
  <bean id="jndiFactory"
    class="org.springframework.jndi.support.SimpleJndiBeanFactory">
    <property name="resourceRef" value="false" />
  </bean>

  <!--
    Configure the CommonAnnotationBeanPostProcessor to always use JNDI lookup (for EJBs)
    and use the custom JNDI bean factory above.
  -->
  <bean
    class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
    <property name="alwaysUseJndiLookup" value="true" />
    <property name="jndiFactory" ref="jndiFactory" />
  </bean>

  <!--
    Since we're turning off "annotation-config" in the context:component-scan below, we need
    to explicitly configure an AutowiredAnnotationBeanPostProcessor.
  -->
  <bean
    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

  <!--
    Configure annotation-based component scanning, instantiation and dependency injection. 
  -->
  <context:component-scan base-package="com.talikos.ese.web.controllers" annotation-config="false" />

  <!--
    Rest of Spring MVC (view resolver, etc.) configuration goes here.
  -->

</beans>

Hope this (lengthy post) helps. I really should blog this.

分享到:
评论

相关推荐

    Struts2.0+Springframework2.5+ibatis2.3完美整合用户登录及增删改查

    本演示示例主要使用目前最新,最流行技术Struts2.1 +Spring 2.5.1+ibatis2.3整合开发而成,这与我以前发布的版本中最大特色是整合了Spring2.5.1中的注解功能和半自动化工具ibatis技术,这是本示例的两大特色,简化了配置...

    cxf(jax-ws)+spring+hibernate整合包

    wsn-core-2.7.6.jar,cxf-xjc-boolean-2.6.2.jar,cxf-xjc-bug671-2.6.2.jar,cxf-xjc-dv-2.6.2.jar,cxf-xjc-runtime-2.6.2.jar,cxf-xjc-ts-2.6.2.jar,dom4j-1.6.1.jar,ehcache-1.2.3.jar,ehcache-core-2.5.1.jar,ejb3-...

    Spring攻略PDF版

    因为上传大小的限制,分为两部分上传,这是第二部分,第一部分会在评论中给出链接 ... 第17章 Spring对EJB和JMS的支持   第18章 Spring对JMX、电子邮件和调度的支持   第19章 Spring中的脚本编程 

    Spring攻略中文版PDF

    因为上传大小的限制,分为两部分上传,这是第一部分,第二部分会在评论中给出链接 ... 第17章 Spring对EJB和JMS的支持   第18章 Spring对JMX、电子邮件和调度的支持   第19章 Spring中的脚本编程 

    Spring攻略英文版(附带源码)

     第3章 Spring中的Bean配置   3.1 在Spring IoC容器里配置Bean   3.1.1 问题描述   3.1.2 解决方案   3.1.3 实现方法   3.2 实例化Spring IoC容器   3.2.1 问题描述   3.2.2 解决方案   ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6 .3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. 移植到Spring 2.0 ...

    Spring in Action(第二版 中文高清版).part2

    11.3 Spring和EJB3 11.3.1 引入Pitchfork 11.3.2 从Pitchfork起步 11.3.3 通过注释注入资源 11.3.4 使用注释声明拦截器 11.4 小结 第12章 访问企业服务 12.1 从JNDI中获取对象 12.1.1 使用传统的JNDI ...

    Spring in Action(第2版)中文版

    11.3spring和ejb3 11.3.1引入pitchfork 11.3.2从pitchfork起步 11.3.3通过注释注入资源 11.3.4使用注释声明拦截器 11.4小结 第12章访问企业服务 12.1从jndi中获取对象 12.1.1使用传统的jndi 12.1.2注入jndi...

    Spring in Action(第二版 中文高清版).part1

    11.3 Spring和EJB3 11.3.1 引入Pitchfork 11.3.2 从Pitchfork起步 11.3.3 通过注释注入资源 11.3.4 使用注释声明拦截器 11.4 小结 第12章 访问企业服务 12.1 从JNDI中获取对象 12.1.1 使用传统的JNDI ...

    Spring预览

    3. **事务管理**:Spring提供了更为直观且易于使用的事务管理方式,相比于EJB事务管理更为灵活。 4. **组件化**:Spring支持组件化开发,使得代码更加模块化和可维护。 5. **性能优化**:Spring框架的设计考虑了性能...

    Spring攻略(第二版 中文高清版).part1

    第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...

    Spring 2.0 开发参考手册

    2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6.3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. 移植到Spring ...

    Spring攻略(第二版 中文高清版).part2

    第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...

    Spring中文帮助文档

    2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于Annotation的控制器 2.5.4. Spring MVC的表单标签库 2.5.5. 对Tiles 2 支持 2.5.6. 对JSF 1.2支持 2.5.7. JAX-WS支持 2.6. 其他 2.6.1. 动态...

    Spring API

    2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于Annotation的控制器 2.5.4. Spring MVC的表单标签库 2.5.5. 对Tiles 2 支持 2.5.6. 对JSF 1.2支持 2.5.7. JAX-WS支持 2.6. 其他 2.6.1. 动态...

    spring chm文档

    2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6.3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. 移植到Spring ...

    Spring JPA学习

    在容器管理下,`EntityManager`的生命周期由容器(如EJB容器)管理。 **2.5.3.2 应用程序管理(Application-Managed)** 应用程序管理则由应用程序本身负责`EntityManager`的生命周期。 **2.5.3.3 使用方式** 在...

    网上购物系统

    &lt;classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ejb3-persistence.jar"/&gt; &lt;classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate3.jar"/&gt; ...

    Maven权威指南 很精典的学习教程,比ANT更好用

    2.5.1. 用户相关配置和仓库 2.5.2. 升级Maven 2.6. 获得Maven帮助 2.7. 使用Maven Help插件 2.7.1. 描述一个Maven插件 2.8. 关于Apache软件许可证 I. Maven实战 3. 一个简单的Maven项目 3.1. 简介 ...

Global site tag (gtag.js) - Google Analytics