ConversionServiceFactoryBean Does Not Work - java

I'm working with a special CacheConfig object that holds a field (with standard getter/setter methods), accessExpirationValue, which is of type java.time.Duration. (EDIT: actually, the field is of type Long (the number of seconds), but the getter and setter are of type Duration.)
I'm trying to wire this in Spring by setting this value as a number of seconds and using a ConversionServiceFactoryBean, like so:
Relevant beans in ApplicationContext.xml:
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean" >
<property name="converters">
<set>
<bean
class="com.tjamesboone.example.config.SecondsToDurationConverter"/>
</set>
</property>
</bean>
<bean id="cacheConfig" class="com.tjamesboone.example.cache.CacheConfig" >
<property name="accessExpirationValue" value="0" />
</bean>
SecondsToDurationConverter:
package com.tjamesboone.example.cache;
import java.time.Duration;
import org.springframework.core.convert.converter.Converter;
public class SecondsToDurationConverter implements Converter<String, Duration> {
#Override
public Duration convert(String seconds) {
return Duration.ofSeconds(Long.parseLong(seconds));
}
}
Now, as I understand it, this is supposed to just work. When I pass in "0" for the value of accessExpirationValue, the fact that I've declared a conversionService bean that handles converting Strings to Durations means that is should set the value as a Duration of zero length.
But this would be to easy. And it is. Because when I test my application (using SpringJUnit4ClassRunner), I get this error, as if I'd never registered a Converter:
Bean property 'accessExpirationValue' is not writable or has an
invalid setter method. Does the parameter type of the setter match
the return type of the getter?
So my question is, What am I doing wrong? How do I get this to work they way I want?
For reference, this is the primary documentation I've been using:
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html#core-convert-Spring-config
It specifically says,
In a Spring application, you typically configure a ConversionService instance per Spring container (or ApplicationContext). That ConversionService will be picked up by Spring and then used whenever a type conversion needs to be performed by the framework.
EDIT:
I should probably also post the relevant part of CacheConfig!
package com.tjamesboone.example.config;
import java.time.Duration;
public class CacheConfig {
private Long accessExpirationValue;
public Duration getAccessExpiration() {
return Duration.ofSeconds(accessExpirationValue.intValue);
}
public void setAccessExpiration() {
this.accessExpirationValue = expirationDuration.getSeconds();
}
}

Spring is going to try to match the properties in your bean with the getters and setters specified in your class.
Your getters/setters are currently getAccessExpiration() but should be getAccessExpirationValue() to match your bean property name="accessExpirationValue". Change one or the other and you should have it.

Related

Can you assign a value directly to a bean in Spring?

I am trying to reproduce an assignment in Java code with an equivalent bean definition in Spring. As far as I can tell, though, Spring only lets you assign values to the fields within an object (provided that the class defines the appropriate setter methods). Is there a way to simply capture a reference to an object using Spring beans?
Here's an example of how I would expect this to work:
<!-- Non-working example. -->
<bean id="string" class="java.lang.String">
<value>"I am a string."</value>
</bean>
I realize that in this particular case I could just use a <constructor-arg>, but I'm looking for more general solution, one that also works for classes that don't provide parameterized constructors.
String class is immutable. No property setter method is available in java.lang.String class. If you want to inject the property value you can use below:
<bean id="emp" class="com.org.emp">
<property name="name" value="Alex" />
</bean>
in above for the obj emp, its name property will be set as Alex.
The thing to use here is a factory-method, possibly in conjunction with a factory-bean. (Non-static functions must be instantiated by a bean of the appropriate type.) In my example problem, I wanted to capture the output of a function that returns a String. Let's say the function looks like this:
class StringReturner {
public String gimmeUhString(String inStr) {
return "Your string is: " + instr;
}
}
First I need to create a bean of type StringReturner.
<bean name="stringReturner" class="how.do.i.java.StringReturner" />
Then I instantiate my String bean by calling the desired function as a factory-method. You can even provide parameters to the factory method using <constructor-arg> elements:
<bean id="string" factory-bean="stringReturner" factory-method="gimmeUhString">
<constructor-arg>
<value>I am a string.</value>
</constructor-arg>
</bean>
This is (for my purposes) equivalent to saying:
StringReturner stringReturner = new StringReturner();
String string = stringReturner.gimmeUhString("I am a string.");
String is not a Bean, Bean is a Objet that his Class that have a Constructor with empty arguments and the properties are accessible by Getters Methods and are modifiable by Setters Methods

Spring Data, Mongo, and #TypeAlias: reading not working

The issue
Awhile back I started using MongoDB and Spring Data. I'd left most of the default functionality in place, and so all of my documents were stored in MongoDB with a _class field pointing to the entity's fully-qualified class name.
Right away that didn't "smell" right to me, but I left it alone. Until recently, when I refactored a bunch of code, and suddenly none of my documents could be read back from MongoDB and converted into their (refactored/renamed) Java entities. I quickly realized that it was because there was now a fully-qualified-classname mismatch. I also quickly realized that--given that I might refactor again sometime in the future--if I didn't want all of my data to become unusable I'd need to figure something else out.
What I've tried
So that's what I'm doing, but I've hit a wall. I think that I need to do the following:
Annotate each entity with #TypeAlias("ta") where "ta" is a unique, stable string.
Configure and use a different TypeInformationMapper for Spring Data to use when converting my documents back into their Java entities; it needs to know, for example, that a type-alias of "widget.foo" refers to com.myapp.document.FooWidget.
I determined that I should use a TypeInformationMapper of type org.springframework.data.convert.MappingContextTypeInformationMapper. Supposedly a MappingContextTypeInformationMapper will scan my entities/documents to find #TypeAlias'ed documents and store an alias->to->class mapping. But I can't pass that to my MappingMongoConverter; I have to pass a subtype of MongoTypeMapper. So I am configuring a DefaultMongoTypeMapper, and passing a List of one MappingContextTypeInformationMapper as its "mappers" constructor arg.
Code
Here's the relevant part of my spring XML config:
<bean id="mongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey" value="_class"></constructor-arg>
<constructor-arg name="mappers">
<list>
<ref bean="mappingContextTypeMapper" />
</list>
</constructor-arg>
</bean>
<bean id="mappingContextTypeMapper" class="org.springframework.data.convert.MappingContextTypeInformationMapper">
<constructor-arg ref="mappingContext" />
</bean>
<bean id="mappingMongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg ref="mongoDbFactory" />
<constructor-arg ref="mappingContext" />
<property name="mapKeyDotReplacement" value="__dot__" />
<property name="typeMapper" ref="mongoTypeMapper"/>
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory" />
<constructor-arg ref="mappingMongoConverter" />
</bean>
Here's a sample entity/document:
#Document(collection="widget")
#TypeAlias("widget.foo")
public class FooWidget extends Widget {
// ...
}
One important note is that any such "Widget" entity is stored as a nested document in Mongo. So in reality you won't really find a populated "Widget" collection in my MongoDB instance. Instead, a higher-level "Page" class can contain multiple "widgets" like so:
#Document(collection="page")
#TypeAlias("page")
public class Page extends BaseDocument {
// ...
private List<Widget> widgets = new ArrayList<Widget>();
}
The error I'm stuck on
What happens is that I can save a Page along with a number of nested Widgets in Mongo. But when I try to read said Page back out, I get something like the following:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.myapp.document.Widget]: Is it an abstract class?
I can indeed see pages in Mongo containing "_class" : "page", with nested widgets also containing "_class" : "widget.foo" It just appears like the mapping is not being applied in the reverse.
Is there anything I might be missing?
In the default setting, the MappingMongoConverter creates a DefaultMongoTypeMapper which in turn creates a MappingContextTypeInformationMapper.
That last class is the one responsible for maintaining the typeMap cache between TypeInformation and aliases.
That cache is populated in two places:
In the constructor, for each mappingContext.getPersistentEntities()
When writing an object of an aliased type.
So if you want to make sure the alias is recognized in any context, you need to make sure that all your aliased entities are part of mappingContext.getPersistentEntities().
How you do that depends on your configuration. For instance:
if you're using AbstractMongoConfiguration, you can overwrite its getMappingBasePackage() to return the name of a package containing all of your entities.
if you're using spring boot, you can use #EntityScan to declare which packages to scan for entities
in any case, you can always configure it with a custom set (from a static list or a custom scan) using mongoMappingContext.setInitialEntitySet()
One side note, for an entity to be discovered by a scan, it has to be annotated with either #Document or #Persitent.
More informations can be found in the spring-data-commons Developer Guide
I spent a bunch of time with my debugger and the Spring Data source code, and I learned that Spring Data isn't as good as it probably should be with polymorphism as it should be, especially given the schema-less nature of NoSQL solutions like MongoDB. But ultimately what I did was to write my own type mapper, and that wasn't too tough.
The main problem was that, when reading in my Page document, the default mappers used by Spring Data would see a collection called widgets, then consult the Page class to determine that widgets pointed to a List, then consult the Widget class to look for #TypeAlias information. What I needed instead was a mapper that scanned my persistent entities up front and stored an alias-to-class mapping for later use. That's what my custom type mapper does.
I wrote a blog post discussing the details.
If you extend AbstractMongoConfiguration, you can override method getMappingBasePackage to specify the base package for your documents.
#Configuration
class RepositoryConfig extends AbstractMongoConfiguration {
#Override
protected String getMappingBasePackage() {
return "com.example";
}
}
Update: In spring-data-mongodb 2+ you should use:
#Configuration
class RepositoryConfig extends AbstractMongoConfiguration {
#Override
protected Collection<String> getMappingBasePackages(){
return Arrays.asList("com.example");
}
}
because getMappingBasePackage() is no deprecated and won't work.
Today I ran into the exact same issue. After more research I found out that my subclass was missing a repository. It appears that Spring Data is using the repositories to determine which concrete subclass to create and when it is missing, it falls back to the superclass which in this case is abstract.
So please try to add a FooWidgetRepository and map it to FooWidget with correct ID type. It might work in your case as well.
If you use spring boot with auto-configuration, declaring the following bean can help:
#Bean
MongoMappingContext mongoMappingContext(ApplicationContext applicationContext, MongoCustomConversions conversions) throws ClassNotFoundException {
MongoMappingContext context = new MongoMappingContext();
context.setInitialEntitySet(new EntityScanner(applicationContext).scan(Persistent.class));
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
return context;
}
what does the trick is the following line:
new EntityScanner(applicationContext).scan(Persistent.class)
Instead of scanning for Documents it will scan for both Document and TypeAlias, since both of these annotations are Persistent
Andreas Svensson is right, this can be done much simpler than described by Dave Taubler.
I posted a slightly more elaborate answer than Andreas' (including sample code) in this post. Excerpt:
So all you need to do is to declare an "unused" Repository-Interface
for your sub-classes, just like you proposed as "unsafe" in your OP:
public interface NodeRepository extends MongoRepository<Node, String> {
// all of your repo methods go here
Node findById(String id);
Node findFirst100ByNodeType(String nodeType);
... etc.
}
public interface LeafType1Repository extends MongoRepository<LeafType1, String> {
// leave empty
}
public interface LeafType2Repository extends MongoRepository<LeafType2, String> {
// leave empty
}

What is a reason of warning: Autowired annotation is not supported on static fields

As well as I know, it is not possible to use #Autowired annotation for static fields using Spring. When I run my JBoss server, everything works well but I can see few warnings:
Autowired annotation is not supported on static fields: private static ...
I do not use this annotation really often (in this project I have not used it yet) but maybe some of my colleagues found it more useful.
So, I took one of this warnings:
Autowired annotation is not supported on static fields: private static java.lang.String com.my.package.MyClass.myVariable
I opened this class and inside is:
#Value("${params.myvariable}")
private static String myVariable;
// getter
// setter
I opened config.xml:
<bean id="myid" class="com.my.package.MyClass">
<property name="myVariable" value="${params.myvariable}" />
</bean>
As a last thing, I searched in Eclipse of Autowired strings in my project. Size of the result set was 0.
So my question is, what is a reason of warning:
Autowired annotation is not supported on static fields
in this case?
Thank you in advance
This
<bean id="myid" class="com.my.package.MyClass">
<property name="myVariable" value="${params.myvariable}" />
</bean>
means that you have a class MyClass such as
class MyClass {
...
public void setMyVariable(String value) {
...
}
}
With what you describe, the ... can be replaced with your static field
private static String myVariable;
// in setter method
myVariable = value;
This is a workaround to autowire a static field, which normally cannot be done. Spring works on beans (instances), not on classes.

Is it possible to provide value to API present class using Spring

Is there anyway so that I can provide parameter to API( not to the member of class) using Spring?
I know I can pass result of one API call to member of class
<bean id="registryService" class="foo.MyRegistry">
...properties set etc...
</bean>
<bean id="MyClient" class="foo.MyClient">
<property name="endPoint" value="#{registryService.getEndPoint('bar')}"/>
</bean>
But, I want to pass the value to API( Basically I am trying to add ActionListener on JButton from spring)
Not really a spring expert but...
In Spring 3.
#Value("#{properties.getAppropriateActionListener()}")
public void setActionListener(ActionListener listener) {
myJButton.setActionListener(listener);
}
Also, I think Spring expects a setEndpoint() and getEndPoint() methods to be able to resolve the property which is named "endPoint". Declaring a property like that effectively passes the value to the setEndPoint() method. So passing a value to API (which I assume is invoking a method call) is actually pretty straightforward.

Spring list beans by type

Is there a way in Spring that I can auto populate a list with all of beans of a type AND any of its subtypes? I have a setter method that looks like:
setMyProp(List<MyType> list)
And I would like to autowire in any beans of MyType and all subclasses of MyType.
Thanks,
Jeff
Yup, you can do this. The spring docs say:
It is also possible to provide all
beans of a particular type from the
ApplicationContext by adding the
annotation to a field or method that
expects an array of that type.
Note that it says you need to expect an array, not a list. This makes sense, because generic type erasure means a list may not work at runtime. However, take the following unit test, which works:
<?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-2.5.xsd">
<bean class="test.Test.TypeB"/>
<bean class="test.Test.TypeC"/>
<bean class="test.Test.TypeD"/>
</beans>
and this unit test:
package test;
#ContextConfiguration
#RunWith(SpringJUnit4ClassRunner.class)
public class Test {
private #Autowired List<TypeA> beans;
#org.junit.Test
public void test() {
assertNotNull(beans);
assertEquals(2, beans.size());
for (TypeA bean : beans) {
assertTrue(bean instanceof TypeA);
}
}
public static interface TypeA {}
public static class TypeB implements TypeA {}
public static class TypeC extends TypeB {}
public static class TypeD {}
}
So officially, you're supposed to autowire TypeA[], not List<TypeA>, but the List works good.
If it's acceptable to fill the list from your application code and not from within the bean definition file you could use the org.springframework.beans.factory.xml.XmlBeanFactory and ask it "getBeansOfType( MyType.class )". This gives you all beans of type (and subtype) of MyType.
If you can use #Autowired inside the code to be populated you can safely use the way mentioned by skaffman. If you insist on XML configuration there is a small library called Hera to achieve this. Esentially configuration of a scenario described by you looks like this:
<bean id="client" class="..">
<property name="injectDynamicListHere">
<hera:list class="my.custom.SuperType" />
</property>
</bean>
This will inject all top-level Spring beans implementing SuperType as List into the client bean.
Short answer: no.
Long answer: Java generics work by type erasure, meaning that at runtime that parameter is simply a List, not a List of a generic type. As such you can't figure out that it is meant to be of parameter type MyType so it wouldn't be possible to implement this behaviour.
That being said, there are alternative ways of doing this. The most obvious seems to be to listen for bean creation and then seeing if they are of MyType (or subclasses) and then keeping a reference.
There is probably a few ways to do this. One is creating a bean post-processor. This way you'll get notified of every bean that's created.
Due to legacy code and missing #Autowired I solve it like:
MyBean implements ApplicationContextAware{
#Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
final Map<String, HttpRequestHandlerTemplate> beansOfType = ctx.getBeansOfType(RequiredBean.class);
...
}

Categories

Resources