bagerth
bagerth

Reputation: 11

Automatic reload of "database" beans, possible?

Is there somehow possible to create a number of beans holding database data, and when the database is updated the necessary beans (not the entire context!) are automatically updated?

The background is that I recently migrated an old web application to the Spring Framework and Hibernate. Before the migration, when a database table was updated a pre-defined set of singletons where updated. I haven't found a nice and simple solution for this in Spring yet.

/Bagerth

Context:

<beans:bean id="systems" class="." factory-method="createInstance">  
    <beans:constructor-arg ref="customers"/>  
</beans:bean> 

Bean:

public class Systems {
    public static ArrayList<System> createInstance(ArrayList<Customer> customers) {
        ArrayList<System> systems = new ArrayList<System>();
        // code to populate systems ...
        return systems;
    }
}

Upvotes: 1

Views: 216

Answers (1)

px5x2
px5x2

Reputation: 1565

At first glance that approach maybe used:

Note that reload methods are pre-defined methods which do the reload work on its singleton.

@Component
public class MyReloader {

    @Autowired
    AService aService;

    @Autowired
    BService bService;

    @Scheduled(fixedRate = 2 * 1000)
    public void reload() {
        aService.reload();
        bService.reload();
    }

}

Another approach (async) you implement an ApplicationListener and trigger a custom event that forces singlentons reload themselves.

Upvotes: 1

Related Questions