Spring @PostConstruct function in a @Repository called multiple times

Posted by Seth on Stack Overflow See other posts from Stack Overflow or by Seth
Published on 2010-04-09T22:59:09Z Indexed on 2010/04/09 23:03 UTC
Read the original article Hit count: 459

I have a DAO that I'm trying to inject into a couple different places:

@Repository
public class FooDAO
{
    @Autowired
    private HibernateManager sessionFactory;

    @PostConstruct
    public void doSomeDatabaseStuff() throws DataAccessException
    {
        ...
    }
}

And my application-context.xml is a fairly simple context:component-scan:

<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.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-init-method="init" default-destroy-method="destroy">

     <context:component-scan base-package="top.level"/>
</beans>

The DAO is accessed from a couple servlets in my application server through @Autowired properties. As far as I understand, anything annotated with @Repository should default to being a singleton and thus doSomeDatabaseStuff() should only be called once (as is my intention). The problem is that I'm seeing doSomeDatabaseStuff() called multiple times.

What's going on here? Have I set something up incorrectly? I'm using spring 3.0.0.

Thanks for the help.

© Stack Overflow or respective owner

Related posts about java

Related posts about spring