Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:08:30+00:00 2026-05-29T08:08:30+00:00

I want to do something like this: @Stateless public class GreeterEjb { private final

  • 0

I want to do something like this:

@Stateless
public class GreeterEjb {


    private final Greeter greeter;


    @Inject
    public GreeterEjb(Greeter greeter) {
        this.greeter = greeter;
    }


    public String greet() {
        return greeter.greet();
    }
}

I tried it with Glassfish 3.1.1 and JBoss 7.0.2 with mixed results. Under some circumstances it works, under other circumstances it doesn’t.
See this thread in the Glassfisch forum if you are interested in the details.

The EJB 3.1 spec, section 4.9.2 Bean Classes says:

The class must have a public constructor that takes no parameters.

That sounds like constructor injection is not allowed for EJBs.

BUT the CDI spec says at the start of section 3 that Session Beans are supported by CDI. Section 3.2 then talks at length about CDI and EJBs but never mentions anything about constructor injection not working. Which makes me think that it should be allowed.

So, do the specs allow CDI constructor injection for EJBs or not?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:08:31+00:00Added an answer on May 29, 2026 at 8:08 am

    Kris and Pete Muir have finally convinced me: The EJB must have a public no-arg constructor even if another constructor is used for injection. Weird to use two constructors at the same time, but it works. Thanks guys.

    Successfully tested on Glassfish 3.1.1, JBoss 7.0.2 and TomEE 1.0.0-beta-2.

    @Stateless
    public class GreeterEjb {
    
        private final Greeter greeter;
    
    
        @Inject
        public GreeterEjb(Greeter greeter) {
            this.greeter = greeter;
        }
    
    
        // public no-arg constructor required for EJBs
        // injection still works fine with the @Inject constructor
        public GreeterEjb() {
            this.greeter = null;
        }
    
    
        public String greet() {
            return greeter.greet();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want something like this : public class Order { public Guid OrderID {
I want to do something like this: @Stateless @Path(/sensors/{sensorid}/version) @Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML}) @Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML}) public
I want something like this: public abstract class ListenerEx<LISTENER, PARENT> implements LISTENER { PARENT
I want something like this public abstract class abc { public abstract void test();
i have class something like this class MClass { private int mem1,mem2,mem3.......; public int
I want something like this: <msxsl:script language=C#> ??? getNodes() { ... return ... }
I want to do something like this: const MyFirstConstArray: array[0..1] of string = ('Hi',
I want something like this: public void CopyIteratorIntoList(Iterator<Foo> fooIterator) { List<Foo> fooList = new
I want to be able to do something like this in a stateless Session
Basically, I want something like this: Dictionary<object, string> dict = new Dictionary<object, string>(); dict.Add(null,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.