Announcement Announcement Module
Collapse
No announcement yet.
Using annotations without having Spring search all classes? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using annotations without having Spring search all classes?

    Is there a way to tell spring about the classes I would like it to look at the annotations for without using XML or having it search the classloader? I would like to get the performance of XML based spring ACs without having to use XML for the configuration of the beans themselves.

    Is there a middle ground somewhere?

    I am using Spring 2.5.5 and currently have an applicationContext.xml like so currently:
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <!-- NOTE: I am not convinced this is a good idea -->
        <context:annotation-config />
        <context:component-scan base-package="org.dspace" name-generator="org.dspace.servicemanager.spring.FullPathBeanNameGenerator" />
    </bean>

  • #2
    There is no way to scan components on the class names basis. You can check backing implementation of the <context:component-scan/> config switch and implement your own element at the same way.

    However, are you sure that it's really worth to do? You said that you want 'to get the performance of XML based spring ACs' but context initialization is an inevitable application initialization phase and you may assume that it may take a while to process. Classpath scanning for the filtered packages doesn't seem to provide a significant startup time increase. So, from my point of view it's better to resolve that tasks with the greater business-value.

    Comment


    • #3
      I mean that I would like to avoid putting in this line in the xml file:
      Code:
      <context:component-scan base-package="org.dspace" name-generator="org.dspace.servicemanager.spring.FullPathBeanNameGenerator" />
      But still want a way to identify the classes that should be my beans without having to list them all in the xml file.

      Comment


      • #4
        How do you want to say Spring to search the beans then?

        Comment


        • #5
          I don't want it to search. I want to specify the beans myself but without using XML. I would like to specify them in code in some central file maybe.

          Comment


          • #6
            Originally posted by azeckoski View Post
            I don't want it to search. I want to specify the beans myself but without using XML. I would like to specify them in code in some central file maybe.
            I really don't understand the point to avoid xml with single element in comparison with manual bean definitions. Spring doesn't offer such a facility, however, there are another java IoC frameworks you can look at - Guice, PicoContainer

            Comment


            • #7
              Thanks. We are already using those, we just wanted to be able to use spring in a similar way.
              I guess you are saying that this would not meet our needs then?
              http://www.springframework.org/javaconfig

              Comment


              • #8
                Haven't worked with JavaConfig

                I looked through the tutorial and that project seems to be able to provide desired functionality.

                Thanks for mentioning the project, will check its facilities.

                Comment


                • #9
                  Originally posted by azeckoski View Post
                  Thanks. We are already using those, we just wanted to be able to use spring in a similar way.
                  I guess you are saying that this would not meet our needs then?
                  http://www.springframework.org/javaconfig
                  What is your underlying reason for not wanting a component scan to take place ? Have you ascertained how long doing a component scan takes compared to doing it manually via XML defined beans ? I've just run a check on a project and the component scan for 6 beans took 85 milliseconds, whereas XML beans in same project took 79 milliseconds for 5, so more or less the same amount of time per bean.

                  Comment

                  Working...
                  X