我一直在关注Axon实时编码session,以便用Axon Framework搞定我的手。但是,在尝试启动我的应用程序时遇到了一些问题。 PFB我的代码 -
AxonApplication.java
package com.cognizant.axon;
import static org.axonframework.commandhandling.GenericCommandMessage.asCommandMessage;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.CommandCallback;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.model.GenericJpaRepository;
import org.axonframework.commandhandling.model.Repository;
import org.axonframework.common.jpa.ContainerManagedEntityManagerProvider;
import org.axonframework.common.jpa.EntityManagerProvider;
import org.axonframework.common.transaction.TransactionManager;
import org.axonframework.eventhandling.EventBus;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine;
import org.axonframework.spring.config.EnableAxon;
import org.axonframework.spring.messaging.unitofwork.SpringTransactionManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.PlatformTransactionManager;
import com.cognizant.axon.account.Account;
import com.cognizant.axon.coreapi.CreateAccountCommand;
import com.cognizant.axon.coreapi.WithdrawMoneyCommand;
@EnableAxon
@SpringBootApplication
public class AxonApplication {
public static void main(String[] args) {
final ConfigurableApplicationContext configuration = SpringApplication.run(AxonApplication.class, args);
final CommandBus commandBus = configuration.getBean(CommandBus.class);
commandBus.dispatch(asCommandMessage(new CreateAccountCommand("4321", 500)), new CommandCallback<CreateAccountCommand, Object>() {
@Override
public void onFailure(CommandMessage<? extends CreateAccountCommand> command, Throwable cause) {
cause.printStackTrace();
}
@Override
public void onSuccess(CommandMessage<? extends CreateAccountCommand> command, Object payload) {
System.out.println(payload);
commandBus.dispatch(asCommandMessage(new WithdrawMoneyCommand("4321", 250)));
commandBus.dispatch(asCommandMessage(new WithdrawMoneyCommand("4321", 251)));
}
});
}
@Bean
public EntityManagerProvider entityManagerProvider() {
return new ContainerManagedEntityManagerProvider();
}
@Bean
public Repository<Account> jpaAccountRepository(EventBus eventBus) {
return new GenericJpaRepository<>(entityManagerProvider(), Account.class, eventBus);
}
@Bean
public TransactionManager axonTransactionManager(PlatformTransactionManager transactionManager) {
return new SpringTransactionManager(transactionManager);
}
@Bean
public EventStorageEngine eventStorageEngine() {
return new InMemoryEventStorageEngine();
}
/*@Bean
public CommandBus commandBus() {
return new AsynchronousCommandBus();
}*/
}
Account.java
package com.cognizant.axon.account;
import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.model.AggregateIdentifier;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.spring.stereotype.Aggregate;
import com.cognizant.axon.coreapi.AccountCreatedEvent;
import com.cognizant.axon.coreapi.CreateAccountCommand;
import com.cognizant.axon.coreapi.MoneyWithdrawnEvent;
import com.cognizant.axon.coreapi.WithdrawMoneyCommand;
import lombok.NoArgsConstructor;
@Aggregate(repository="jpaAccountRepository")
@NoArgsConstructor
@Entity
public class Account {
@Id
@AggregateIdentifier
private String accountId;
@Basic
private int balance;
@Basic
private int overdraftLimit;
@CommandHandler
public Account(CreateAccountCommand command) {
apply(new AccountCreatedEvent(command.getAccountId(), command.getOverdraftLimit()));
}
@CommandHandler
public void handle(WithdrawMoneyCommand command) throws OverdraftLimitExceededException {
if (balance + overdraftLimit >= command.getAmount()) {
apply(new MoneyWithdrawnEvent(command.getAccountId(), command.getAmount(), balance - command.getAmount()));
} else {
throw new OverdraftLimitExceededException();
}
}
@EventSourcingHandler
public void on(AccountCreatedEvent event) {
this.accountId = event.getAccountId();
this.overdraftLimit = event.getOverdraftLimit();
}
@EventSourcingHandler
public void on(MoneyWithdrawnEvent event) {
this.balance = event.getBalance();
}
}
OverdraftLimitExceededException.java
package com.cognizant.axon.account;
public class OverdraftLimitExceededException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
}
AccountCreatedEvent.java
package com.cognizant.axon.coreapi;
import javax.validation.constraints.NotNull;
import lombok.Value;
@Value
public class AccountCreatedEvent {
@NotNull
private String accountId;
@NotNull
private int overdraftLimit;
}
CreateAccountCommand.java
package com.cognizant.axon.coreapi;
import javax.validation.constraints.NotNull;
import lombok.Value;
@Value
public class CreateAccountCommand {
@NotNull
private String accountId;
@NotNull
private int overdraftLimit;
}
MoneyWithdrawnEvent.java
package com.cognizant.axon.coreapi;
import javax.validation.constraints.NotNull;
import lombok.Value;
@Value
public class MoneyWithdrawnEvent {
@NotNull
private String accountId;
@NotNull
private int amount;
@NotNull
private int balance;
}
WithdrawMoneyCommand.java
package com.cognizant.axon.coreapi;
import javax.validation.constraints.NotNull;
import org.axonframework.commandhandling.TargetAggregateIdentifier;
import lombok.Value;
@Value
public class WithdrawMoneyCommand {
@NotNull
@TargetAggregateIdentifier
private String accountId;
@NotNull
private int amount;
}
当我启动应用程序时,遇到以下错误 -
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.10.RELEASE)
2018-03-07 12:09:27.484 INFO 4664 --- [ main] com.cognizant.axon.AxonApplication : Starting AxonApplication on EC2AMAZ-MF96EBA with PID 4664 (C:\DeveloperSpace\cosmos-alfresco\axon-demo\target\classes started by Administrator in C:\DeveloperSpace\cosmos-alfresco\axon-demo)
2018-03-07 12:09:27.487 INFO 4664 --- [ main] com.cognizant.axon.AxonApplication : No active profile set, falling back to default profiles: default
2018-03-07 12:09:27.545 INFO 4664 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@28c4711c: startup date [Wed Mar 07 12:09:27 IST 2018]; root of context hierarchy
2018-03-07 12:09:28.667 INFO 4664 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$3ad3d038] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-03-07 12:09:28.706 INFO 4664 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean '__axon-parameter-resolver-factory' of type [org.axonframework.spring.config.ApplicationContextLookupParameterResolverFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-03-07 12:09:28.707 INFO 4664 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean '__axon-parameter-resolver-factory' of type [org.axonframework.messaging.annotation.MultiParameterResolverFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-03-07 12:09:29.133 INFO 4664 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-03-07 12:09:29.145 INFO 4664 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-03-07 12:09:29.146 INFO 4664 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.27
2018-03-07 12:09:29.267 INFO 4664 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-03-07 12:09:29.267 INFO 4664 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1725 ms
2018-03-07 12:09:29.412 INFO 4664 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-03-07 12:09:29.416 INFO 4664 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-07 12:09:29.416 INFO 4664 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-03-07 12:09:29.416 INFO 4664 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-03-07 12:09:29.416 INFO 4664 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-03-07 12:09:29.461 WARN 4664 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerProvider': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
2018-03-07 12:09:29.463 INFO 4664 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-03-07 12:09:29.478 INFO 4664 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-03-07 12:09:29.547 ERROR 4664 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.
Action:
Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.
它与实时会话中显示的完全相同。唯一的区别是我使用了更新版本的Axon框架。任何帮助都将非常感激。
~Arunava