简体   繁体   English

没有XML的Spring4-web.xml-转换为Java

[英]Spring4 without XML - web.xml - translate to Java

I do have couple of JNDI resources in my web.xml 我的web.xml中确实有几个JNDI资源

This is for reading a external property file and path depends on environment. 这用于读取外部属性文件,并且路径取决于环境。

<resource-ref id="resourceRef">
    <res-ref-name>config/file</res-ref-name>
    <res-type>java.lang.String</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

This is for JNDI JDBC DB definition 这用于JNDI JDBC DB定义

    <resource-ref id="MyDatabase">
    <description></description>
    <res-ref-name>jdbc/MyDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>     

and this for old ejb (EJB2.x) 这是旧的ejb(EJB2.x)

<ejb-ref id="service">
    <description />
    <ejb-ref-name>ejb/AgencyService</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <home>ejb.Home</home>
    <remote>ejb.Service</remote>
</ejb-ref>

How can translate these resources to Java (Servlet 3.x)? 如何将这些资源转换为Java(Servlet 3.x)? An existing application is migrating from servlet 2.5 and EJB 2.x to Servlet3, Rest and EJB3. 现有的应用程序正在从Servlet 2.5和EJB 2.x迁移到Servlet3,Rest和EJB3。 My new application uses Spring4 (MVC, Security etc) and OpenJPA 2.1.x. 我的新应用程序使用Spring4(MVC,安全性等)和OpenJPA2.1.x。 Our design goal is not to use "xml" (no web.xml, SpringContext.xml etc). 我们的设计目标是不使用“ xml”(不使用web.xml,SpringContext.xml等)。

I created Java class that extends AbstractAnnotationConfigDispatcherServletInitializer 我创建了extends AbstractAnnotationConfigDispatcherServletInitializer Java类

that would be equivalent to Web.xml. 相当于Web.xml。

How can I migrate resource-ref and ejb-ref to Java class?? 如何将resource-refejb-ref迁移到Java类?

Much appreciated your help! 非常感谢您的帮助!

Note :- edited 注意:-已编辑

Basically Spring and JEE stack are "competing" with each other. 基本上,Spring和JEE堆栈彼此“竞争”。 From what you've stated: EJB3 + JPA clearly belong to JEE stack, on the other hand SpringMVC is obviously from Spring universe + spring boot is obviously in a Spring camp. 从您所说的来看:EJB3 + JPA显然属于JEE堆栈,另一方面,SpringMVC显然来自Spring领域,而spring boot显然处于Spring阵营中。

Now, web.xml is kind of a bootstrap point for regular spring application (excluding spring boot of course). 现在,web.xml是常规spring应用程序的引导点(当然不包括spring boot)。 So, in general you should specify a web listener that will load up the spring. 因此,通常应该指定一个Web侦听器,该侦听器将加载弹簧。 When you've tagged a spring boot in the question everything has mixed up. 当您在问题中标记了弹簧靴时,一切都变得混乱起来。 In two words, spring boot indeed alows to configure everything with annotation but it still "assumes" you're using spring under the hood. 简而言之,spring boot确实允许配置带有注释的所有内容,但它仍然“假定”您在后台使用spring。

Regarding the resources, If you are working with DAO layer, you should define beans for those, but bottom line they will be managed by spring then. 关于资源,如果您使用的是DAO层,则应为它们定义bean,但是最重要的是,它们将在Spring之前进行管理。 But in this case there is no need for EJB. 但是在这种情况下,不需要EJB。 Of course you can still use JPA out of spring DAO (people tend to use Hibernate directly IMO this day if they really need ORM, and Hibernate implements JPA these days) if you wish, but in any case its again will be used by spring. 当然,如果您愿意,您仍然可以在春季DAO之后使用JPA(如果人们确实需要ORM,今天他们通常会直接在IMO上使用Hibernate,而现在这些天Hibernate实现JPA),但是无论如何在春季之前都会使用它。

On the other hand if you need a JEE stack I doubt spring boot can really fit you. 另一方面,如果您需要JEE堆栈,我怀疑弹簧靴是否真的适合您。 JEE stack is a kind of "all-in-one" bundle which is supported by various application servers, and spring from this point of view is "take parts and combine by yourself" solution. JEE堆栈是一种由各种应用程序服务器支持的“多合一”捆绑软件,从这个角度出发,JEE堆栈是“自行组装并合并”解决方案。 So for spring project you can use tomcat/jetty which are not by any means fully compliant JEE servers. 因此,对于春季项目,您可以使用tomcat / jetty,但它们绝不完全兼容JEE服务器。 BTW this is exactly what spring boot does. 顺便说一句,这正是Spring Boot的功能。

So I think for the sake of the project you should decide in which camp do you want to be, JEE or Spring. 因此,我认为为了这个项目,您应该确定您想成为哪个营地(JEE或Spring)。 Both technologies look appealing these days, each of them has its benefits/drawbacks, but bottom line it boils down to: 如今,这两种技术都具有吸引力,每种技术都有其优点/缺点,但归根结底可以归结为:

  • Spring is more flexible and innovative, you get changes faster Spring更加灵活和创新,您可以更快地进行更改
  • Spring is more complicated to bootstrap (although if there are seasoned developers familiar with spring, it should not be an issue) 引导Spring更为复杂(尽管如果有经验丰富的开发人员熟悉Spring,那也不应该成为问题)
  • JEE is standardized JEE是标准化的

Use Spring Boot - no need for xml there. 使用Spring Boot-那里不需要xml。 Spring Boot embeds Tomcat (or optionally Jetty), and so does not support EJB. Spring Boot嵌入了Tomcat(或可选的Jetty),因此不支持EJB。

I'd rewrite the EJB' 's functionality as a "plain old Java" class, using spring features where needed. 我将在需要时使用spring功能将EJB的功能重写为“普通的Java”类。

For the external config file, Spring has several ways of providing configuration options at runtime / external to the deployment, so no need for JNDI. 对于外部配置文件,Spring有几种在运行时/部署外部提供配置选项的方式,因此不需要JNDI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM