Define Spring bean with a Map-typed constructor argument

In my Grails app, I'm trying to define a Spring bean in resources.groovy that requires a Map-typed constructor arg. I tried this:

Map<Class, String> mapArg = [(String): 'foo']
myBean(MyBeanImpl, mapArg)

But I get the error message:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBean': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

The implementation class has a single constructor which is defined thus

MyBeanImpl(Map<Class, String> map) {
  // impl omitted 
}

My guess is that the problem is caused by the fact that I've defined a constructor that takes a single Map arg which has the same signature as the default constructor Groovy adds to every class.

If so, a solution would appear to be to add a factory method such as

MyBean getInstance(Map map) {
  // impl omitted  
}

But I'm not sure how I can call this to define a bean (in resources.groovy) that is constructed from a factory method that requires a parameter.

Answers


As far as I can tell the syntax you're using ought to work. Does the alternative syntax:

Map<Class, String> mapArg = [(String): 'foo']
myBean(MyBeanImpl) { bean ->
  bean.constructorArgs = [mapArg]
}

work any better in your case? Failing that, declaring the map as a bean in its own right should definitely do it:

import org.springframework.beans.factory.config.MapFactoryBean

classMap(MapFactoryBean) {
  sourceMap = [(String):'foo']
}

myBean(MyBeanImpl, classMap /* this is a RuntimeBeanReference */)

Need Your Help

Combination of `sleep` and `pause` command

batch-file input sleep simultaneous

Ok, after seeing crazy stuff being completed in so little code, I have high hopes this is possible.

How to abort Python script calling C function using Swig?

python c swig abort

I have a problem trying to abort a Python script, for example:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.