NameNotFoundException: Name jdbc is not bound in this Context - spring

I am trying to define a JNDI DB connection in a test project with Spring. I've bootstrapped the project with Spring Roo and thus is Mavenized. Here is the Roo script for reference (Roo 1.2.1)
project --topLevelPackage org.obliquid.cpool
jpa setup --database MYSQL --provider HIBERNATE --jndiDataSource /jdbc/cpool
web mvc setup
entity jpa --class org.obliquid.cpool.entity.Person
field string --fieldName name
web mvc scaffold --class ~.entity.Person
web mvc all --package ~.web
In src/main/resources/META-INF/spring/applicationContext.xml I've the following (created by Roo):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
...
<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/cpool" resource-ref="true"/>
...
I've created src/main/resources/META-INF/context.xml with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/myapp" docBase="cpool" reloadable="true" debug="1">
<Resource name = "jdbc/cpool"
auth = "Container"
type = "javax.sql.DataSource"
username = "dbusername"
password = "dbpassword"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/dbname?DateTimeBehavior=convertToNull&characterEncoding=UTF-8"
maxActive = "100"
maxIdle = "4"
maxWait = "20000"
removeAbandoned = "true"
removeAbandonedTimeout="600"
logAbandoned="true"/>
</Context>
However, when I try to run the application in Tomcat 7.0, I get the following error:
ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
What should I do to properly define the datasource?

The context.xml file must be in the META-INF directory of the war file. It must not be in the classes directory or in a jar file.
Put the META-INF directory with the context.xml in the directory containing the root of the webapp in your source folder tree.

Related

deploying rest api war and starting with App Manager produces error FAIL - Application at context path

First time working with web apps and created a simple springboot rest api and it worked fine locally. Deployed the war to a tomcat server by copying the war file into the /webapps/ directory and restarted tomcat.
I checked if this was successful by opening the tomcat web application manager page and can see the app listed:
http://1xx.xxx.xxx.xxx:8080/manager/
When I click start to get it running the following error appears:
FAIL - Application at context path [/first_restapi] could not be started
I searched around and most answers suggest ensuring all pom file dependencies are included and to check the logs. I checked the logs on the tomcat server and the following error stuck out:
Exception sending context initialized event to listener instance of class
[org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem:
Unable to locate Spring NamespaceHandler for XML schema namespace
[http://www.springframework.org/schema/security]
Offending resource: ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
I did not include a servlet-context.xml file as the tutorial did not show that. But I added it anyway and still got the same error. Can anyone help with this issue?
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-
beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<security:http auto-config="true" create-session="stateless">
<security:intercept-url pattern="/**" access="ROLE_USER"
requires-channel="http"/>
<security:http-basic />
</security:http>
</beans:beans>
You have a dependency problem. For each XML namespace that you (effectively) use in your configuration file, you need the appropriate NamespaceHandler. So in your case, to use the:
beans or util namespace you need spring-beans.jar,
context or task namespace you need spring-context.jar,
mvc namespace you need spring-webmvc.jar,
security namespace you need spring-security-config.jar.
You certainly don't have the last one dependency.

How to fix "Configuration Problem: Unable to locate Spring NamespaceHandler for XML schema namespace context"?

I was trying to run application in eclipse, it was running fine. I created jar using Maven install, and while running the jar I am getting the error "Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: class path resource [appContext.xml]"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="package"></context:component-scan>
...
Snap of project structure

TomEE + Spring Framework + JNDI Lookup Failures

I have a simple Spring Framework (4.3.16.RELEASE) ear file deployed to TomEE (tomee-plume-8.0.0-M2) using Zulu Java (zulu8.36.0.1-ca-jdk8.0.202-macosx_x64).
I getting errors injecting a data source via JNDI Lookup. My TomEE instance has the following resource in server.xml:
<Resource
auth="Container"
driverClassName="oracle.jdbc.OracleDriver"
name="jdbc/my/DataSource"
password="1234"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:#database.local:1521/db"
username="admin" />
In my application ear I have the following in my web.xml file:
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/r/*</url-pattern>
</servlet-mapping>
My applicationContext.xml that lookups the JNDI resource:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configures support for #Controllers -->
<context:component-scan base-package="local.gerb" />
<mvc:annotation-driven/>
<jee:jndi-lookup id="writeDataSource" jndi-name="jdbc/my/DataSource" resource-ref="true"/>
<bean id="helloWorld" class="local.gerb.HelloWorldImpl" />
</beans>
However, when I deploy the application I get the following error in the log file:
CreationException: Error creating bean with name 'writeDataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/my/DataSource] is not bound in this Context. Unable to find [jdbc].
04-Mar-2019 12:43:33.610 SEVERE [main] org.springframework.web.servlet.DispatcherServlet.initServletBean Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'writeDataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/my/DataSource] is not bound in this Context. Unable to find [jdbc].
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630)
I do see the jdbc/my/DataSource being created in TomEE:
04-Mar-2019 12:43:32.113 INFO [main] org.apache.tomee.catalina.OpenEJBNamingContextListener.bindResource Importing a Tomcat Resource with id 'jdbc/my/DataSource' of type 'javax.sql.DataSource'.
04-Mar-2019 12:43:32.113 INFO [main] org.apache.openejb.assembler.classic.Assembler.createRecipe Creating Resource(id=jdbc/my/DataSource)
So I believe I created the Resource correctly in TomEE but for some reason, spring framework is unable to correctly inject the resource.
I do have the full source pushed to my github repo: https://github.com/jstralko/tomee-poc/tree/master/SpringExamples in case you want to look at everything, I kept it as minimal as possible to help debug this issue.
Any help would be greatly appreciated.
I feel silly, apparently, once I moved the Resource into context.xml from server.xml; I was able to successfully inject a JDBC resource.

Set System Properties in JSF+Spring Application

I am developing a web application using JSF 2.0 ,Spring 3.1.
When I am deploying my application I am getting following error
ERROR [ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'AllProjectDetailsBean' defined in ServletContext resource [/WEB-INF/springApp-spring.xml]: Could not resolve placeholder 'OutputFilePath'
Application Context file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
</bean>
<bean id="AllProjectDetailsBean"
class="com.tcs.srl.expertFinder.sections.ProjectDetailsBeanFactory"
factory-method="createInstance">
<constructor-arg index="0" value="${OutputFilePath}" />
</bean>
What I understand from error is when I deploy application the context loads very first time but it fails because it dose not find any property with name OutputFilePath.
That means I have to set system property with the name OutputFilePath before context loads.
Can some tell me how to set system property while deploying jsf application or before context loads.
Or is there any option to remove this error.
Please Help
Thanks

Drools integrated with spring faced with a xsd not found warning

I'm using Spring+Drools in my Web Application.The following is the drools modules configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:drools="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://drools.org/schema/drools-spring
http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd"
default-autowire="byName">
<drools:kbase id="kbase1">
<drools:resources>
<drools:resource type="DRL" source="classpath:uniteStickyRules.drl"/>
</drools:resources>
</drools:kbase>
<drools:ksession id="ksession1" type="stateful" kbase="kbase1"/>
When I ran my application using this configuration in my junit project it worked, yet it came with the following problem when I startup my Web application. As a result, I couldnot find my kbase bean.
2012-03-29 12:55:48,402 WARN resolver.SpringPluggableSchemas - Could not find schema classpath:org/drools/container/spring/drools-service-spring.xsd for URI: http://drools.org/schema/drools-service-spring.xsd,
class path resource [org/drools/container/spring/drools-service-spring.xsd] cannot be resolved to URL because it does not exist
2012-03-29 12:55:48,413 WARN resolver.SpringPluggableSchemas - Could not find schema classpath:file:/C:/Users/Dan%20Diephouse/workspace/xfire/target/checkout/xfire-spring/target/test-generated/services.xsd for URI: http://xfire.codehaus.org/config/1.0,
class path resource [file:/C:/Users/Dan%20Diephouse/workspace/xfire/target/checkout/xfire-spring/target/test-generated/services.xsd] cannot be resolved to URL because it does not exist
You see, I didn't put the drools-service-spring.xsd in my .xml file. So I'm quite stuck with it. Any suggestions?

Resources