4 Replies Latest reply on Apr 24, 2014 9:24 AM by sixtdeu

    Private bean constructor?

    sixtdeu Newbie

      Hi folks!

       

      In the CDI 1.1 spec I read:

       

      {quote}

      3.9 Bean constructors [bean_constructors]

      When the container instantiates a bean class, it calls the bean constructor. The bean constructor is a default-access, public,

      protected or private constructor of the bean class.

      {quote}

       

      I am understanding something wrong, or why I get an exception on the Server when i add a default private constructor to the bean?

       

      Best!

       

      sixt

        • 1. Re: Private bean constructor?
          Martin Kouba Master

          Hi sixtdeu,

          please add some more info, e.g. what exception you get. A code snippet with your bean would be helpful as well. Also what application server and weld version do you use?

          • 2. Re: Private bean constructor?
            sixtdeu Newbie

            Hi Martin,

             

            the WAR cannot be deployt to the server (in my case WildFly). Just try to add an private default constructor to any CDI bean with scope other than dependent, inject it into an other bean and then deploy your application. Does it work?

             

            E.g.

            @RequestScoped

            public class RequestScopedService

            {

              private RequestScopedService() {

              }

            }

             

             

            org.jboss.weld.exceptions.DeploymentException: WELD-001410: The injection point [BackedAnnotatedField] @Inject private ....ScopePresenter2.requestScopedService has non-proxyable dependencies...

            (...)

            Caused by: org.jboss.weld.exceptions.UnproxyableResolutionException: WELD-001436: Normal scoped bean class ....RequestScopedService is not proxyable because it has a private constructor private ....RequestScopedService() - Managed Bean [class ....RequestScopedService] with qualifiers [@Any @Default].

             

            It is clear for me, that a private constructor could be a problem, when the container trys to create the proxy, but why there is this in the spec:

             

            3.9 Bean constructors [bean_constructors]

            When the container instantiates a bean class, it calls the bean constructor. The bean constructor is a default-access, public,

            protected or private constructor of the bean class.

             

            It cannot be that I'm the only one who tried it out with a private ctor?

            • 3. Re: Private bean constructor?
              Martin Kouba Master
              • 4. Re: Private bean constructor?
                sixtdeu Newbie

                Oh yes, now I see... The first 3.15 says is

                 

                "Certain legal bean types cannot be proxied by the container:

                classes which don’t have a non-private constructor with no parameters,"

                 

                Thanks!