NoSuchBeanDefinitionException since Spring 3.1.3 - exception

I played around with the Spring version in my application today. I started out at Spring 3.1.1 and when I moved to Spring 3.1.3 my application began throwing a NoSuchBeanDefinitionException during startup.
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
matching bean of type
[nl.my.product.properties.Properties] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {}
I configured the Properties bean in the application context like this:
<!-- Gather properties from environment.properties in a map -->
<bean id="systemProperties" class="nl.my.product.properties.Properties">
<property name="propertyMap">
<map>
<entry key="email.from.address" value="${email.from.address}"/>
<entry key="site.host" value="${site.host}"/>
<entry key="site.protocol" value="${site.protocol}"/>
...
</map>
</property>
</bean>
And I autowired the bean into many of my controllers and services, like this:
#Autowired
public ArticleController(ArticleService articleService, PageBlockService pageBlockService, Properties systemProperties) {
this.articleService = articleService;
this.pageBlockService = pageBlockService;
this.properties = systemProperties;
}
Any clue as to why Spring is unable to find my bean since Spring 3.1.3? I tried moving the Properties bean to a package to was not also scanned for components, but that doesn't solve the problem.
When I annotate the Properties bean with #Component Spring does pick it up, but then the properties map is empty.

It started working when I removed the id="systemProperties" from the bean definition

Related

How to test a class contains #Lazy #autowired bean in spring JUnit

I have a class in spring project which contains #Autowire bean with #Lazy annotation.
At run time I am getting below errors while testing the class using junit.
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.context.annotation.Lazy(value=true)}

Reading method-level properties using JUnit + spring-test

What is the best way to use spring-test with JUnit to read property files specific to a test method?
The following bean illustrates the intent to search method-level, then class-level, then package-level, then root-level property files:
<bean name="properties" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" >
<property name="locations">
<list>
<value>classpath:properties.xml</value>
<value>classpath:*/properties.xml</value>
<value>classpath:*/*/properties.xml</value>
<value>classpath:*/*/*.properties.xml</value>
</list>
</property>
</bean>
I've considered using/subclassing PropertySourcesPlaceholderConfigurer or SpringMethodRule, but so far, neither a viable nor elegant solution has occurred to me. spring-test offers a variety of promising class-level annotations, but the stumbling block is that I want to be able to find a method-specific file, e.g.:
classpath:myPackage/myClass/myMethod.properties.xml
If you are just looking for an algorithm that helps you to look up classpath resources based on the current method, you can take inspiration from the ServletTestExecutionListener in the Spring TestContext Framework, specifically the detectDefaultScript() method.
Regards,
Sam (author of the Spring TestContext Framework)

Issue upgrading from Jackson 1.9 to 2.5 using Spring 3.1.2 - ProviderBase class not found

I'm trying to upgrade a current project from Jackson 1.9 to 2.5. Everything was going well until I tried to startup my WAS 7 server and receive this error:
org.springframework.beans.factory.CannotLoadBeanClassException: Error
loading class
[com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider] for bean
with name 'jaxbProvider' defined in ServletContext resource
[/WEB-INF/spring/applicationContext-configuration.xml]: problem with
class file or dependent class; nested exception is
java.lang.NoClassDefFoundError:
com.fasterxml.jackson.jaxrs.base.ProviderBase
This appears to be in relation to trying to register the Jackson Provider in my web.xml below:
<!-- Jackson Provider -->
<bean id="jaxbProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" >
<property name="mapper" ref="jacksonObjectMapper"/>
</bean>
<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" >
<property name="annotationIntrospector" ref="jacksonAnnotationIntrospector"></property>
</bean>
<bean id="jacksonAnnotationIntrospector" class="com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair" >
<constructor-arg ref="primaryAnnotationIntrospector" />
<constructor-arg ref="secondaryAnnotationIntrospector" />
</bean>
<bean id="primaryAnnotationIntrospector" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector" />
<bean id="secondaryAnnotationIntrospector" class="com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector" />
I have the following jars on my classpath:
jackson-annotations-2.5.0.jar
jackson-core-2.5.0.jar
jackson-databind-2.5.0.jar
jackson-jaxrs-json-provider-2.5.0.jar
jackson-module-jaxb-annotations.2.5.0.jar
Now from my understanding its looking for this fellow:
com.fasterxml.jackson.jaxrs.base.ProviderBase
but can't find it. A google search reveals that class as belonging to a
jackson-jaxrs-provider project, but I can't find a specific jar for that. I think that's because that is just a base for the jackson-jaxrs-json-provider.2.5.0.jar that I already included. So shouldn't it inherently be able to see that base class through the jackson-jaxrs-json-provider.2.5.0.jar??
If anyone has an idea of what could be wrong I would be very appreciative!
Thanks.
If you used Maven, adding jackson-jaxrs-json-provider as a dependency, you will see all the following pulled in
(I had an image from another post with v2.2.3- disregard the version)
As you can see, it does depend on a jackson-jaxrs-base, which is where the ProviderBase is located.
You can download it here (just click the 2.5.0, then the Download Bundle)

Using SkipListener which implements Ordered with scope="step": No context holder available for step scope

When using a skipListener with scope="step" which implements Ordered I get the following exception:
Error creating bean with name 'scopedTarget.scopedTarget.fisalisBatchMeldungenListener': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
If I don't let my listener implement Ordered the exception does not ocure.
Here is my Bean Definition:
<bean id="erzeuger" class="...." scope="step">
<property name="user" ref="user" />
<property name="termin" ref="termin" />
<property name="services" ref="services"
</bean>
<bean id="listener" class="Listenerclass" scope="step">
<property name="erzeuger" ref="erzeuger" />
</bean>
And here is the part where I am using it:
<step id="myStep" parent="abstractBatchStep" xmlns="http://www.springframework.org/schema/batch">
<listeners merge="true">
<listener ref="listener" />
</listeners>
</step>
Has anyone an idea where the problem is?
I have the point:
When the Listener is added to the step, the step checks, if its implements Ordered. If it does, the step adds the listener to a collection which will be immediatly sort by using a comperator. This comperator invokes getOrder(9 on the instance of the listener. But if the listener ist stepScoped, the SpringBatch Factory (s. comment below) has to return the "real" bean instance. But at this point the "real" bean instance does not exist.
For sope="step" read this:
Scoped bean works in different manner. When context is created this bean is wrapped into additional proxy object (by default created by CGLIB), which is passed to the bean that refers it (this proxy is by default singleton, e.g. shared). So each time the method is invoked on the proxy in runtime Spring intersects the call, requests the factory to return the instance of the bean and invokes the method on that bean. The factory in its turn may lookup for "real" bean instance e.g. in HTTP request ("request" scope) or HTTP session ("session" scope) and/or create new instance if necessary. Late instantiation allows to initialize the scoped bean with "runtime" (scope) values, e.g. values from HTTP request/session which are obviously undefined when context was created. In particular "step"-scoped beans are bound to thread local (remember that steps are run in parallel for partitioning). So, scoped beans are dereferred when you call a method on them. Finally one can easily break this elegant Spring "ideology" by calling any method on scoped bean just after it is set to another bean (e.g. in the setter) :)

Spring MVC and JSON, Jackson class not found exception

I cant get Spring's JSON support working. In my spring-servlet.xml file i have included following lines:
<mvc:annotation-driven/>
<context:component-scan base-package="my.packagename.here" />
<context:annotation-config />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
I have also downloaded jackson libraries and added them to my eclipse project and also to WEB-INF/lib folder. When sending request to controller with jQuery getJSON method i get following errors:
javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonProcessingException
java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcessingException
What do you think is the problem. I'm guessing it has something to do with my spring-servlet.xml file. I can paste entire error log, if you need.
For Jackson v2 jars, class to be used for bean should be
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
For older jackson version, org.springframework.http.converter.json.MappingJacksonHttpMessageConverter is ok. Make sure the jar files are added to the project library.
The answer of anshul tiwari partially captures the problem. Here is a more complete answer ...
When Jackson made it to version 2.0, the core library got changed from jackson-core-asl-x.x.x.jar to jackson-core-x.x.x.jar. With that, the paths changed. In version 1, org.codehaus.jackson was the path. In version 2, it is in com.fasterxml.jackson.core if you were to open up the jar file.
Now if you have the libraries of version 2 and you are seeing the org.codehaus.jackson ClassNotFoundException, it means that there is a mixing of versions. Some code is expecting v1 but you have provided v2. This is certainly possible when using Spring so you have to be careful to choose the correct jar file for your code.
EDIT
In fact, looking at 3.2 Spring source code, org.springframework.http.converter.json.MappingJacksonHttpMessageConverter still references the org.codehaus stuff so this is a case where Spring source code needs to import the correct path and there is nothing you the developer can do to use jackson 2.
JsonProcessingException is part of the jackson-core-asl-x.x.x.jar. Make sure that it's part of your classpath.
Just to complement anshul tiwari answer, the bean tag should go inside mvc:annotation-driver:
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<!-- <bean -->
<!-- class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"
/> -->
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<!-- <bean -->
<!-- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"
/> -->
</mvc:message-converters>
</mvc:annotation-driven>
Use it Like below : Hope it will work..
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"
default-lazy-init="true">
<context:component-scan base-package="com.vc.bmp.resource" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<!-- property name="prefixJson" value="true" />
<property name="supportedMediaTypes" value="application/json" /-->
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>