我正在尝试从Spring迁移到Spring Boot。我已经将以下代码添加到我的pom.xml中:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
这不是我的spring-web依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我添加了默认的Spring Boot应用程序类。这是我的应用程序配置类:
@Configuration
@ComponentScan("com.foxminded.university.dao")
@PropertySource("classpath:config.properties")
public class ApplicationConfig {
@Value("${jndiLookup}")
private String name;
@Bean
DataSource dataSource() throws NamingException {
return new JndiTemplate().lookup(name, DataSource.class);
}
@Bean
public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
当我尝试运行应用程序时,我在JndiTemplate().lookup(name, DataSource.class)
中收到错误消息:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
我确定我的server.xml包含有关数据源的信息,而我的context.xml包含正确的资源链接,这导致它在没有Spring Boot的情况下可以正常工作。我还尝试添加spring boot tomcat启动器,但没有帮助。
这是我的项目结构:
答案 0 :(得分:0)
您可以使用application.properties(或.yml)配置数据源:
jdbc.url=java:comp/env/jdbc/JNDINAME
Java配置:
@Bean
public DataSource dataSource() throws NamingException {
return (DataSource) new JndiTemplate().lookup(env.getProperty("jdbc.url"));
}
如果这不起作用,请尝试以下操作:Spring Boot with JNDI Data Source