一开始,我遇到了这个错误
Caused by: java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@78bd863d: startup date [Thu Sep 17 14:11:50 KST 2015]; root of context hierarchy at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:344) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331) at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.postProcessAfterInitialization(DataSourceInitializedPublisher.java:69) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422) at org.springframework.beans.factory.support.AbstractAutowireCapab @SpringBootApplication @ImportResource( {"classpath:/config/*.xml" } ) @EnableAutoConfiguration(exclude=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class) @ComponentScan @Configuration public class Starter extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Starter.class); } public static void main(String[] args) { SpringApplication.run(Starter.class, args); }
但是我又遇到了另一个错误。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:575) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:531) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:697) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:670)
更新1
添加messgesource bean之后,此错误已经开始。
<bean id="messageSource" class="com.xxx.redca.hub.common.context.support.DatabaseDrivenMessageSource"> <constructor-arg index="0" ref="com.xxx.redca.hub.admin.module.messagemgn.service.MessageService" /> <constructor-arg index="1" ref="com.xxx.redca.hub.admin.language.service.LanguageMgnService" /> <constructor-arg index="2" value="#{comProps['defaultLang']}" /> </bean> public class DatabaseDrivenMessageSource extends AbstractMessageSource implements ResourceLoaderAware{ private Logger logger = LoggerFactory.getLogger(this.getClass()); private ResourceLoader resourceLoader; private final Map<String, Map<String, String>> properties = new HashMap<String, Map<String, String>>(); private String defaultLang; private MessageService messageService; private LanguageMgnService languageMgnService;
更新2
我正在使用jpa和mybatis两种orm。
Orm由spring-data-jpa设置,而mybatis由spring xml设置。
application.properties
spring.datasource.url=jdbc:mysql://70.121.224.51:3306/faro spring.datasource.username=root #spring.datasource.password= spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.data=classpath:/init.dml/TB_FR_LANGUAGE_INFO_INITDATA.sql,classpath:/init.dml/TB_FR_MESSAGE_INFO_INITDATA.sql spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect spring.jpa.database-platform= org.hibernate.dialect.MySQL5Dialect spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = create spring.view.prefix=/WEB-INF/pages/ spring.view.suffix=.jsp
XML配置
文件太多了。但是我附加了似乎相关的文件。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <util:properties id="props" location="classpath:/properties/mysql.datasource.properties" /> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://70.121.224.51:3306/faro" /> <property name="username" value="root" /> <property name="password" value="" /> <property name="logStatementsEnabled" value="true" /> </bean> <bean id="messageSource" class="com.xxx.redca.hub.common.context.support.DatabaseDrivenMessageSource"> <constructor-arg index="0" ref="com.xxx.redca.hub.admin.module.messagemgn.service.MessageService" /> <constructor-arg index="1" ref="com.xxx.redca.hub.admin.language.service.LanguageMgnService" /> <constructor-arg index="2" value="#{comProps['defaultLang']}" /> </bean>
更新3
我的pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.1.6.RELEASE</spring.version> <spring.security.version>3.2.3.RELEASE</spring.security.version> <jacoco.version>0.7.2.201409121644</jacoco.version> <jacoco.reportPath>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.reportPath> <system.score.path>${project.basedir}/lib</system.score.path> <querydsl.version>4.0.1</querydsl.version> <java.version>1.7</java.version> <java.home>C:/Program Files/Java/jdk1.7.0_51</java.home> <!-- <java.home>C:/Program Files/Java/jdk1.8.0_40</java.home> --> <!-- <datasource.type>h2</datasource.type> --> </properties> <dependencies> <!-- <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> --> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.1.Final</version> </dependency> --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-data</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>4.0.2.RELEASE</version> </dependency> <!-- for aui --> <dependency> <groupId>com.anyframe</groupId> <artifactId>anyframe-ui-spring</artifactId> <version>1.0.4</version> </dependency> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>lego.core</groupId> <artifactId>lego-core</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>system</scope> <systemPath>${system.score.path}/lego-core-0.0.1-SNAPSHOT.jar</systemPath> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.12-beta1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.12-beta1</version> </dependency> <dependency> <groupId>org.anyframe</groupId> <artifactId>anyframe-core</artifactId> <version>1.6.0</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.security.version}</version> </dependency> <!-- mysinge api --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-mysingle</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${system.score.path}/axis-mysingle-1.0.0.jar</systemPath> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery-mysingle</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${system.score.path}/commons-discovery-mysingle-1.0.0.jar</systemPath> </dependency> <dependency> <groupId>ESBMailService</groupId> <artifactId>ESBMailService</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${system.score.path}/ESBMailService.jar</systemPath> </dependency> <dependency> <groupId>ESBService_EMP_ORG_MAIL</groupId> <artifactId>ESBService_EMP_ORG_MAIL.jar</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${system.score.path}/ESBService_EMP_ORG_MAIL.jar</systemPath> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>jaxrpc-mysingle</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${system.score.path}/jaxrpc-mysingle-1.0.0.jar</systemPath> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${system.score.path}/mail-1.0.0.jar</systemPath> </dependency> <dependency> <groupId>javax.xml.soap</groupId> <artifactId>saaj-mysingle</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${system.score.path}/saaj-mysingle-1.0.0.jar</systemPath> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j-mysingle</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${system.score.path}/wsdl4j-mysingle-1.0.0.jar</systemPath> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>bouncycastle</groupId> <artifactId>bouncycastle-jce-jdk13</artifactId> <version>112</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>aui-fileuploader</groupId> <artifactId>aui-fileuploader</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.10.4</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.10.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-sqlmap</artifactId> <version>2.3.4.726</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.8.0.RELEASE</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <version>${querydsl.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-jpa</artifactId> <version>${querydsl.version}</version> </dependency> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-sql</artifactId> <version>${querydsl.version}</version> </dependency> <dependency> <groupId>net.wimpi</groupId> <artifactId>telnetd-x</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-cypher-compiler-2.1</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> </dependency> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>com.mysema.maven</groupId> <artifactId>apt-maven-plugin</artifactId> <version>1.1.3</version> <executions> <execution> <goals> <goal>process</goal> </goals> <configuration> <outputDirectory>target/generated-sources/</outputDirectory> <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor> </configuration> </execution> </executions> </plugin> </plugins> </build>
更新4
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.xxx.redca.hub.admin.language.dao.LanguageMgnDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6f2735cc: startup date [Thu Sep 17 17:53:23 KST 2015]; root of context hierarchy at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:357) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ... 42 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6f2735cc: startup date [Thu Sep 17 17:53:23 KST 2015]; root of context hierarchy at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:116) at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1517) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:314) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:565) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:531) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:697) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:670) at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:354) ... 53 more Caused by: java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6f2735cc: startup date [Thu Sep 17 17:53:23 KST 2015]; root of context hierarchy at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:344) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331) at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.postProcessAfterInitialization(DataSourceInitializedPublisher.java:69) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1719) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:113) ... 63 more
班级
@Repository("com.xxx.redca.hub.admin.language.dao.LanguageMgnDao") public class LanguageMgnDaoImpl implements LanguageMgnDao{ @Autowired private LegoSqlTemplate sqlTemplate; @Autowired private SequenceIdGenerator generator; private final static String sqlStatement="LanguageMgn."; // jpa repo @PersistenceContext private EntityManager em; //@Autowired //EntityManagerFactory emf; JPAQueryFactory queryFactory; @PostConstruct void init() { //System.out.println("=============================>>>>LanguageMgnDaoImpl "+em); //System.out.println("=============================>>>>LanguageMgnDaoImpl "+emf); //em = emf.createEntityManager(); queryFactory = new JPAQueryFactory(em);
您可以尝试将其包装MessageSource在一个懒惰的代理中,这会延迟Bean的创建直到实际使用为止。
MessageSource
<bean id="messageSource" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="targetSource"> <bean class="org.springframework.aop.target.LazyInitTargetSource"> <property name="targetClass" value="com.xxx.redca.hub.common.context.support.DatabaseDrivenMessageSource" /> <property name="targetBeanName" value="lazyMessageSource"/> </bean> </property> </bean> <bean id="lazyMessageSource" class="com.xxx.redca.hub.common.context.support.DatabaseDrivenMessageSource" lazy-init="true"> <constructor-arg index="0" ref="com.xxx.redca.hub.admin.module.messagemgn.service.MessageService" /> <constructor-arg index="1" ref="com.xxx.redca.hub.admin.language.service.LanguageMgnService" /> <constructor-arg index="2" value="#{comProps['defaultLang']}" /> </bean>
现在,在启动时,您将获得代理,并立即将其用于实例化并使用它来检索消息。这应该足够延迟。