Results 1 to 2 of 2

Thread: Annotations for context-aware operations and init/destroy

  1. #1

    Default Annotations for context-aware operations and init/destroy

    I love Autowired and Component. My apps tend to be on the smaller so these suffice for 99% of my use cases and I directly instantiate and operate on spring managed beans.

    I need for my latest requirement to be able to programmatically access the context this bean is part of, and operate on other beans in the context. I see you can implement BeanFactoryAware and then use that, but I was wondering if there annotations for context-level items. Also does the same thing exist for init and destroy so I don't have to drop down to xml for that?

    Code:
    public class ContextOperator {
    
       // either of these would be awesome
       @BeanFactory
       private BeanFactory factory;
     
       @ApplicationContext
       private ApplicationContext context;
    
       @Init
       public void load() {
       }
    
       @Destroy
       public void close() {
       }
    
       public void doStuff() {
         context.getBean("adsf") etc...
       }
    }
    That seems like it would be a little more friendly in spring terms - you still are "tying yourself" to spring, but I don't care about that. I used to be very xml-oriented, but the annotations are really are so useful I've completely forgotten about any hesitation to include them in the code.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    How about reading the reference guide, that gives the answers to all the questions you ask here...

    If you want the BeanFactory simply put in @Autowired on a BeanFactory property inside your bean that should inject it.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

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