Results 1 to 2 of 2

Thread: XML Beans Fully in XML, Stereotyped beans fully in Annotations

  1. #1
    Join Date
    Jan 2011
    Posts
    4

    Default XML Beans Fully in XML, Stereotyped beans fully in Annotations

    Let me explain a little about my design choices before I get to the problem because then it will make more sense. I like having an entire bean definition in one location so that I can view it in a glance. I use stereotyped beans only for specific categories of objects such as DAOs, MVC Controllers, BOs, etc. because the bean definitions are repetitive and it is easier to look up the bean wiring information by finding the java source file rather than digging through a 2000 line xml file. All of my stereotyped beans are autowired and fully defined using annotations and I never refer to them by ID and so it is always easy to track them down just by class name alone. The rest of my beans are defined in xml because I like having them in a central location in an easy to read format. Any bean with an explicit ID is defined in the xml so I can easily find it with a simple text search. These design guidelines have worked great but I have run into problems keeping the xml bean definitions completely in the xml...

    The problem is when I have a bean defined in xml that needs a stereotyped bean injected in it. Currently, when an xml bean has a dependency on a stereotyped bean, I put an @Autowired annotation on the field in the java source file. This violates my above design guidelines since the bean defined in xml but has some wiring information in the java source file. Because of this, when looking at beans in my xml file, I always have to wonder if there might be additional wiring configuration elsewhere and so always have to look at both the xml definition and the java source to be sure that I see the entire configuration.

    So what I would really like to do is autowire a single property by type in the xml so that I don't have to use the @Autowired annoations for these particular beans. From what I can tell, the only way to autowire in xml is to do it on a bean by bean basis, autowiring all the properties for a bean, rather than selecting individual properties to autowire.

    I messed around with bean factories hoping I could have a generic bean factory find a bean by class and then just use this everywhere in the xml, supplying the class name I want to "autowire". But I ran into problems with this method.

    I have thought about creating a namespace extension whereby I have my own property element that I use inside standard spring bean xml definitions. In the handler, I would then set metadata in the bean definition to later be picked up by a bean post processor and autowired that way. I am unsure about a few things with that method though and it seems like it could potentially take a while to implement.

    Does anyone have a good solution to this problem? Is there anything wrong with my design guidelines? Does anyone else use guidelines like this?

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    Bean defining annotations (@Component, @Controller, @Service, @Repository and also JSR's @Resource) let you specify, as an argument, the id with which that bean is to be registered in the container. So just specify the id and then in xml do normal injection by specifying that id. Example:

    Code:
    @Repository("bankAccountDao")
    public class BankAccountDaoImpl implements BankAccountDao {...
    
    
    <bean id="bankAccountService">
        <property name="bankAccountDao" ref="bankAccountDao"/>
    </bean>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •