Announcement Announcement Module
Collapse
No announcement yet.
Coding to Interfaces using Autowire Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Coding to Interfaces using Autowire

    Is there a way to use the "coding to interfaces" convention with Autowiring? For example, class foobar implements foo and bar. If we want to access foobar via foo, what will be the autowiring procedure? If we want to access the class foobar via the interface bar, what will be the procedure? This is especially required since controlling visibility on a class via interfaces was the basic purpose of interfaces and I'm finding it hard to do this with Autowiring. Thanks alot in advance,

  • #2
    1. Spring reference documentation is quite clear about autowiring contract - 3.3.5. Autowiring collaborators, 3.11.2. @Autowired. Feel free to check the mentioned sections for autowiring problems resolution facilities;
    2. Why did you post the question at the AOP forum? It is a pure 'Core Container' question;

    Comment


    • #3
      Are we mistaken?

      I think we're into a serious issue here then. I read this
      "Please also note that it is not currently possible to autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties). (This is by-design and should be considered a feature.)".... Does this mean one cannot Autowire a class? For example, can't one autowire a class which handles persistence?(If it is not a bean)....

      The next issue is, can we autowire interfaces, or is it actually wrong to autowire other primitives except beans? I'm a bit new to spring as I'm have been learning only ejb... Thanks!

      Comment


      • #4
        Originally posted by ravindranathakila View Post
        I think we're into a serious issue here then. I read this
        "Please also note that it is not currently possible to autowire so-called simple properties such as primitives, Strings, and Classes....
        Term 'Classes' here means objects of class java.lang.Class


        Originally posted by ravindranathakila View Post

        ...
        The next issue is, can we autowire interfaces, or is it actually wrong to autowire other primitives except beans? I'm a bit new to spring as I'm have been learning only ejb... Thanks!
        Have you tried to use spring autowiring for the properties which static type is interface? I assume that you haven't. So, it's high time to do that.

        Comment


        • #5
          Thank You

          It seems you are just giving me links and tries. If I had to try with things I would have done it already. I thought a quicker response would be much faster to learn it. Now I see why spring is much loosely documented than EJB 3+. Having to experiment something is not good enough for any framework. They should explain clearly how to go about a design pattern. I'm much of an experimenter but I didn't think an enterprise solution like spring would need it. Thank you for your attention which was of no use in the end.

          Comment


          • #6
            Just as curiosity - have you tried any of the examples in the reference documentation related to auto-wiring ? You expect someone else to try it for you and someone else to be the "experimenter".

            spring is much loosely documented than EJB 3+
            when you already know something very well, is easy to say that something you don't know is hard to learn and the thing you know is very easy .

            Just "experiment" some examples yourself and, if you don't succeed, come back with some specific questions!

            And, btw, have you used google and searched the forum before asking ? (or the answer should have just popped up as soon as you opened the browser ?)

            Comment


            • #7
              Spring does not support coding to interfaces through autowiring does it? Looks like they overlooked an important feature! I have told I am new to "spring" above. But nevertheless, googling could not dig out any articles about springs autowiring for interfaces which would come quite handy. Thanks.

              Comment


              • #8
                I do not really understand what you want...
                Is it something as simple as this?

                Code:
                public interface Foo {}
                
                public interface Bar{}
                
                @Component
                public class FooBar implements Foo,Bar {}
                
                @Autowired
                public void setFoo(Foo foo) {
                    // You will get you FooBar here accessible through Foo interface
                }

                Comment


                • #9
                  This is what I want through Autowiring

                  1 package test;
                  2
                  3 public class FooBar implements Foo,Bar{
                  4
                  5 public void foos() {
                  6 System.out.println("\tAccessible only by Foo interface!");
                  7 }
                  8
                  9 public void bars() {
                  10 System.out.println("\tAccessible only by Bar interface!");
                  11 }
                  12
                  13 }
                  14

                  1 package test;
                  2
                  3 interface Foo {
                  4 public void foos();
                  5 }
                  6

                  1 package test;
                  2
                  3 interface Bar {
                  4 public void bars();
                  5 }
                  6

                  1 package test;
                  2
                  3 public class FooBarMain {
                  4 public static void main(String args[]){
                  5 Foo foo = new FooBar();
                  6 foo.foos();
                  7 //foo.bars(); This is not possible
                  8
                  9 Bar bar = new FooBar();
                  10 bar.bars();
                  11 //bar.foos(); This is not possible
                  12 }
                  13 }
                  14


                  So, can one inject an object via an interface, using @Autowire? (as here in
                  Foo foo = new FooBar();
                  Bar bar = new FooBar(); )

                  Comment


                  • #10
                    Of course, isnīt that what my example above does?
                    If you create an instance of FooBar it will be autowired using the method setFoo(Foo).

                    Code:
                    public interface Foo {}
                    
                    public interface Bar{}
                    
                    @Component
                    public class FooBar implements Foo,Bar {}
                    
                    @Autowired
                    public class DependsOnFoo {
                        public void setFoo(Foo foo) {
                            // You will get you FooBar here accessible through Foo interface
                       }
                    }
                    I may be missing the point, but this is what spring does...

                    Comment


                    • #11
                      I got partly what your code means, but could you kindly give me a more clearer explanation please? I'd be very grateful. We usually annotate the class right? if then, how do we set an injected object to behave as in the interface?

                      Comment


                      • #12
                        Sorry, the @Autowired annotation should of course be on the method
                        and you want the @Component on both classes. In your application context you need <context:component-scan package="packagename"/>

                        You can also instansiate
                        both classes explicitly in the application context instead of annotating
                        with @Component and the @Autowired will work.

                        The instance of FooBar will be autowired as type Foo if you do it
                        as in the example below.

                        Code:
                        public interface Foo {}
                        
                        public interface Bar{}
                        
                        @Component
                        public class FooBar implements Foo,Bar {}
                        
                        @Component
                        public class DependsOnFoo {
                        
                             private Foo foo;
                            
                            @Autowired
                            public void setFoo(Foo fooBar) {
                                // You will get you FooBar here accessible through Foo interface
                               this.fooBar = foo;
                           }
                        }

                        Comment

                        Working...
                        X