I am able to compile and start my Spring project using Maven:
mvn -e clean compile exec:java -Dexec.mainClass=de.fraunhofer.fkie.tet.vmware.manager.Test
However, when I assemble all jars in a single file using the maven-assembly-plugin (including applicationContext.xml), I always get an Exception during the java execution:
java -cp target/test-jar-with-dependencies.jar:. de.fraunhofer.fkie.tet.vmware.manager.Test
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Sep 6, 2010 10:37:21 AM org.springframework.util.xml.SimpleSaxErrorHandler warning
WARNING: Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/context/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 <xsd:schema>.
...
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 10 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c:
The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
...
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c:
The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
I also tried to attach the schema definitions, i.e. spring-context.xsd etc., directly to the classpath, but without any success.
less src/main/resources/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: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:annotation-config /> <!-- wegen '#PostConstruct' -->
<!--<context:component-scan base-package="de.fraunhofer" /> -->
...
</beans>
Spring namespace handlers are resolved using files /META-INF/spring.schemas and /META-INF/spring.handlers. Because files with these names exist in different Spring jars, probably only one of them remains in target jar after maven-assembly-plugin.
Perhaps you may merge these files manually and somehow configure maven-assembly-plugin to overwrite file in target jar with this merged file.
I suspect your spring config file is missing the context XML namespace. It should be added to the root element of your spring config file like this:
<?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"
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">
<context:annotation-config />
<context:component-scan base-package="com.abc.xyz" />
</beans>
I've found the root problem for this as per the reply of axtavt, and I've reported it as a ?bug? in Spring: https://jira.springsource.org/browse/SPR-8368 - a workaround to generate your own merged copies of these files is included there. For posterity the code is here as well:
//IOUtils and FileUtils come from Apache Commons IO
for(String s : new String[] {"spring.schemas", "spring.handlers", "spring.tooling"}) {
Enumeration<?> e = Test.class.getClassLoader().getResources("META-INF/"+s);
StringBuilder out = new StringBuilder();
while(e.hasMoreElements()) {
URL u = (URL) e.nextElement();
out.append(IOUtils.toString(u.openStream())).append("\n");
}
File outf = new File(s);
FileUtils.writeStringToFile(outf, out.toString(), "UTF-8");
}
I believe there are 3 solutions to this problem
The tx jar file should be included in the classpath/buildpath of the project. (most common error)
Mentioned by "axtavt" above
Try this before "axtaxt" solution: go to your war directory (specified in GWT compile's advanced tab) and put the spring-tx.jar file in the lib folder under your war directory, refresh and run again.
What spring dependencies do you have in your pom? You may get xml parsing errors due to some spring jar files not being on the class path. In spring 3 the library was split up into many jar files. Check out this post to see what you need, specifically:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
As it suggests there is a problem with the parsing either in Request or in Response. This error can occur if the RestTemplate client is expecting particular kind of Response from the Resource, but the Resource is returning something entirely. I got this error for a POST RestTemplate client that was related to change in the Response returned.
For e.g. Initial RestTemplate that was returning an Entity 'MyClass' changed and stared returning String so the Parser started giving error
ResponseEntity<MyClass> postResponseEntity = restTemplate.postForEntity(postEndPoint, httpRequestEntity, MyClass.class);
MyClass postResponseBody = postResponseEntity.getBody();
Changed to
ResponseEntity<String> postResponseEntity = restTemplate.postForEntity(postEndPoint, httpRequestEntity, String.class);
String postResponseBody = postResponseEntity.getBody();
As the response type changed from the entity to String the parser started giving error when processing the Response. Changed it to correct Response type (which in my case was String) and it started working.
Related
I am attempting to implement an outdated open-source Properties File Manager (https://github.com/Confluex/Zuul/wiki) with a Mule Test Application in MuleSoft's Anypoint Studio platform. This appears to require the Spring Context Schema, but the project cannot locate it. It continually states there is no 'context:property-placeholder'. I feel a versioning error may be the problem. Here is my attempt:
<mule
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:zuul="zuul-spring-client-1.5.1"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
zuul-spring-client-1.5.1 zuul-spring-client-1.5.1.xsd">
<context:property-placeholder properties-ref="MuleMeetZuul" />
</mule>
Here is the error thrown:
Caused by: org.mule.runtime.api.exception.MuleRuntimeException: There was '1' error while parsing the given file 'zuultest.xml'.
Full list:
org.xml.sax.SAXParseException; lineNumber: 17; columnNumber: 65; cvc-complex-type.2.4.a: Invalid content was found starting with element 'context:property-placeholder'. One of '{"http://www.mulesoft.org/schema/mule/core":annotations, "http://www.mulesoft.org/schema/mule/core":description, "http://www.mulesoft.org/schema/mule/core":global-property, "http://www.mulesoft.org/schema/mule/core":configuration, "http://www.mulesoft.org/schema/mule/core":notifications, "http://www.mulesoft.org/schema/mule/core":abstract-extension, "http://www.mulesoft.org/schema/mule/core":abstract-shared-extension, "http://www.mulesoft.org/schema/mule/core":abstract-mixed-content-extension, "http://www.mulesoft.org/schema/mule/core":abstract-security-manager, "http://www.mulesoft.org/schema/mule/core":abstract-transaction-manager, "http://www.mulesoft.org/schema/mule/core":abstract-shared-transaction-manager, "http://www.mulesoft.org/schema/mule/core":abstract-connector, "http://www.mulesoft.org/schema/mule/core":abstract-shared-connector, "http://www.mulesoft.org/schema/mule/core":abstract-global-endpoint, "http://www.mulesoft.org/schema/mule/core":abstract-exception-strategy, "http://www.mulesoft.org/schema/mule/core":abstract-on-error, "http://www.mulesoft.org/schema/mule/core":abstract-flow-construct, "http://www.mulesoft.org/schema/mule/core":flow, "http://www.mulesoft.org/schema/mule/core":sub-flow, "http://www.mulesoft.org/schema/mule/core":top-level-processor, "http://www.mulesoft.org/schema/mule/core":abstract-global-intercepting-message-processor, "http://www.mulesoft.org/schema/mule/core":abstract-object-store}' is expected.
Any advice would be much appreciated. Thanks.
If you are trying to use the Mule 3 implementation in a Mule 4 it will fail, and that should be expected. Mule 3 property placeholder providers where directly Spring property placeholder providers. Mule 4 uses a different way to implement them. <context:property-placeholder> doesn't exist in Mule 4. Its replacement <configuration-properties> will not work for this usage.
Instead you will have to use Mule SDK for Java to develop a custom configuration property provider by implementing the ConfigurationPropertiesProviderFactory interface in a factory class and extending the DefaultConfigurationPropertiesProvider class to implement the provider. In the provider you will need to use the Zuul library to implement the operations to get the keys and values.
Instructions are available in the documentation: https://docs.mulesoft.com/mule-runtime/4.3/custom-configuration-properties-provider
I am very new to Spring Framework. I am using NetBeans for IDE. I followed couple of tutorials to learn it by myself. However, I am stuck in the middle and cannot proceed further. Let me breakdown my project here:
My project folder structure looks like this:
There are two classes; the major one MainApp.java contains following code:
package com.myprojects.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context;
context = new ClassPathXmlApplicationContext("classpath*:beans.xml");
FirstPage obj;
obj = (FirstPage) context.getBean("firstPage");
obj.getMessage();
}
}
Second class file FirstPage.java looks like this:
package com.myprojects.spring;
public class FirstPage {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
The beans.xml file looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.0.RELEASE.xsd
">
<bean id = "firstPage" class = "com.myprojects.spring.FirstPage">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
Now, the error I am getting is like below:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'firstPage' is defined
I know I have been doing some silly mistake here.
Thank you in Advance !
Almost similar problem have been discussed before. I think your program is unable to locate beans.xml.
Try doing this:
context = new ClassPathXmlApplicationContext("META-INF/beans.xml");
EDIT:
This new error XmlBeanDefinitionStoreException means that your schema is not valid. Try changing your schema as described in one of these answers:
https://stackoverflow.com/a/21525719/2815219
https://stackoverflow.com/a/25782515/2815219
Spring configuration XML schema: with or without version?
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id = "firstPage" class = "com.myprojects.spring.FirstPage">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
According to the directory structure you posted it is very likely that src/main/resources is on your classpath. If you like to reference your spring context file beans.xml you have to specify it relative to the folders on your classpath. Hence you should try:
context = new ClassPathXmlApplicationContext("classpath:/META-INF/beans.xml");
Besides: the notation classpath*:beans.xml means you want to read in all context files having a name of beans.xml.
Put beans.xml to outside Meta-inf ,
or use new ClassPathXmlApplicationContext("META-INF/beans.xml");
And http://www.springframework.org/schema/beans/spring-beans-4.0.0.RELEASE.xsd
should change to
http://www.springframework.org/schema/beans/spring-beans-4.0.0.xsd , as spring's xsd filenames don't contain "RELEASE".
The xsd files are in org.springframework.beans.factory.xml package in spring-beans.jar, see if the xsd file is in that package.
Doing following two things solved my issue.
1) There was an incorrect beans.xml path. I changed that to context = new ClassPathXmlApplicationContext("META-INF/beans.xml");.
2) Also, there was an invalid xsi:schemaLocation attribute value. I changed that attribute's value to http://www.springframework.org/schema/beans/spring-beans-3.0.xsd.
Thank you all for your help.
I am trying to write a spring program with using Annotation config in xml file, It is working without this configuration initiating also. I wrote method() in one file and and declared CLassPathXmlApplicationContext with Beans.xml file, I wrote a Student bean and applied the #Required annotation for setter methods, Please find the Bean configuration xml file below:
<?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"
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">
<context:annotation-config/>
<!-- Definition for student bean -->
<bean id = "student" class = "com.tutorialspoint.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/>
</bean>
</beans>
When i run this program with or without following Annotation indication in xml file, the code works fine, Please guide me the underlying reason for it.
<context:annotation-config/>
The #Required annotation applies to bean property setter methods and it indicates that the affected bean property must be populated in XML configuration file at configuration time.
Once <context:annotation-config/> is configured, you can start annotating your code to indicate that Spring should automatically wire values into properties, methods, and constructors. It does not means that some things will wrong without it when you using the #Required
In your xml, you have defined your properties, so the #Required will run well
So I have been using the 3.2.0.RELEASE Spring XML configurations for most of my beans but now I am faced with a unique situation where the Getters and Setters can't be used (bad legacy code - can't get around it).
As such, I want to use Spring #Configuration class and the XML to workaround this problem.
However, I am getting "Class Not Found" exception when it tries to read my #Configuration Class.
Caused by: java.lang.ClassNotFoundException: v1.inventory.item.myJavaConfig
My XML file which is failing looks like this:
<?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"
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:annotation-config/>
<bean class="v1.inventory.item.myJavaConfig"/>
</beans>
My #Configuration class looks like this:
package v1.inventory.item;
#Configuration
#ImportResource("classpath:v1/inventory/item/baseItemConfigs.xml")
public class myJavaConfig {
#Autowired
#Qualifier("parentItem")
Item baseItem;
#Bean
public Item realItem(){
Item modifiedBean = baseItem;
modifiedBean.setManufacturer("Fake Setter for Manufacturer");
modifiedBean.setDesigner("Fake Setter for Designer");
return modifiedBean;
}
}
I need this to be read by the ApplicationContext so I need to make sure these beans can be found. Is this a bug with Spring 3.2.0.RELEASE? Or my code?
For the record, I am pulling in the #Configuration last (parentItem is scanned first in XML).
I figured out the issue here.
It seems that maven/spring (not sure which) wasn't looking in my "resource" directory for the file. Only my "java" directory. When I moved my file into the "java" directory, Spring found the file just fine.
The tests which were passing in JUnit now pass using the Maven compiler (which was throwing the above error during the test phase)
I have followed the following Spring MVC tutorial which works fine until I deploy step 4.6:
Spring MVC step by step tutorial
The Errors I get are:
SEVERE: Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 i
n XML document from ServletContext resource [/WEB-INF/springapp-servlet.xml] is
invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4
.a: Invalid content was found starting with element 'beans'. One of '{"http://ww
w.springframework.org/schema/beans":description, "http://www.springframework.org
/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "htt
p://www.springframework.org/schema/beans":bean, WC[##other:"http://www.springfra
mework.org/schema/beans"]}' is expected.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadB
eanDefinitions(XmlBeanDefinitionReader.java:369)
I don't know why they are showing it like that and honestly you shouldn't be looking at a super old tutorial like that, but the solution is to simply remove the <beans> in
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- the application context definition for the springapp DispatcherServlet -->
<beans> <---remove this guy