Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 2010-2011. Axon Framework
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.axonframework.eventsourcing;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;

Implementation of the AggregateSnapshotter that eases the configuration when used within a Spring Application Context. It will automatically detect a Transaction Manager and Aggregate Factories.

The only mandatory properties to set is AbstractSnapshotter.setEventStore(org.axonframework.eventstore.SnapshotEventStore). In most cases, you should also provide an executor, as the default will execute snapshotter tasks in the calling thread.

Allard Buijze
         implements InitializingBeanApplicationContextAware {
     private boolean autoDetectAggregateFactories = true;
     protected Runnable createSnapshotterTask(String typeIdentifierAggregateIdentifier aggregateIdentifier) {
         Runnable command = super.createSnapshotterTask(typeIdentifieraggregateIdentifier);
         if ( != null) {
             return new TransactionalRunnableWrapper(command);
         return command;

Sets the transaction manager to manager underlying transaction with. If none is provided, an attempt is made to auto detect is from the application context. If a single transaction manager is found, it is used to manage transactions. Of none or more than one is found, they are ignored.

transactionManager the transaction manager managing underlying transactions
     public void setTransactionManager(PlatformTransactionManager transactionManager) {
         this. = transactionManager;

Optionally sets the transaction definition to use. By default, uses the application context's default transaction semantics (see

transactionDefinition the transaction definition to use
     public void setTransactionDefinition(TransactionDefinition transactionDefinition) {
         this. = transactionDefinition;
     public void afterPropertiesSet() throws Exception {
         if () {
             Set<AggregateFactoryfactoriesFound = new HashSet<AggregateFactory>();
             Collection<EventSourcingRepositoryeventSourcingRepositories =
             for (EventSourcingRepository repo : eventSourcingRepositories) {
             setAggregateFactories(new ArrayList(factoriesFound));
        if ( == null) {
            Map<StringPlatformTransactionManagercandidates = .getBeansOfType(
            if (candidates.size() == 1) {
                this. = candidates.values().iterator().next();

Optionally sets the aggregate factories to use. By default, this implementation will auto detect available factories from the application context. Configuring them using this method will prevent auto detection.

aggregateFactories The list of aggregate factories creating the aggregates to store.
    public void setAggregateFactories(List<AggregateFactory<?>> aggregateFactories) {
        this. = false;
    public void setApplicationContext(ApplicationContext applicationContextthrows BeansException {
        this. = applicationContext;
    private class TransactionalRunnableWrapper implements Runnable {
        private final Runnable command;
        public TransactionalRunnableWrapper(Runnable command) {
            this. = command;
        public void run() {
            try {
                if (transaction.isNewTransaction()) {
            } catch (RuntimeException e) {
                if (transaction.isNewTransaction()) {
                throw e;
New to GrepCode? Check out our FAQ X