How can I mix and match custom Spring schema types with traditional Spring schema types? - spring

Let's say I have a class Person with properties name and age, and it can be configured with Spring like so:
<beans:bean id="person" class="com.mycompany.Person">
<beans:property name="name" value="Mike"/>
<beans:property name="age" value="38"/>
</beans:bean>
I'd like to have a custom Spring schema element for it, which is easy to do, allowing me to have this in my Spring configuration file:
<Person name="Mike" age="38"/>
The schema definition would look something like this:
<xsd:complexType name="Person">
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="age" type="xsd:int"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
Based on this, let's now say I would like the option of mixing and matching my custom schema element with traditional elements and attributes of Spring beans, so I could have the option of doing this:
<Person name="Mike">
<beans:property name="age" value="38"/>
</Person>
How would I go about doing that? Perhaps this is impossible without some major customization, but I'm hoping there is some fairly simple mechanism to achieve this. I thought extending "bean" might be the trick, but that doesn't look to be correct.

First of, if your example is really all you want to do, consider using p-namespace instead and save yourself some major headache:
<beans:bean id="person" class="com.mycompany.Person" p:name="Mike" p:age="38"/>
Yes, it doesn't look as pretty as <Person name= age= /> but there's no custom code to write :-)
If that does not satisfy your requirements, you're looking at implementing your own namespace / bean definition parser. Spring documentation has a chapter dedicated to that which will explain it better then I can. If you hit any issues, please post back specific questions and I'll try to help.

Related

How to serialize JodaTime using UNIX timestamp format with Jackson

I am using
jackson-annotations-2.4.3.jar
jackson-core-2.4.3.jar
jackson-databind-2.4.3.jar
jackson-datatype-joda-2.4.3.jar
and Spring 3.2.11. I am using the joda time's DateTime format, and i want to serialize beans that have some date-times as properties. What i would like is to serialize only date-time's timestamp. Instead, jackson serializes the whole object, which leads to problems in js afterwards.
What i am trying to achieved worked when using jackson 1.8.3.
I have tried to register a JodaModule to the object mapper for MappingJackson2HttpMessageConverter, by defining this in applicationContext.xml. Even though the joda module is loaded, it doesnt seem to work.
I tried the following config:
<beans:beans>
<beans:bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ">
</beans:bean>
<beans:bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
<beans:property name="arguments">
<beans:list>
<beans:bean class="com.fasterxml.jackson.datatype.joda.JodaModule" />
</beans:list>
</beans:property>
</beans:bean>
</beans:beans>
After that i tried:
What else should i try?
Nobody seems to care about this question. However, i will close it, since i have found the answer: i had to save the configuration in a xml file and import it in multiple xml's so that the config would be visible to all the involved contexts.

How to get Spring Data to read documents from Mongo that don't have _class attributes

MongoTemplate inserts an attribute named "_class" into anything it puts into Mongo, and there are ways to turn that off.
However, it seems to be unwilling to read anything back out of it that doesn't have a _class attribute. Simply removing that from the mongo document appears to make it inaccessible. Since reading data you didn't write yourself is an obvious use case, I figure I must be missing something here.
I've been attempting to use this:
List<SomeClass> list = mongoTemplate.findAll(SomeClass.class, "someCollection");
...where SomeClass is annotated with #Id and #Document, and the documents in someCollection otherwise correctly map to the object. I can verify this by creating one of these objects in code, using insert to get it into Mongo, and then see that I can read it back out again.
This works just fine if _class is there but fails if it is not. I do not care about polymorphism or anything that might actually need this attribute. How can I get MongoTemplate to read data that it didn't itself write?
Okay, I found the answer...DavidA's advice was correct but missing the crucial part: when you set up the MappingMongoConverter dealio with the null type mapping business, it not only stops writing the "_class" pollution, but also stops attempting to read it. This causes it to fall back to the type you provide when attempting to retrieve your documents from Mongo.
I haven't seen anywhere that anyone actually mentions that. :)
So, for anybody else running into this issue, here's the XML configuration I used (adapted from something I found somewhere else here on StackOverflow, but I lost the link, sorry):
<mongo:db-factory id="mongoDbFactory" host="${mongo.host}" port="${mongo.port}" dbname="${mongo.dbname}"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mongoConverter" />
<property name="writeResultChecking" value="EXCEPTION" />
</bean>
<bean id="mongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id="mongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mongoMappingContext" />
<property name="typeMapper" ref="mongoTypeMapper"></property>
</bean>
And then in the Java code:
//build query object
UnifiedProduct mpp = mongoTemplate.findOne(query, UnifiedProduct.class, "collection-name");
...which results in the UnifiedProduct class I wanted, and no annoying "_class" pollution.
Spring's MappingMongoConverter uses a MongoTypeMapper in order to figure out what type to use when reading in a DBObject from the database. The DefaultMongoTypeMapper uses the "_class" attribute to work as you have described.
You should be able to implement your own MongoTypeMapper and tell the MappingMongoConberter to use it. Your version can use other indicators instead of "_class" value to determine what type should be created when reading.

request definition in wadl file

Is there any way the wadl can tells the request type. For example the following PUT method expect a xml data type of "setBlockRequest", is there any way I can reference it to the xml schema(xsd file) to define the content of "setBlockRequest"?
This wadl is generated by Jersey.
<resource path="/appliance/{device_id}/update_multiple_values">
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="device_id" style="template" type="xs:string"/>
<method id="setBlockValue" name="PUT">
<request>
<ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="setBlockRequest" mediaType="application/xml"/>
</request>
</method>
</resource>}
yes, there is. See http://www.w3.org/Submission/wadl/#x3-40001.3 (<grammars> tag).
Jersey now generated it automatically for you but only for cases where your type is annotated with JAXB annotations. (This is valid since Jersey 1.13 if I remember correctly, so you might want to give it a try).

Dynamically configuring java beans based on property file in Spring

Wondering if there is a way to dynamically instantiate beans based on set of values in your property file using PropertyPlaceholderConfigurer class.
I have a java bean say Student with two attributes: "name" and "subject"
I have a property file with:
student.1.name=student1name
student.1.subject=student1subject
student.2.name=student2name
student.2.name=student2subject
Now I have a Classroom object that can take a list of students.
I am wondering if there is a way we could do this using Spring. The challenge here is that the number of students could vary.
If there was only one student object then:
<bean id="student" class="com.abc.Student">
<property name="name" value="${student.1.name}" />
<property name="subject"
value="${student.1.subject}" />
</bean>
<bean id="classRoom" class="com.abc.ClassRoom">
<property name="student" ref="student" />
</bean>
would have worked. But in this case we have a list of n Students. And the value of n could vary depending on the number of entries in the properties file.
I'm with Kevin--IMO you're going about this the wrong way.
One possible workaround would be to create a bean that takes the property file as an argument, reads it in, and exposes a list of students (which would need to be indexed on something, like the n in the existing property file).
The classroom bean could then use that list of students.
But it sure looks like you're trying to duplicate the functionality of a DB, without a DB, in an awkward way.
I don't think there's a way to do that with PropertyPlaceholderConfigurer. Usually when I have a situation like that I choose a configuration format of either JSON or XML and use GSON/Jackson/JAXB to unmarshall the data into objects.

<security:custom-authentication-provider /> means?

i have a bean in xml like below
<bean id="theCustomAuthenticationProvider" class="test.custom.CustomAuthenticationProvider">
<security:custom-authentication-provider />
a.may i know what does security:custom-authentication-provider means when i put it in my bean like above?
b. do i need to create
<bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager"> ref to theCustomAuthenticationProvider
in the xml ?
c. if b. answer is yes, alternatively, can i use ref of theCustomAuthenticationProviderinside tag?
The idea is that marking a bean:
<security:custom-authentication-provider />
Will registor the bean as a authentication provider with the AuthenicationManager provided by spring security. You don't need b.

Resources