Results 1 to 9 of 9

Thread: Spring Annotation Limitations Vs Spring XML (spring 2.5.6)

  1. #1
    Join Date
    Sep 2011
    Posts
    8

    Lightbulb Spring Annotation Limitations Vs Spring XML (spring 2.5.6)

    HI ,

    I am working on migration of Spring XML to Spring Annotaions , Since we have limitation we have to use featured upto spring 2.5.6

    As per my idea , we cant achieve all things that we do via XML

    Few Limitations with Annotations
    -------------------------------------

    1. cant declare bean (pojo) as abstract , as we do in XML
    2. Factory way of instantiating beans
    3. Lazy loading ( Available ins Spring 3.0.)
    4. Declaring bean as prototype (or) non prototype
    5. Injecting collections to bean



    Let me know if we have any work around of above mentioned issues .

    Could somebody point out link , where i can see spring annotations limitations .

    Thanks in advance

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

    Default

    2. Factory way of instantiating beans
    @Bean is basically a Factory but you can simply return a FactoryBean from the method (or utilize it inside your method).

    4. Declaring bean as prototype (or) non prototype
    That is why there is a @Scope annotation which by default is singleton and you can set it to prototype.

    5. Injecting collections to bean
    AFAIK there shouldn't be any reason why that shouldn't work.
    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

  3. #3
    Join Date
    Sep 2011
    Posts
    8

    Default

    Hi Marten ,

    Thanks for reply .

    Do we have @bean in spring 2.5.6 ? i didnt find it .

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

    Default

    Sorry was ahead of things... @Bean is in java config which was a separate project. (I was mixing 3.0 after I noticed you where using 2.5.6).

    Also with 1 you can simply use a class hierarchy and if you use @Autowired on the abstract superclass it will get dependencies injected. It isn't as powerful as the abstract bean definition of xml but the ti also the drawback of annotations.
    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

  5. #5
    Join Date
    Sep 2011
    Posts
    8

    Default

    With @Autowired it perfectly works with abstract classes ,Thanks for info

    is there way to create the virtual abstract bean

    <bean id="sampleDao" abstract="true">
    <property name="hibernateTemplate" ref="sampleHibernateTemplate" />
    </bean>


    in above XML case ,we really dont have real abstract class in java ,instead we call hibernamteTemplate (aka sampleDao )

    Thanks in advance .

  6. #6
    Join Date
    Sep 2011
    Posts
    8

    Default

    Hi Marten ,

    whats the best way in finding custom annotations using spring framework ? do we need to use some bean "InstantiationAwareBeanPostProcessorAdapter" ?

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

    Default


    is there way to create the virtual abstract bean

    <bean id="sampleDao" abstract="true">
    <property name="hibernateTemplate" ref="sampleHibernateTemplate" />
    </bean>
    in above XML case ,we really dont have real abstract class in java ,instead we call hibernamteTemplate (aka sampleDao )
    When using auto wiring the default is doing it by type so there is no need for an abstract class (virtual or concrete) because spring simply detects the dependencies. So why would you need an abstract class...


    whats the best way in finding custom annotations using spring framework ? do we need to use some bean "InstantiationAwareBeanPostProcessorAdapter" ?
    Well it depends on the annotation, what does it need to do... Does it need to operate on how a bean is constructed then you need a BeanFactoryPostProcessor if you need to inject some dependency you are going to need a BeanPostProcessor. I suggest taking a look at the different implementations spring already provides.
    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

  8. #8
    Join Date
    Sep 2011
    Posts
    8

    Lightbulb

    Again Thanks for ur kind replies .

    so far i am good after implementing your suggestions .

    We create the Hibernate Session factory where we pass more than 500 Domain classes , it occupies many lines which i would liek to avoid instaed of that i am following the below approach

    • Since all our domain classes were already annotated with @Entity Annotation ,I would like to know all fully qualifies list of class annotated with @Entity by scanning all classes in classptah .This class should be called much before spring instantiates Hibernate Session factory

      Storing this list of bean class in spring context ,so that hibernate session factory will refer the list of class which i created in spring contet and i will delete those 500 lines of domain classes hard coded in xml.


    Code i am using to find the list of fully qualified class by scanning classes in classpth

    Code:
    List<String> beansList = new ArrayList<String>();
    		ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
    				true);
    		Long startDate = System.currentTimeMillis();
    		
    		String basePackage = "com.firm.xx.yy.model";
    		provider.addExcludeFilter(new AnnotationTypeFilter(RemoteBinding.class, false));
    		provider.addIncludeFilter(new AnnotationTypeFilter(Entity.class, false));
    		Set<BeanDefinition> filteredComponents = provider
    				.findCandidateComponents(basePackage);
    		System.out.println("No of components :" + filteredComponents.size());
    
    		for (BeanDefinition component : filteredComponents) {
    			System.out.println("Component:" + component.getBeanClassName());
    		}
    
    		provider.resetFilters(true);
    		provider.addIncludeFilter(new AnnotationTypeFilter(Repository.class,
    				true));
    		filteredComponents = provider.findCandidateComponents(basePackage);
    		System.out.println("No of components :" + filteredComponents.size());
    		Long endDate = System.currentTimeMillis();
    		for (BeanDefinition component : filteredComponents) {
    			beansList.add(component.getBeanClassName());
    		}
    some cases above code gives Annotation Error .

    on side note , i think even we can configure <component-scan ../> to scan our custom annotaions , i really dont know who to get the list bean class from that

    Any help would be highly appreciated .

    Thanks in advance
    Gowtham

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

    Default

    I suggest a forum search, there is a class around here in the forums which basically does what you want. If you cannot find it you can always simply create a FactoryBean which returns a list of your entities, which you can then simply wire as internal bean to your session factory.
    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
  •