工厂方法“dataSource”抛出异常;嵌套异常是 javax.naming.NameNotFoundException

Posted

技术标签:

【中文标题】工厂方法“dataSource”抛出异常;嵌套异常是 javax.naming.NameNotFoundException【英文标题】:Factory method 'dataSource' threw exception; nested exception is javax.naming.NameNotFoundException 【发布时间】:2018-06-15 17:56:34 【问题描述】:

我正在尝试使用 JNDI 为我的 demo project 设置休眠 + 弹簧。为此,我选择了Baeldung manual。但是有一个错误(见最后的堆栈跟踪)。任何相同的主题都没有帮助我。任何想法如何解决它?

数据库连接测试

我的步骤:

1在[tomcat_home]/conf/server.xml文件中打开并添加资源

Tomcat server.xml 文件内容

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />

    <Resource   name="jdbc/OctavaDatabase"
                auth="Container"
                type="javax.sql.DataSource"
                driverClassName="org.postgresql.Driver"
                url="jdbc:postgresql://localhost:5432/octavadb"
                username="postgres"
                password="1"
                maxActive="20"
                maxIdle="10"
                maxWait="-1"/>        
  </GlobalNamingResources>

2 放到 [tomcat_home]/lib/ 在这种情况下,PostgreSQL JDBC jar。 Postrges JDBC driver

3 在 [tomcat_home]/conf/context.xml 的元素内定义一个 ResourceLink

tomcat context.xml 文件内容[Resource->name 和 ResourceLink->name 相同]

<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>$catalina.base/conf/web.xml</WatchedResource>

    <ResourceLink
        name="jdbc/OctavaDatabase"
        global="jdbc/OctavaDatabase"
        type="javax.sql.DataSource"/>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
</Context>

4.在 resources\rdbmsDev.properties 文件中指定 jndi 查找 url 属性

rdbmsDev.properties 文件内容

jdbc.url=java:comp/env/jdbc/OctavaDatabase

5 Hibernate配置文件内容

@Configuration
@PropertySource("classpath:rdbmsDev.properties")
public class HibernateConfiguration 

    @Value("$jdbc.url")
    private String jndiLookupUrl;

    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException 
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setPersistenceUnitName("persistenceUnitName");
        emf.setPackagesToScan("rinex.model");
        emf.setJpaVendorAdapter(getJpaVendorAdapter());
        emf.setDataSource(dataSource());
        return emf;
    

    @Bean
    public JpaVendorAdapter getJpaVendorAdapter() 
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setGenerateDdl(false);
        jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
        return jpaVendorAdapter;
    

    @Bean
    public DataSource dataSource() throws NamingException 
        return (DataSource) new JndiTemplate().lookup(jndiLookupUrl);
    

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) 
        return new JpaTransactionManager(entityManagerFactory);
    

数据源初始化调试

仅 pom.xml 休眠依赖项

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.12.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.4.Final</version>
    </dependency>

要查看完整的项目结构,请访问 GitHub 了解该项目

但是我遇到了错误

     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in rinex.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in rinex.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is javax.naming.NameNotFoundException: Name [jdbc/OctavaDatabase] is not bound in this Context. Unable to find [jdbc].
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1249)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1098)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4641)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5105)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:740)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:716)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1729)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in rinex.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is javax.naming.NameNotFoundException: Name [jdbc/OctavaDatabase] is not bound in this Context. Unable to find [jdbc].
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:186)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:575)
    ... 59 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in rinex.config.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is javax.naming.NameNotFoundException: Name [jdbc/OctavaDatabase] is not bound in this Context. Unable to find [jdbc].
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1249)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1098)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:392)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:364)
    at rinex.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$210cba4a.dataSource(<generated>)
    at rinex.config.HibernateConfiguration.entityManagerFactory(HibernateConfiguration.java:31)
    at rinex.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$210cba4a.CGLIB$entityManagerFactory$2(<generated>)
    at rinex.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$210cba4a$$FastClassBySpringCGLIB$$26fea56b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
    at rinex.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$210cba4a.entityManagerFactory(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:155)
    ... 60 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is javax.naming.NameNotFoundException: Name [jdbc/OctavaDatabase] is not bound in this Context. Unable to find [jdbc].
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:186)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:575)
    ... 82 more
Caused by: javax.naming.NameNotFoundException: Name [jdbc/OctavaDatabase] is not bound in this Context. Unable to find [jdbc].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:817)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at org.springframework.jndi.JndiTemplate.lambda$lookup$0(JndiTemplate.java:156)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:91)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:156)
    at rinex.config.HibernateConfiguration.dataSource(HibernateConfiguration.java:46)
    at rinex.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$210cba4a.CGLIB$dataSource$3(<generated>)
    at rinex.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$210cba4a$$FastClassBySpringCGLIB$$26fea56b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
    at rinex.config.HibernateConfiguration$$EnhancerBySpringCGLIB$$210cba4a.dataSource(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:155)
    ... 83 more

【问题讨论】:

【参考方案1】:

我一直试图让它工作大约 3 天,但没有运气。最后,我选择了另一种方法来使用 Jakarta Commons Database Connection Pooling 项目来获取数据库连接池。这样,你不应该更改 tomcat XML 文件,下载 JDBC jar 文件等。所以,我喜欢它!

要使用 Jakarta Commons Database Connection 方法,您应该执行几个简单的步骤。

1.添加依赖

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.1.4</version>
    </dependency>

2.更新/更正属性文件 (rdbms.dev.properties)

jdbc.platform=POSTGRESQL
jdbc.driverClassName=org.postgresql.Driver
jdbc.username=postgres
jdbc.password=1
jdbc.url=jdbc:postgresql://localhost:5432/octavadb
connection.init_size=5
connection.pool_size=10

hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.hbm2ddl.auto=create

3.创建数据源 bean

@Bean(name = "dataSource")
    public BasicDataSource getDataSource() 
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        basicDataSource.setUsername(env.getProperty("jdbc.username"));
        basicDataSource.setPassword(env.getProperty("jdbc.password"));
        basicDataSource.setUrl(env.getProperty("jdbc.url"));
        basicDataSource.setInitialSize(Ints.tryParse(env.getProperty("connection.init_size")));
        basicDataSource.setMaxIdle(Ints.tryParse(env.getProperty("connection.pool_size")));
        return basicDataSource;
    

更多详情

AppInitializer.class

public class AppInitializer implements WebApplicationInitializer 

@Override
public void onStartup(ServletContext container) throws ServletException 
    AnnotationConfigWebApplicationContext context = getContext();
    container.addListener(new ContextLoaderListener(context));

    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context));

    if (dispatcher == null) 
        System.out.println("Servlet is already added");
     else 
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    


private AnnotationConfigWebApplicationContext getContext() 
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(MvcConfiguration.class);
    context.register(HibernateConfiguration.class);
    return context;


@Bean(name = "propertyConfigurer")
public PropertySourcesPlaceholderConfigurer getPropertyPlaceholderConfigurer() 
    PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    placeholderConfigurer.setLocation(new ClassPathResource("common.properties"));
    placeholderConfigurer.setLocation(new ClassPathResource("amazon.S3Storage.properties"));
    placeholderConfigurer.setLocation(new ClassPathResource("rdbms.dev.properties"));
    placeholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
    return placeholderConfigurer;

HibernateConfiguration.class

@Configuration
@PropertySource("classpath:rdbms.dev.properties")
public class HibernateConfiguration 

    @Autowired
    private Environment env;

    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException, IOException 
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();

        Properties createStrategy = new Properties();
        createStrategy.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));

        emf.setJpaProperties(createStrategy);
        emf.setPackagesToScan("rinex.model");
        emf.setJpaVendorAdapter(getJpaVendorAdapter());
        emf.setDataSource(getDataSource());
        return emf;
    

    @Bean
    public JpaVendorAdapter getJpaVendorAdapter() 
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setGenerateDdl(false);
        jpaVendorAdapter.setDatabasePlatform(env.getProperty("hibernate.dialect"));
        return jpaVendorAdapter;
    

    @Bean(name = "dataSource")
    public BasicDataSource getDataSource() 
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        basicDataSource.setUsername(env.getProperty("jdbc.username"));
        basicDataSource.setPassword(env.getProperty("jdbc.password"));
        basicDataSource.setUrl(env.getProperty("jdbc.url"));
        basicDataSource.setInitialSize(Ints.tryParse(env.getProperty("connection.init_size")));
        basicDataSource.setMaxIdle(Ints.tryParse(env.getProperty("connection.pool_size")));
        return basicDataSource;
    

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) 
        return new JpaTransactionManager(entityManagerFactory);
    

没有任何 web.xml 文件的 SpringMVC 配置

            @Configuration
            @EnableAspectJAutoProxy(proxyTargetClass = true)
            @ComponentScan(basePackages = "rinex")
            @EnableWebMvc
            public class MvcConfiguration extends WebMvcConfigurationSupport 

            @Bean
            public ViewResolver getViewResolver() 
                InternalResourceViewResolver resolver = new InternalResourceViewResolver();
                resolver.setViewClass(JstlView.class);
                resolver.setPrefix("WEB-INF/views/");
                resolver.setSuffix(".jsp");
                return resolver;
            

            @Bean(name = "multipartResolver")
            public CommonsMultipartResolver multipartResolver() 
                CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
                multipartResolver.setMaxUploadSize(500000000L);
                return multipartResolver;
            
        

pom.xml

   <properties>
        <commons.loggin.version>1.1</commons.loggin.version>

        <hibernate.core.version>5.2.12.Final</hibernate.core.version>
        <javax.persistence.version>1.0.2</javax.persistence.version>
        <hibernate.persistence.version>1.0.0.Final</hibernate.persistence.version>
        <hibernate.validation>6.0.4.Final</hibernate.validation>
        <jakarta.dbcp.version>2.2.0</jakarta.dbcp.version>
        <postgres.jdbc.version>42.1.4</postgres.jdbc.version>

        <amazonaws.version>1.11.224</amazonaws.version>
        <java.version>1.8</java.version>

        <spring.version>5.0.1.RELEASE</spring.version>
        <spring.boot.version>1.5.7.RELEASE</spring.boot.version>
        <spring.webflow.version>2.4.5.RELEASE</spring.webflow.version>
        <cglib.version>3.2.5</cglib.version>
        <aspectj.version>1.8.12</aspectj.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <lombok.version>1.16.18</lombok.version>
        <java.bean.validation.version>2.0.0.Final</java.bean.validation.version>
        <java.el.validation.version>3.0.1-b04</java.el.validation.version>
        <fasterxml.jackson.version>2.9.2</fasterxml.jackson.version>
        <slf4j.version>1.7.5</slf4j.version>

        <mockito.version>1.9.5</mockito.version>
        <junit.version>4.12</junit.version>
        <junit.platform.version>1.1.0-M1</junit.platform.version>
        <junit.jupiter.version>5.1.0-M1</junit.jupiter.version>
        <junit.vintage.version>$junit.jupiter.version</junit.vintage.version>

        <servlet.api.version>4.0.0</servlet.api.version>
        <jsp.api.version>2.2</jsp.api.version>
        <jstl.api.version>1.2</jstl.api.version>

        <google.guava.version>22.0</google.guava.version>
        <commons-collections4.version>4.1</commons-collections4.version>

        <apache.maven.plugins.version>3.2.0</apache.maven.plugins.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging-api</artifactId>
            <version>$commons.loggin.version</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>$fasterxml.jackson.version</version>
        </dependency>

        <!--Hibernate-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>$hibernate.core.version</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>$javax.persistence.version</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>$hibernate.persistence.version</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>$hibernate.validation</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>$jakarta.dbcp.version</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>$postgres.jdbc.version</version>
        </dependency>
        <!--Amazon-->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>$amazonaws.version</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>$spring.boot.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>$spring.boot.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>$spring.version</version>
        </dependency>
        <!--AOP + AspectJ-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>$aspectj.version</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>$aspectj.version</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>$aspectj.version</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>$cglib.version</version>
        </dependency>
        <!--Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>$lombok.version</version>
        </dependency>
        <!-- Validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>$java.bean.validation.version</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>$java.el.validation.version</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>$java.el.validation.version</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>$slf4j.version</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>$slf4j.version</version>
        </dependency>
        <!--Tests-->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>$mockito.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>$junit.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>$junit.platform.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>$junit.jupiter.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>$junit.jupiter.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>$junit.jupiter.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>$junit.vintage.version</version>
            <scope>test</scope>
        </dependency>
        <!--Collection Utils-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>$google.guava.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>$commons-collections4.version</version>
        </dependency>
        <!-- Servlet Api -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>$jstl.api.version</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>$jsp.api.version</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>$servlet.api.version</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>$apache.maven.plugins.version</version>
        </dependency>
        <dependency>
            <groupId>com.github.davidcarboni</groupId>
            <artifactId>encrypted-file-upload</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>Octava</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>$apache.maven.plugins.version</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

【讨论】:

以上是关于工厂方法“dataSource”抛出异常;嵌套异常是 javax.naming.NameNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章

为啥我收到错误工厂方法'halLinkDisocoverer'在springboot中抛出异常?

为啥设置 DataSource 时 ComboBox 不抛出异常?

工厂方法“dataSource”抛出 NullPointerException

构造函数抛出异常嵌套异常是 java.lang.nullpointerexception

Laravel 7 模型工厂为 faker 抛出“InvalidArgumentException: Unknown formatter”异常

Automapper 链/嵌套对象映射抛出异常