i want to validate some files looking like this:
<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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd />
...
<bean>...</bean>
...
</beans>
Some of them have more xmlns and locations and some less. Someone knows if there is any api where u just give in such files and it will validate it in java?
greetings
Spring will validate spring configuration files. You can write a junit that loads your configuration to test if the configuration files are formatted correctly.
There you go.
http://download.oracle.com/javase/1,5,0/docs/api/javax/xml/validation/package-summary.html
You can wrap it and make parser that will first extract schemas, then use them for validation.
Related
I'm exploring cache in spring mvc framework. Following the guide on internet like here or here, i'm configuring:
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
... other configuration are omitted....
</beans>
Before adding configuration for cache, my web app run normally, now, my web app start with error:
java.lang.NoClassDefFoundError: org/springframework/cache/interceptor/CacheInterceptor
Not sure what i did wrong?
A NoClassDefFoundError is simply an exception that is thrown when java can't find a certain class that another library uses.
All this means is that when it gets thrown you just need to get it on the classpath. You can make sure it's there by going to libraries-> maven dependencies(if you are using maven) -> and then go to the location of the noclassdef that is being thrown, in your case it would be at org/springframework/cache/interceptor/CacheInterceptor, this is just so you know why you are getting this error, you should not see it now.
As for how to solve it, this could be happening because of 3 things:
Your maven dependencies are not on the deployment assemblies(libraries are in the project, but not on the server), although probably spring wouldn't work at all if this was the case.
You don't have the dependency, add this to pom.xml:
org.springframework
spring-context
4.1.4.RELEASE
You have conflicting spring versions, make all version tags the same.
I am going to change ldap config of another program (its called openkm) which includes editing a spring xml file like this:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="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
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<security:ldap-server id="ldapServer"
url="ldap://192.168.0.6:389/DC=ldap,dc=weyler,dc=local"
manager-dn="CN=Administrator,cn=users,dc=weyler,dc=local"
manager-password="password"/>
<security:authentication-manager alias="authenticationManager">
<security:ldap-authentication-provider
server-ref="ldapServer"
user-search-base="cn=Users"
user-search-filter="(sAMAccountName={0})"
group-search-base="cn=Users"
group-search-filter="(member={0})"
group-role-attribute="cn"
role-prefix="none">
</security:ldap-authentication-provider>
</security:authentication-manager>
</beans:beans>
The configuration just concerns to replace existing values (for example to change ip, port or baseDn).
Using replace all with regex is not reliable and Dom xml parser is a mess for this big xml file. How else could this be done?
Use Spring PropertyPlaceHolderConfigurer to externalize these configurations into a property file and access the same from your XML. An example here.
And then you can update your property file programmetically with apache common properties or any other property writer.
This way it will be far efficient and cleaner than to manipulate XMLs.
im using spring for my java web app. the site has got bigger and i would like to set some configurations.
i have been researching and came across things like document builder factory, replacing spring xml with java config and others. i dunno where to start.
im thinking of implementing the configurations in xml (WEB/newConfig.xml) and have it read by the java beans. basically i wanna input my cofiguration values into xml and have it load by a java bean so that i can use it in controllers and jstl.
im just giving some examples here. for example xml configurations:
<property name="numberOfCars" value="3" />
<property name="webSiteName" value="New Spring Web App" />
....
and i read it in my java class:
class Config {
public getNumberOfCars() {
return numOfCars;
}
public getWebSiteName() {
return webSiteName;
}
}
where should i start and what online materials can i read?
==============================
update
here is what i have created.
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.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 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="/WEB-INF/your_prop_file.properties" />
<bean id="ConfigMgr" class="org.domain.class.ConfigMgr">
<property name="username" value="${username}">
</bean>
</beans>
you_prop_file.properties
username=hello world name
ConfigMgr.java
public class ConfigMgr {
private String username;
...getter
...setter
}
in my controller, here is what i did:
ConfigMgr config = new ConfigMgr();
sysout.out.println(config.getUsername());
i am getting null and i am sure im missing something here. where should i set the username value to the ConfigMgr class?
Spring Java configuration is a newer feature that allows you to configure your Spring application using Java classes instead of XML files. Its just an alternative for XML configuration. XML way is equally feature rich.
From what I could figure out from your problem, you want to move the hardcoded values of params (numberOfCars,webSiteName.. ) outisde your configuration file.
If that is the case, you don't have to go that far.
Just use :-
<context:property-placeholder location="classpath:your_prop_file.properties" />
in your spring xml file and replace the param values like:-
<property name="webSiteName" value="${website.name}" />
You need to have a your_prop_file.properties file in your classpath with enteries like:-
website.name=New Spring Web App
You are not injecting the ConfigMgr bean that you created in XML file.
What you are doing is you are creating a new Object in controller which does not have a clue about properties file.
Now you can inject it using either #Autowired inside your controller or through xml configuration.
There are plenty of examples available in google on basic spring dependency injection.
I tried everything I can think of, but nothing seems to work.
I am using the current version of Spring Framework (3.2) in my java web application.
Every time I start my project I get the following error:
cvc-elt.1: Cannot find the declaration of element 'beans'
this is 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
</beans>
I tried to :
change the version of the schema (3.2, 2.0...)
copy the schema from the jar into WEB-INF
change unix EOL into windows EOL
and nothing seems to work, except using the DTD declaretion instead of the XSD.
What should I do?
Spring is having trouble finding the xsds. Take a look at this SO post. If you are sure you have the required spring-beans jar in your classpath, you may have a corrupted META-INF/spring.schemas file. The spring.schemas file tells spring which classpath to use to find the corresponding xsd file in the spring jars. I have had this occur when using the maven-shade plugin improperly configured.
The xsd came down fine for me when I pasted it in the browser.
This one works fine for me:
<?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="example"/>
</beans>
I am working on struts2 application with spring for back end.
We are using database.properties file and the entries are as follows:
jdbc.url=jdbc:mysql://localhost:3306/myDb
jdbc.username=root
jdbc.password=rooooot
jdbc.csvlocation=C:\myCSV
I added the following new entry in database.properties
enhancePerf.Flag=true
In applicationcontext.xml I am fetching the value like this :-
<bean id="userLogin" scope="prototype"
class="com.hello.something.actions.UserLoginAction">
<property name="perfEnhance" value="${enhancePerf.Flag}"/>
</bean>
After declaring a global variable perfEnhance in UserLoginAction, and forming the setters and getters method of the same, I'm still not getting the value.
I followed the following link:-
http://www.roseindia.net/tutorial/spring/spring3/web/applicationcontext.xml-properties-file.html
Please advise.
Instead of your PropertyPlaceholderConfigurer bean, put:
<context:property-placeholder location="classpath:path/to/database.properties"
ignore-unresolvable="false"/>
This way if the property is not found, it will complain. Otherwise it seems that you may have another "database.properties" file in your classpath, that simply does not have such a property.
Make sure that "path/to/database.properties" is in your classpath. If database.properties itself is your class path, then no "path/to" is needed => just classpath:database.properties
You also have to configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, as well as you have to use bean names in Struts config. If you have the following in your struts-config.xml file:
<action path="/users" .../>
You must define that Action's bean with the "/users" name in action-servlet.xml:
<bean name="/users" .../>
Please take a look at Spring Struts Integration from official Spring's docs.
EDIT to answer the comment:
context is an XML namespace that should be defined in the XML file where it is used:
<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:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util.xsd">