我有一个基于弹簧的jar程序,它从我的机器运行良好。但目标是将其作为企业应用程序包装起来" ear"并将其部署到weblogic。已经完成了以下工作,如上所述" jar"作为java程序从我的本地机器运行良好。当同一个id部署到weblogic时,它抱怨Datasource Autowiring。
根据http://javahonk.com/create-maven-ear-project-eclipse/
创建了ear项目上的 StartupListener 会引发错误
applicationContext.register(AppConfig.class);
以下是结构和一些代码片段
Main_Project
|
|- ear_module
|
|- src/main/java
|
|- StartupListener.java
|
|- pom.xml
|
|- jar_module
|
|- java clases
|
|- pom.xml
|
|- war_module
|
|- src/main/java
|
|- WebCongig.java
|
|- Servlet.java
|
|- pom.xml
|
|-pom.xml
StartupListener.java
applicationContext = new AnnotationConfigApplicationContext();
applicationContext.getEnvironment().setActiveProfiles("war");
applicationContext.register(AppConfig.class);
applicationContext.refresh();
App app = applicationContext.getBean(App.class);
app.run();
jar中的AppConfig.java
@Autowired
Environment env;
@Autowired
DataSource dataSource;
@Bean(destroyMethod="")
@Profile("test")
public DataSource getDataSource() {
LOG.info("Initializing DataSource...");
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(env.getProperty("driver"));
ds.setUrl(env.getProperty("url"));
ds.setUsername(env.getProperty("user"));
ds.setPassword(env.getProperty("password"));
ds.setInitialSize(2);
LOG.info("DriverManagerDataSource configured: " + ds.getUrl());
dataSource = ds;
return dataSource;
}
@Bean
@Profile("jndi")
public DataSource getJndiDataSource() {
LOG.info("Lookup JNDI DataSource...");
return new JndiDataSourceLookup().getDataSource(env.getProperty("jndi"));
}
在Weblogic中部署时出现异常
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[javax.sql.DataSource]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}" org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[javax.sql.DataSource]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 在com.abc.startup.StartupListener.postStart(StartupListener.java:27) at weblogic.application.internal.flow.BaseLifecycleFlow $ PostStartAction.run(BaseLifecycleFlow.java:309) 在weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) 在weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120) at weblogic.application.utils.SecurityUtils $ WLSSecurityProvider.invokePrivilegedAction(SecurityUtils.java:177) at weblogic.application.utils.SecurityUtils.invokePrivilegedAction(SecurityUtils.java:80) at weblogic.application.internal.flow.BaseLifecycleFlow $ LifecycleListenerAction.invoke(BaseLifecycleFlow.java:200) 在weblogic.application.internal.flow.BaseLifecycleFlow.postStart(BaseLifecycleFlow.java:67) 在weblogic.application.internal.flow.TailLifecycleFlow.activate(TailLifecycleFlow.java:26) 在weblogic.application.internal.BaseDeployment $ 2.next(BaseDeployment.java:729) at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42) 在weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:258) 在weblogic.application.internal.EarDeployment.activate(EarDeployment.java:61) 在weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165) 在weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:80) 在weblogic.deploy.internal.targetserver.operations.AbstractOperation.activate(AbstractOperation.java:587) 在weblogic.deploy.internal.targetserver.operations.ActivateOperation.activateDeployment(ActivateOperation.java:150) 在weblogic.deploy.internal.targetserver.operations.ActivateOperation.doCommit(ActivateOperation.java:116) 在weblogic.deploy.internal.targetserver.operations.AbstractOperation.commit(AbstractOperation.java:339) 在weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentCommit(DeploymentManager.java:846) 在weblogic.deploy.internal.targetserver.DeploymentManager.activateDeploymentList(DeploymentManager.java:1275) 在weblogic.deploy.internal.targetserver.DeploymentManager.handleCommit(DeploymentManager.java:442) 在weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.commit(DeploymentServiceDispatcher.java:176) 在weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doCommitCallback(DeploymentReceiverCallbackDeliverer.java:195) 在weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access $ 100(DeploymentReceiverCallbackDeliverer.java:13) 在weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer $ 2.run(DeploymentReceiverCallbackDeliverer.java:68) at weblogic.work.SelfTuningWorkManagerImpl $ WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:553) 在weblogic.work.ExecuteThread.execute(ExecuteThread.java:311) 在weblogic.work.ExecuteThread.run(ExecuteThread.java:263)