在Weblogic中进行部署时,努力使JPA与Spring配合使用

时间:2018-09-16 16:54:38

标签: java spring hibernate jpa weblogic12c

我已经使用spring-boot-starter-data-jpa用Spring Boot和JPA创建了POC,我的解决方案运行得很好。当我尝试向使用 Spring 4 hibernate JPA 2.1 版本的旧项目中添加相同的解决方案时,就会出现我的问题。当我在weblogic中部署战争时,遇到的第一个错误是无法自动装配存储库,因此我添加了@EnableJpaRepositories。我什至尝试添加spring.datasource.jndi-name=EXAMPLE_Data_Source,其中EXAMPLE_Data_Source是我的weblogic数据源,但是没有运气。

应用程序类:

@Configuration
@EnableWs
@ComponentScan("com.example.package")
@EnableJpaRepositories(basePackages = {"com.example.repository","com.example.entity"})
public class AppConfig extends WsConfigurerAdapter { //WsConfigurerAdapter  is spring-ws-core 2.2.0 Release

@Autowire
MyRepository  repository; 

存储库类:

public interface MyRepository extends JpaRepository<EntityClass, EntityPKey> {

    @Query("select c from EntityClass c where ID = :param1 and  TYPE = :param2 and :param3 between FROM_DATE and TO_DATE ")
    EntityClass entityClass(@Param("param1") int param1, @Param("param2")
            char param2, @Param("param3") Date param3);
}

现在我得到以下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean  named 'entityManagerFactory' is defined" weblogic.application.ModuleException: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean   named 'entityManagerFactory' is defined
    at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140)
    at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:233)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:228)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
    Truncated. see log file for complete stacktrace
Caused By: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    Truncated. see log file for complete stacktrace

我必须让它处理在weblogic中部署的旧的Spring项目。

问题1:这种解决我的问题的方法是否适用于Spring框架或仅适用于Springboot?

问题2:我缺少哪些配置?

问题3:请给我指出一个可行的示例。我正在努力使这项工作。

Spring webmvc和spring-tx版本是4.0.6

请在您的说明中更加详细。我还很初级。

谢谢。

2 个答案:

答案 0 :(得分:0)

您必须配置Under Beans

DriverManagerDataSource
LocalContainerEntityManagerFactoryBean
JpaTransactionManager

refer这个例子

答案 1 :(得分:0)

我使用的是基于注释的配置,所以经过2天的使用以下配置,我解决了该问题:

@Bean
public DataSource jndiDataSource() {
    final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);

    return dsLookup.getDataSource(dataSourceName); // where dataSourceName is your weblogic data source
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(jndiDataSource());
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
    factoryBean.setPackagesToScan("com.example.entity");
    return factoryBean;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setShowSql(false);
    hibernateJpaVendorAdapter.setGenerateDdl(true);
    hibernateJpaVendorAdapter.setDatabase(Database.ORACLE);
    return hibernateJpaVendorAdapter;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory);
    return txManager;
}