Five ‘s Weblog

October 29, 2007

Autowire in Spring

Filed under: Spring — by powerdream5 @ 8:32 pm
Tags: , , , ,

         At the beginning, let’s talk about the function of autowire in spring briefly. In order to save the programmers’ time and effort in writing the spring configuration files by hand, Spring framework introduces the autowire function. To be honest, this function is pretty useful sometimes, but I don’t recommend to use it, since it always bring some problems confusing you. In the following, I would like to show you an example to exmplain why I avert autowire.

10_29_2_2007.jpg

        <beans default-autowire=”autodetect”> <!–autowire is the cause of exception–>
                <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>
                       ……
                </bean>
                <bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
                      ……
                </bean>
                     …….
               <bean id=”cronReportTrigger” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
                     ……
               </bean>
               <bean id=”schedulerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
                         <property name=”triggers”>
                               <list>
                                       <ref bean=”cronReportTrigger”/>
                               </list>
                         </property>
               </bean>
         </beans>

        In my example, firstly, I configure Hibernate. In order to execute a method in a specific time, I configure Quartz. Both of the two configuration processes are so easy that you will not come across any obstacles. However, if you set the autowire properties, when you start the server, you would get the exception: “Failure obtaining db row lock: Table ‘hibernate.qrtz_locks’ doesn’t exist“. I believe you must be confused, and you would ask why the SchedulerFactoryBean class has relationship with Hibernate.

        In fact, this exception is caused by the autowire property of Spring. The org.springframework.scheduling.quartz.SchedulerFactoryBean has a method named setDataSource. Because we set the autowire properiy as “autodetect”, and we also configure database source in our application, the spring container will inject this database source into the SchedulerFactoryBean class automatically, then SchedulerFactoryBean class will looking the database for tasks. There is no task in your database, therefore, you get the exception. Fortunately, as soon as you realize the cause of the exception, it is easy to handle it by removing the autowire property. Then, restart your server, things goes well.

         From this example, we can see that the autowire property may bring some problems over our expectation. Therefore, use this property cautiously!

10_29_1_20071.jpg

Advertisements

7 Comments »

  1. Thnxs powerDream!!I was also trying to fix this issue.You helped me in a right time.

    Comment by Super — June 30, 2008 @ 2:31 am |Reply

  2. I just can’t thank you enough for this post. After a of debugging and googling your post resolved my problem. Thanks very much.

    Comment by Rishi — June 24, 2010 @ 6:38 am |Reply

  3. Thanks a lot! As Rishi and 1 week later, you’ve provided the right answer for us! THANKS!

    Comment by Gonzalo — July 2, 2010 @ 2:17 pm |Reply

  4. There’s no need to abandon default-autowire altogether. In this specific case, you can set the ‘dataSource’ property of the ‘schedulerFactoryBean’ to null, as follows:

    This works fine even with default-autowire still kept in place.

    Comment by Shriniwas — April 5, 2011 @ 2:17 am |Reply

  5. It took out my config example snippet. Here it is once again:
    <property name=”dataSource”><null /></property>

    Comment by Shriniwas — April 5, 2011 @ 2:19 am |Reply

  6. Man, you saved my ass… what a life-saver article.

    Comment by me — May 27, 2011 @ 8:48 pm |Reply

  7. Zenmed Rosacea Reviews You can see the difference in each picture I now have no redness, no more spots and my skin is less greasy. See this stuff really works. So thank you again Zenmed you are my savior!

    Comment by Zenmed Rosacea Reviews — June 14, 2012 @ 10:48 pm |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: