why autowired variable in prototype scope works only in the first instance - spring

I am trying to Autowired RestTemplate and I am using the suggestion from why can I autowire or inject pojo but I can't autowire or inject RestTemplate . Unfortunetlly I am facing a problem from the second instantiation. The variable restTemplate works only once. After that, it is null. I guess that I might have done some mistake in dispatcher-servlet or web.xml config.
I know that the scope prototype creates a new instance every time it is called but I wasn't expect the auto wired RestTemplate to be null in the second instance. All others autowired variables runs perfectly every calls.
I saw some similiar threads and all of them ends up with some solution related to create a scope proxy. I changed to #Scope(value="prototype", proxyMode = ScopedProxyMode.TARGET_CLASS) but I got the error "Can only specify arguments for the getBean method when referring to a prototype bean definition". Honestly I can't imagine this as the right direction for my case as I have problem only with restTemplate and in the second evoke of context.getBean.
//the process starts here
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
display = context.getBean(Lo_DisplayHandler.class, request, response);
display.lastPage();
DisplayHandler
#Component
#Scope("prototype")
public class Lo_DisplayHandler extends Lo_Handler {
//this constructor runs twice when the application start
public Lo_DisplayHandler() {
super();
}
this constructor runs every time the bean is get from context (context.getBean(Lo_DisplayHandler.class, request, response)). The first time Lo_DisplayHandler is created restTemplate is filled in but from the second time forward restTemplate is null
public Lo_DisplayHandler(HttpServletRequest request,HttpServletResponse response) {
super();
//get DB2 connection and set some data in session
}
#Autowired
private RestTemplate restTemplate;
//restTemplate will exist only in the first time
LogDisplay _l = restTemplate.postForObject(RestProperties.getUrl() + RestProperties.getFirstpage(),_mas60010, LogDisplay.class);
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="com.myCompany.mhe.common.controller, com.myCompany.mhe.log.handler, com.myCompany.mhe.utilities, com.myCompany.mhe.log.domain, com.myCompany.mhe.log.storedprocedure" />
<context:property-placeholder location="classpath:restServices.properties"/>
<mvc:resources mapping="/**" location="/" />
<!—-
If take the next three beans definition from here to applicationContext.xml and change contextConfigLocation to point to applicationContext.xml instead of mvc-dispatcher-servlet.xml my controller stops to work
-->
<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonObjectMapper" />
<property name="targetMethod" value="configure" />
<property name="arguments">
<list>
<value type="com.fasterxml.jackson.databind.DeserializationFeature">READ_DATE_TIMESTAMPS_AS_NANOSECONDS</value>
<value>true</value>
</list>
</property>
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
</beans>
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
//Exception after default constructor removed
[WARNING ] Exception encountered during context initialization - cancelling refresh attempt
Error creating bean with name 'lo_Controller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.mhe.Mhe_Handler com.mycompany.mhe.common.controller.Lo_Controller.mhe_Handler; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lo_DisplayHandler' defined in file [C:\STS\wsRestTemplate\MHE_original\WebContent\WEB-INF\classes\com\mycompany\mhe\log\handler\Lo_DisplayHandler.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mycompany.mhe.log.handler.Lo_DisplayHandler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.mycompany.mhe.log.handler.Lo_DisplayHandler.<init>()
[ERROR ] Context initialization failed
Error creating bean with name 'lo_Controller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.mhe.Mhe_Handler com.mycompany.mhe.common.controller.Lo_Controller.mhe_Handler; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lo_DisplayHandler' defined in file [C:\STS\wsRestTemplate\MHE_original\WebContent\WEB-INF\classes\com\mycompany\mhe\log\handler\Lo_DisplayHandler.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mycompany.mhe.log.handler.Lo_DisplayHandler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.mycompany.mhe.log.handler.Lo_DisplayHandler.<init>()

Related

Spring NotWritablePropertyException and Invalid property 'lazyInit' of bean JndiObjectFactoryBean

We are using WAS8.5 based JNDI data source configuration. While server start up, this datasource is not getting created. Hence throwing
org.springframework.beans.factory.BeanCreationException by saying "Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'lazyInit' of bean class [org.springframework.jndi.JndiObjectFactoryBean]: Bean property 'lazyInit' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?"
We are not trying to set lazyInit property in our application. What could be the issue here? Is anything missed here?
Spring-context.xml:
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">
<context:annotation-config/>
<jee:jndi-lookup id="ds_app1" jndi-name="java:comp/env/jdbc/ds_app1" />
<!-- SQL Session factories -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property ref="ds_app1" name="dataSource"/>
<property name="configLocation" value="classpath:/conf/xml/mybatis-config.xml" />
<property name="mapperLocations" value="classpath:/conf/xml/mapper/*.xml"/>
</bean>
</beans>
Same piece of code is working in another environment with same WAS8.5 server with same set of data source configurations. In our application, we are using spring4.3.8,mybatis3.x and java8. Here we are injecting datasource beans using xml configuration(dao-spring-context.xml)
Expected output would be data source should be injected to sql session factory bean.but actual result is getting the below exception.
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/xml/dao-spring-context.xml]: Cannot resolve reference to bean 'ds_app1' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds_app1': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'lazyInit' of bean class [org.springframework.jndi.JndiObjectFactoryBean]: Bean property 'lazyInit' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?
This approach worked for me
1) Create a post processor
package org.test;
import org.springframework.bean.PropertyValue;
import org.springframework.bean.PropertyValues;
import org.springframework.bean.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import java.beans.PropertyDescriptor;
#Component
public class CustomJndiInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
#Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {
if (bean instanceOf org.springframeworkf.jndi.JndiObjectFactoryBean) {
for (PropertyValue pv: pvs.getPropertyValues()) {
if ("lazyInit".equals(pv.getName())) {
pv.setOptional(true);
}
}
}
}
}
2) Include this bean in your spring context xml
<bean id="customJndiInstantiationAwareBeanPostProcessor" class="org.test.CustomJndiInstantiationAwareBeanPostProcessor"/>

JPA No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined

I have a JPA/spring/hibernate application deployed on Tomcat 8. But when the tomcat server startup i can see this warning message "No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined". Help would be appreciated because i have no idea about the configuration error.
persistence.xml
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
...
spring context files are defined into several files for reusability
context-ds.xml
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="persistenceUnitName" value="persistenceUnit" />
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManager" />
</bean>
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
...
context-config.xml
<context:annotation-config />
<context:component-scan base-package="com.app" />
<jpa:repositories base-package="com.app" />
<tx:annotation-driven transaction-manager="txManager"
order="200" />
and web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context-config.xml, /WEB-INF/context-ds.xml, /WEB-INF/context-dao.xml</param-value>
</context-param>
and DAO abstract class defines the PersistenceContext annotation
#PersistenceContext
protected EntityManager entityManager;
Real message
11:45:18.710 [localhost-startStop-1] WARN AbstractBeanFactory.getTypeForFactoryBean - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDAO' defined in ServletContext resource [/WEB-INF/context-dao.xml]: Cannot create inner bean 'genericDao$child#632cb33' of type [com.app.dao.GenericDaoImpl] while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericDao$child#632cb33': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
if i declare PersistenceContext like this, i have the message No bean named 'persistenceUnit' is defined
#PersistenceContext(unitName="persistenceUnit")
protected EntityManager entityManager;
you have to define in an #Configuration annotated class a bean like this... a nd name it accordingly to your ref
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = null;
try {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
vendorAdapter.setDatabasePlatform(MyAppSettings.getDbPlattform());
HibernateJpaDialect jpd = new HibernateJpaDialect();
factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaDialect(jpd);
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(MyAppSettings.packagesToScan);
factory.setDataSource(MyDataSource());
} catch (SQLException e) {
e.printStackTrace();
}
return factory;
}
edit:
maybe your spring config xml isnt included into component scan
try :
#ImportResource("classpath:spring-config.xml")
....please post the full stacjtrace of you eception..

How do I autowire my webservices client in my WAR application (using Spring)?

I’m using Maven 3.0.3, CXF 2.7.15, and Spring 3.2.11.RELEASE. Using the JAX-WS Maven plugin, web service client classes are auto-generated (plugin code below) ...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>${basedir}/src/wsdl</wsdlDirectory>
<sourceDestDir>${basedir}/src/main/java</sourceDestDir>
<packageName>org.collegeboard.bsorg</packageName>
</configuration>
</execution>
</executions>
</plugin>
which include the below
package org.myproject.bsorg;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.3-b02-
* Generated source version: 2.1
*
*/
#WebService(name = "OrganizationWebService", targetNamespace = "http://mainco.org/bsorg/")
#XmlSeeAlso({
ObjectFactory.class
})
public interface OrganizationWebService {
Then I have my own service class, in which I try and reference the above through auto wiring …
package org.mainco.subco.myproject.service;
#Service("orgWsdlSvc")
public class OrgWsdlServiceImpl implements OrgWsdlService
{
#Autowired
private OrganizationWebService m_ows;
When I deploy my WAR file, I get the error
09:20:17,846 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-14) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orgWsdlSvc': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.mainco.bsorg.OrganizationWebService org.mainco.subco.myproject.service.OrgWsdlServiceImpl.m_ows; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.mainco.bsorg.OrganizationWebService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290) [spring-beans-3.2.11.RELEASE.jar:3.2.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148) [spring-beans-3.2.11.RELEASE.jar:3.2.11.RELEASE]
But I’m confused about how to properly autowire things in my web.xml and accompanying context files. In my WEB-INF/web.xml, I have
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/META-INF/spring/applicationContext-myproject.xml,
classpath:/META-INF/spring/infrastructure.xml
</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>myproject.webapp</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
In my WEB-INF/dispatcher-servlet.xml file I have
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="org.mainco" />
<!-- Define spring bean for use with this app. -->
<bean id="localPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:application.properties</value>
</property>
</bean>
<!-- Define application properties for use in Spring classes -->
<util:properties id="applicationProperties" location="classpath:application.properties" />
<tx:annotation-driven />
and in my “applicationContext-myproject.xml”, I have
<util:properties id="applicationProperties" location="classpath:application.properties" />
<util:properties id="coreProperties" location="classpath:core.properties" />
<context:component-scan base-package="org.mainco.subco" />
<context:component-scan base-package="org.mainco.bsorg" />
<bean id="sessionTimeoutInSeconds" class="java.lang.Long">
<constructor-arg>
<value>3600</value>
</constructor-arg>
</bean>
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<!-- Define spring bean for use with this app. -->
<bean id="localPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:application.properties</value>
</property>
</bean>
<bean id="assert" class="org.mainco.subco.util.Assert" factory-method="createInstance" />
<http-conf:conduit name="https://.*">
<http-conf:tlsClientParameters secureSocketProtocol="TLSv1" disableCNCheck="true">
<sec:trustManagers>
<sec:keyStore type="JKS" password="${key.store.password}" resource="${key.store.file}" />
</sec:trustManagers>
<sec:keyManagers keyPassword="${key.manager.password}">
<sec:keyStore type="pkcs12" password="${private.key.password}" resource="${private.key.file}" />
</sec:keyManagers>
</http-conf:tlsClientParameters>
</http-conf:conduit>
<jaxws:client id="orgWebServiceClient"
serviceClass="org.mainco.bsorg.OrganizationWebService"
address="${wsdl.url}"
/>
<bean id="organizationWebService" class="org.mainco.bsorg.OrganizationWebService"></bean>
What do I need to do so I can autowire my web services client?
Edit: Per the suggestion given, I added the bean definition to my application context (see above) file, but received the following error upon deplyohing the application:
Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orgWsdlSvc': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.myproject.bsorg.OrganizationWebService org.mainco.subco.myproject.service.OrgWsdlServiceImpl.m_ows;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationWebService' defined in class path resource [META-INF/spring/applicationContext-myproject-mvc.xml]: Instantiation of bean failed;
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.mainco.bsorg.OrganizationWebService]: Specified class is an interface
You are missing the bean definition in your applicationContext-myproject.xml.
<!-- Definition for OrganizationWebService bean -->
<bean id="organizationWebService" class="org.mainco.bsorg.OrganizationWebService"></bean>
See this for more examples:
http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/
EDIT:
You are making progress. Now spring is able to find the autowired candidate in the application context.
However, there is another problem, the candidate is an interface and spring is not able to instantiate it (neither does javac). So, you need to create a class to implement your interface and describe it in your application context (instead or describing an interface). So; you need to create a org.mainco.bsorg.OrganizationWebServiceImp class implementing org.mainco.bsorg.OrganizationWebService and change your application context to:
<bean id="organizationWebService" class="org.mainco.bsorg.OrganizationWebServiceImp"></bean>
More info here: Could not instantiate bean class: Specified class is an interface
EDIT2:
Look at https://jax-ws.java.net/2.2.1/docs/UsersGuide.html#3.1.2_Starting_from_a_WSDL_File
The documentation clarifies that:
Generate a service endpoint interface.
Implement the service endpoint interface.
Create a WAR file to deploy.
You need to implement the service endpoint interface with a concrete class eg: OrganizationWebServiceImp

Spring bean profile reference

I started using bean profile for conditional bean creation, but When i use the profiled bean outside of profile with ref I get exception nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'pb' is defined
<bean id="q" class="com.Q"> //spring can not create this bean since it depends on `pb` bean which is profiled.
<property name="p" ref="pb"/>
</bean>
<beans profile="a">
<bean id="pb" class="com.T"/>
</beans>
<beans profile="b">
<bean id="pb" class="com.T"/>
</beans>
How can I achieve this? I am sure a profile is on:
System.setProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "a");
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "a");

Autowiring request scoped beans into application scoped beans

Is it possible to autowire a request scoped bean into an application scoped bean. i.e
I have a class RequestScopedBean:
class RequestScopedBean {
....
....
....
}
and a class Application scoped bean in which the request scoped bean is autowired.
class ApplicationScopedBean {
#Autowire
private RequestScopedBean requestScopedBean;
....
....
....
}
and the spring-config xml is as follows:
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<bean id="applicationScopedBeans" class="ApplicationScopedBean" />
<bean id="requestScopedBean" class="RequestScopedBean" scope="request">
</bean>
</beans>
when I try to run this application the bean creation of applicationScopedBean fails with the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationScopedBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private RequestScopedBean requestScopedBean; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'request'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
at com.amazon.coral.reflect.instantiate.SpringInstantiatorFactory$1.newInstance(SpringInstantiatorFactory.java:168)
... 19 more
You have to mark your requestScopedBean as a scoped proxy also, this way Spring will inject in a proxy for requestScopedBean and in the background manage the scope appropriately.
<bean id="requestScopedBean" class="RequestScopedBean" scope="request">
<aop:scoped-proxy/>
</bean>
More here
The exception above suggests that you have not correctly configured Spring for the provision of request scoped beans.
You need to add this to your web.xml as described in the docs here:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
However, there is more to your question than just configuration. You are attempting to inject a request scoped bean into a singleton scoped bean. Spring resolves dependencies and instantiates singletons when the DI container starts. This means that ApplicationScopedBean will only be created once (at this point there will be no request in flight and so the autowiring will most likely fail).
If you were using a prototype scoped bean instead of request scoped you'd have to consider a way of suppling the singleton scoped bean with a fresh instance everytime it was used. The approaches for this are described in the Method Injection chapter of the Spring docs.
#Airwavezx the annotation equivalent is the followinng:
#Scope( value = SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS )

Resources