I am currently working on spring and I am very much new to this framework. I have added all the required jars of spring and written the following spring-dispatcher-servlet.xml 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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
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.1.xsd">
<context:component-scan base-package="com.quiz_mcq.controller, com.quiz_mcq.service, com.quiz_mcq.dao"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My code works fine when there is internet connection but breaks down when I am not connected to the internet. And gives the following error.
Line 12 in XML document from ServletContext resource [/WEB-INF/spring->dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 109; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.
What am I doing wrong here?
Can any tell me the solution what needs to be done to work the project even offline?
for xsi schema Location put
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
classpath:org/springframework/beans/factory/xml/spring-context-3.0.xsd"
in your case code need to check on springframework.com every time
the schema location i provided should work because the xsd is packaged inside the spring jar here
Related
I was getting the following error message while i was working with Spring with IntelliJ:
Failed to read schema document
'http://www.springframework.org/schema/beans/spring-context.xsd',
because 1) could not find the document; 2) the document could not be
read; 3) the root element of the document is not .
Help on this.
I was using wrong url in xsi:schemaLocation
Instead if using:
http://www.springframework.org/schema/beans/spring-context.xsd
I should have to used:
http://www.springframework.org/schema/context/spring-context.xsd
That solved my problem.
I crossed checked this with firing url into browser where within beans no spring-context.xsd was available.
Hope some of you can help with this.
#Indrajeet : The proper spring.xml or application-context.xml format :
<?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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.demo" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
I'm placed statics folder css, js, images, wanted add sources on web-app
the xml seem can't be resolved.
report error is:
Cant resolve location statics, Spring Xml mode validaton
after open browser checked Sources, can't load js, images
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.f.dao, com.f.controller, com.f.service, com.f.advice"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/statics/" location="statics"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/users/"/>
<property name="suffix" value=".jsp"/>
</bean>
<aop:aspectj-autoproxy/>
</beans>
I want to execute a method every hr which will do my job.
I am using SPRING 3.0 (please keep in mind) schedule cron below is my code. But it is giving the below error.
Error : "Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'scheduling.job.cron'"
ApplicationContext.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"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd ">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:test.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
Java Class
#Scheduled(cron = "${scheduling.job.cron}")
public void testScheule()
{
logger.info("Schedule Call" + new Date());
}
Properties file (which is present in src/main/resource/test.properties) contain below line
scheduling.job.cron=0 0/1 * * * ?
Can somebody please help me to get out of this error and work sucessfully.
Thanks in advance.
Be sure you have correctly configured your application.xml
<?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:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:annotation-driven />
<util:properties id="testProps" location="test.properties" />
<context:property-placeholder properties-ref="testProps" />
<bean id="yourBeanClassHere" class="com.howtodoinjava.service.YourBeanClassImplHere"></bean>
</beans>
Have a look at this link for a better understanding http://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/
What you are trying to achieve is not possible in Spring 3.0.
This feature is available ,starting from spring 3.0.1.
Even if you are able to resolve the property scheduling.job.cron from test.properties file, you will receive below exception:-
java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in ${scheduling.job.cron})
In Spring 3.0, only possible way to do is:-
#Service (value="testService")
public class TestService {
// ..
public void testScheule() { .. }
}
Now you can define in xml like below:-
<context:component-scan base-package="com.some.package" />
<task:annotation-driven />
<util:properties id="applicationProps" location="test.properties" />
<task:scheduled-tasks>
<task:scheduled ref="testService " method="testScheule" cron="#{applicationProps['scheduling.job.cron']}" />
</task:scheduled-tasks>
I am writing a web application with Spring Framework 4.0.6 and I have to schedule a small routine to run everyday at the same time.
I am trying it by using the #Scheduled annotation and adding the <task:annotation-driven /> tag in my dispatcher-servlet.xml , but I get a SAXParseException when the application starts.
My xml file is this:
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<context:component-scan base-package="my.package.name.controllers" />
<mvc:annotation-driven/>
<task:annotation-driven />
</beans>
The full error I get is this:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 30 in XML document from ServletContext resource [/WEB-INF/spring-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 30; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'task:annotation-driven'.
Solved it!
The problem was in the / characters, at the end of http://www.springframework.org/schema/task/ and http://www.springframework.org/schema/util/. Just removed them, like in http://www.springframework.org/schema/task and now it works just fine.
After I have upgraded spring to 3.2.3.RELEASE, I am getting the following error on Tomcat startup in Eclipse:
ug 12, 2013 1:59:14 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalArgumentException: Cannot locate BeanDefinitionParser for element [component-scan].
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.findParserForElement(NamespaceHandlerSupport.java:63)
at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseCustomElement(DefaultXmlBeanDefinitionParser.java:399)
at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseBeanDefinitions(DefaultXmlBeanDefinitionParser.java:358)
at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.registerBeanDefinitions
Here's my applicationContext.xml:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="merchant" />
<context:component-scan
base-package="merchant.domain" />
<context:component-scan
base-package="merchant.controller" />
<context:component-scan
base-package="merchant.repo" />
<context:component-scan
base-package="merchant.service" />
<!-- Load Hibernate related configuration -->
<import resource="hibernate-context.xml" />
<tx:annotation-driven/>
<mvc:annotation-driven/>
</beans>
I searched online a lot for a solution, but couldn't find any relevant ones.
Please help!
Thanks in advance!
This error is very likely because the spring-context-3.2.3.RELEASE.jar is somehow not being found in the classpath after your upgrade. Please do check your dependency and make sure this jar is in the list of dependencies.