spring-beans.xsd does not define "local". Why? - java

Almost every spring project uses spring-beans.xsd (refers to it to be more precise). However, if you look at the file, http://www.springframework.org/schema/beans/spring-beans.xsd, you'll see that it is version 3.2 and doesn't have a definition for the attribute "local".
What's even more interesting, is that http://www.springframework.org/schema/beans/spring-beans-3.2.xsd does actually define "local".
Also since the file is pulled out of the jar (org/springframework/beans/factory/xml/spring-beans-3.2.xsd) due to spring.schema, I don't think any project will have compile or runtime issues.
Eclipse's xml validator on the other hand, I think, uses only the internet link and shows an xml error, more specifically :
"cvc-complex-type.3.2.2: Attribute 'local' is not allowed to appear in element 'ref'"
Is this is a bug?
Edit:
As per request, this is my spring header:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
Edit 2: here is where I'm using local
<bean id="bar"
class="org.springframework.batch.item.support.CompositeItemWriter">
<property name="delegates">
<list>
<ref local="foo" />
</list>
</property>
</bean>
<bean id="foo" class="java.lang.String" />

You should try to use <ref bean="someBean"/> instead.

As stated here: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/
The local attribute on the ref element is no longer supported in the 4.0 beans xsd since it does not provide value over a regular bean reference anymore. Simply change your existing ref local references to ref bean when upgrading to the 4.0 schema.
As you might know Spring 3.x is not fully compatible with java 8, so you either use unsupported and outdated Java 7 with Spring 3.x, or upgrade both Spring and JDK. I would strongly recommend the latest

The http://www.springframework.org/schema/beans/spring-beans.xsd actually points to the 4.0 XSD, which does NOT have local. Additionally, There are bugs in eclipse with spring integration that could be the issue. INT-1353
Additionally, there /was/ a configuration option for eclipse to specify loading XSDs from Classpath, but I'm not 100% sure this works or even exists anymore. There is a screenshot of that here: https://stackoverflow.com/a/2896051/1958771
~ sheenobu -> curl -s http://www.springframework.org/schema/beans/spring-beans.xsd | md5
0483a5565138fe9f79c5fbe38c7c5969
~ sheenobu -> curl -s http://www.springframework.org/schema/beans/spring-beans-3.2.xsd | md5
1562baeab9550e2149e9608dbb3bbadd
~ sheenobu -> curl -s http://www.springframework.org/schema/beans/spring-beans-4.0.xsd | md5
0483a5565138fe9f79c5fbe38c7c5969

Spring (and Eclipse) will use whichever xsd you declared in the schemaLocation regardless of the one in the jar file. This is actually validation done by the XML parser that Spring uses underneath it all.
If you want to use the local attribute of ref, you will need to declare a schemaLocation that points to a schema that has the local attribute. Note that if you do do this, the generated context will need to be parseable by the BeanDefinitionParser that exists in your Spring jar.

I have solved the problem with using older http://www.springframework.org/schema/beans/spring-beans-3.2.xsd in xsi:schemaLocation. (As my project is "older" as well)

Related

How to load XSD files in spring when the app is working offline

I have gone through multiple stackoverflow threads but couldn't find a solution so posting this question. This is regarding how the xsd's are being mapped in spring container.
Below is the configuration in the context.xml
<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.xsd">
This works perfectly when the application is connected to internet, but the problem is when it works offline. It throws below error on start-up.
Caused by: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber:
68; cvc-elt.1.a: Cannot find the declaration of element 'beans'.
If I explicitly point to the classpath like this,it works.
xsi:schemaLocation="http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans.xsd"
As per my understanding, Every spring jar shipped with the xsd and it is being mapped in META-INF/spring.schemas. The refered xds in the xml file should be able to load the local xsd available in the jar.
My concern is
Why it is unable to load the local xsd when there is no internet?
Of-course I can refactor all the xsi:schemaLocation to load from classpath but I have more than 25 xml files so instead of changing in all of them is there a better way to deal with it?
Spring version - spring-beans-5.1.5.RELEASE.jar
spring.schemas -
http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.0.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.1.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.2.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.3.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool-4.0.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool-4.1.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool-4.2.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool-4.3.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
http\://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util-4.0.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util-4.1.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util-4.2.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util-4.3.xsd=org/springframework/beans/factory/xml/spring-util.xsd
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util.xsd

Problem configuring Jolokia server and agent

I have a Spring Boot Java application that among other things uses Apache Camel to do a little route interception and data modification. We have a separate "camel routes" file (camelRoutes.xml) that defines the camel routes that will be used. There is also a bit of configuration in that file that, as best I understand, configures a Jolokia MBean server and Jolokia agent.
There is something about the Jolokia config XML that the system is not happy with. I've done some very quick Googling and it seems like it's specified properly (see section 9.3.2 in https://jolokia.org/reference/html/jmx.html). The problem manifest itself in various ways. When the file is open in the XML editor in eclipse, the corresponding XML is highlighted with an error (although it provides no information about what it thinks is wrong). More importantly, when I attempt to run the application, it fails with cascading exceptions, basically pointing to a problem in the config file.
Excerpts from camelRoutes.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jolokia="http://www.jolokia.org/jolokia-spring/schema/config"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.jolokia.org/jolokia-spring/schema/config http://www.jolokia.org/jolokia-spring/schema/config/jolokia-config.xsd
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.xsd">
...
<context:mbean-export server="jolokiaServer" />
<jolokia:mbean-server id="jolokiaServer" />
<jolokia:agent lookupConfig="false"
systemPropertiesMode="never">
<jolokia:config autoStart="true" host="0.0.0.0"
port="${jolokia.port:8778}"
agentDescription="${jolokia.agentName:jolokia}" />
</jolokia:agent>
Errors are displayed in eclipse for the line where the jolokia:mbean-server and jolokia:agent are defined.
Small section of the exception trace when application is executed:
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 85 in XML document from file [C:\Users\JO24447\workspace\RST_Service_Base\rust-service-base-rest\target\classes\rst_camel\camelRoutes.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 85; columnNumber: 45; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'jolokia:mbean-server'.
Any ideas what could be wrong?
In my case the cause was that I used the Maven assembly plugin which did not merge the spring.schemas files. I switched to the shader plugin and schema validation worked.

Spring Batch Job Scope Unavailable

It seems like the Spring Batch 3.0.6 release points to the 2.2 schema by default.
In the spring.schemas file there is this definition
http\://www.springframework.org/schema/batch/spring-batch.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.2.xsd
And when I add this namespace to my spring file, I then get the scope="step" option (in my IDE, although IntelliJ has been pretty reliable matching runtime issues)
...
xmlns:batch="http://www.springframework.org/schema/batch"
...
But everything seems to claim that scope="job" is invalid.
Is it the spring.schemas file doing this? Why would the 3.xx iteration point back to the 2.2 schemas?
I can't seem to get a declaration like xmlns:batch="http://www.springframework.org/schema/batch/spring-batch-3.0.xsd" to work. Surprisingly that totally invalidates the batch namespace. Even though the spring.schemas file points to 3.xx like so
http\://www.springframework.org/schema/batch/spring-batch-3.0.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-3.0.xsd
If I do this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
...
</beans>
I can then get the scope="step" to be valid, and everything seems to point to batch-3.0, but scope="job" is still invalid.
Does anyone have a clean example of importing batch & using a job scope declaration on a bean?
Thanks!

Spring 3.1: Getting the <mvc:resources To Work

I'm new to Spring. I'm learning Spring 3.1. I'm using the book "Spring In Action, 3rd ed.. I'm having trouble getting static resources found. The book doesn't say much about it so I was wondering if I could get some help.
The name of my application is "abc"
The URL is something like "https://blah.blah.blah/abc
I'm running "abc" on WebLogic 9.2
I'm deploying "abc" as "abc.war"
The directory structure inside of my "abc.war" is
WEB-INF
classes
lib
web.xml
webLogic.xml
abc-servlet.xml
css
images
js
jsp
META-INF
In my web.xml I have my dispatcher servlet defined like this:
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
.....and this is the content of my abc-servlet.xml file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.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.0.xsd">
<context:component-scan base-package="org.company.abc.controllers" />
<mvc:resources mapping = "/**" location = "/"/>
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/jsp/"/>
<property name = "suffix" value = ".jsp"/>
</bean>
</beans>
I've tried putting my static resources under a "resources" directory off of the root of my webapp, but that didn't make any difference ( I changed the tag to <mvc:resources mapping = "/resources/**" location = "/resources"/> when I did that ).
Basically, the abc webapp isn't finding any of the static resources.
It thinks the picture "header.png" is at
- htts://blah.blah.blah/images/header.png
So the image comes up blank, though when I paste this URL into a browser, I can get the image
- htts://blah.blah.blah/**abc**/images/header.png
I haven't found a way to get Spring to stick a "abc" into that url though.
I'm a brand new white belt with Spring, so baby talk in helping me figure this out with suggestions will not be taken as an insult :)
Thanks in advance for any help
Steve
I tried my stuff in Tomcat 6 and all worked well ( this is not the first time something was harder in WebLogic, FWIW ).
I Google around and found this
http://forum.springsource.org/showthread.php?102166-mvc-resources-and-war-on-Weblogic/page2
explaining that WebLogic and Spring 3.* have unresolved issues.
Being a newbie to Spring and WebLogic I didn't understand most of it.
I did notice that all of my static stuff would map to
https:/blah.blah.blah/abc/images/myimage.png
But Spring would think it was
https:/blah.blah.blah/images/myimage.png
So,
I set my mvc tag to <mvc:resources mapping = "/resources/**" location = "/resources"/>
Then in my new files that use spring I changed the paths to ../abc/images/myimage.png
It all works. The legacy files are still finding the static resources off of the root in the war with the paths in the legacy code AND the new Spring files ( I'm gradually converting abc from a Servlet webapp to a Spring webapp ) find them as ell.
Its a hack and its crude, but it gets me going for now

Eclipse: How to convert a web project into an AspectJ project and weave and run it using the AJDT plug in?

What I want to do:
I want to use the #Configured annotation with Spring. It requires AspectJ to be enabled. I thought that using the AJDT plugin for compile time weaving would solve this problem. Before installing the plug in the dependencies which were supposed to be injected into my #Configured object remained null.
What I have done:
Installed the AJDT: AspectJ Development Tools plug in for Eclipse 3.4.
Right clicked on my web project and converted it into a AspectJ project.
Enabled compile time weaving.
What doesn't work:
When I start the Tomcat 6 server now, I get an exception*.
Other information:
I haven't configured anything in the AspectJ Build and AspectJ Compiler parts of the project properties.
JDT Weaving under Preferences says weaving is enabled.
I still have Java build path and Java Compiler under project properties. And they look like I previously configured them (while the above two new entries are not configured).
The icon of my #Configured object file looks like any other file (i.e. no indication of any aspect or such, which I think there should be). The file name is MailNotification.java (and not .aj), but I guess it should still work as I'm using a Spring annotation for AspectJ?
I haven't found any tutorial or similar which teaches: How to turn a Spring web application project into an AspectJ project and weave aspects into the files using the AJDT plugin, all within Eclipse 3.4. If there is anything like that out there I would be very interested in knowing about it.
What I would like to know:
Where to go from here? I just want to use the #Configured annotation of Spring. I'm also using #Transactional which I think also needs AspectJ.
If it is possible I would like to study AspectJ as little as possible as long as my needs are met. The subject seems interesting, but huge, all I want to do is use the above two mentioned Spring annotations.
*** Exception when Tomcat 6 is started:
Caused by: java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-agent.jar
at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:82)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
... 41 more
Have you added spring-aspects.jar to your aspect path for the project?
In the project properties, under 'AspectJ Build' -> 'Aspect Path' try adding spring-aspects.jar and clean building the project.
Sorry you might have already done this - but you didn't mention it.
It looks like the compile time weaving isn't working. Try adding the below lines to your applicationcontext.xml
<context:load-time-weaver />
<context:spring-configured/>
You'll probably want to add the following xsd to the xml file also
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
For details see here:
http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-aj-ltw
You can use #Transactional without AspectJ. Your configuration file should contain something like following to make it work:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd"
>
<tx:annotation-driven/>
tells spring to look for #transactional annotations when creating instances of configured beans. On finding such annotation, spring returns a dynamic proxy of the bean to the application code. This dynamic proxy ensures that whenever the annotated methods are called, spring is able to intercept it to provide intended transactional behavior. But the proxy-based AOP mandates that you code against interfaces and not concrete classes.

Categories