在 grails 3.0 或 run-app 中运行 test-app 时,grails 会运行它自己版本的嵌入式 Tomcat 服务器。我能够从以下链接得出结论:https://roshandawrani.wordpress.com/2011/03/13/grails-tip-configuring-embedded-tomcat-instance-used-in-developmenttest-env/
但是,context.xml 和 server.xml 文件是使用下拉库预编译的。从头开始创建 grails 应用程序时,我找不到这两个文件中的任何一个。 config.groovy 也是如此,因为它位于外部库中。
我正在尝试将 JNDI 资源注入(inject)到容器中,以便我可以调用它们。像这样:
<Resource name="myDatasourceName" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/my_db_name"/>
在第一个链接中,作者在 scripts/_Events.groovy 目录中提供了一种方法,但我也没有。
更新 1:非工作代码
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.apache.catalina.Context
import org.apache.catalina.startup.Tomcat
import org.apache.tomcat.util.descriptor.web.ContextResource
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
import org.springframework.context.annotation.Bean
@SpringBootApplication
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
context.getNamingResources().addResource(preconfigureDbResource("oneSource", "127.0.0.1"))
context.getNamingResources().addResource(preconfigureDbResource("nextSource", "127.0.0.1"))
}
}
}
private ContextResource preconfigureDbResource(String name, String ip) {
ContextResource resource = new ContextResource()
resource.setType("javax.sql.DataSource")
resource.setName("jdbc/" + name)
resource.setProperty("url", "jdbc:oracle:thin:@" + ip + ":1521:ucop")
resource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver")
resource.setProperty("username", "coolio")
resource.setProperty("password", "password")
resource.setProperty("auth", "Container")
resource.setProperty("maxTotal", "100")
resource.setProperty("maxIdle", "30")
resource.setProperty("maxWaitMillis", "10000")
return resource;
}
}
我在我的服务文件中这样调用这个源:
public DataSource getOneSource() {
Context context = (Context) new InitialContext().lookup("java:/comp/env")
oneSource= (DataSource) context.lookup("jdbc/oneSource")
return oneSource
}
但我收到一条错误消息:
javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
有人做过吗?如果有一个额外的线程正在覆盖上下文,我不会感到惊讶。
最佳答案
在 Grails 3 中,您可以这样做:SampleTomcatJndiApplication
通常,在 Grails 网络应用程序中,这是在 /grails-app/init/Application.groovy
(在我的例子中,我注释掉了 jndiDataSource()
部分,只使用了 postProcessContext()
。)
关于spring - 在 Grails 3 中将 JNDI 添加到嵌入式 Tomcat 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35465278/