5 Replies Latest reply on Mar 29, 2010 2:51 AM by shin0135

    How to programmatically set a required attribute?

    shin0135

      Hi,


      I have an injected property which has a required attribute false.




      @In(required=false)
      private MyDto dto;
      



      I would like to convert above to like this:




      private MyDto dto = (MyDto)Component.getInstance(MyDto.class);





      How can I set a required attribute programatically?


      Thank you for your help!


        • 1. Re: How to programmatically set a required attribute?
          amitev
          @In(create=true)
          


          creates the component for you.

          • 2. Re: How to programmatically set a required attribute?
            shin0135

            Thanks Adrian for the answer.


            I'm just curious to ask,



            private MyDto dto = (MyDto)Component.getInstance(MyDto.class);



            is the above equivalent to



            @In(required=false)
            private MyDto dto;




            I'm trying to remove @In without losing any benefits of seam bijection provides.


            When I used Component.getInstance, it didn't complain about non-null required of dto. Does this mean both methods are equal to use?


            • 3. Re: How to programmatically set a required attribute?
              amitev

              No, it's not equivalent.


              private MyDto dto = (MyDto)Component.getInstance(MyDto.class);
              



              creates a component if it doesn't exist in the scope


              @In(required=false)
              private MyDto dto;
              


              this just performs a dependency injection

              • 4. Re: How to programmatically set a required attribute?
                kragoth

                James S. wrote on Mar 26, 2010 16:08:


                Hi,

                I have an injected property which has a required attribute false.



                @In(required=false)
                private MyDto dto;
                



                I would like to convert above to like this:



                private MyDto dto = (MyDto)Component.getInstance(MyDto.class);





                How can I set a required attribute programatically?

                Thank you for your help!




                You can't actually set the required attribute programmatically. However, you should get the same behavior by doing this:


                private MyDto dto = (MyDto)Component.getInstance(MyDto.class, false);
                



                What this means is: Inject an instance of MyDto.class if and only if one exits in my current context(s). Doing this is basically the same as setting required=false. The boolean represents the decision to create an instance if one does not exist.

                • 5. Re: How to programmatically set a required attribute?
                  shin0135

                  Thanks for your clarification, Adrian and Tim.


                  Now I understand what I was wrong.


                  Again, thanks for your help!