I am having a problem creating request scoped beans within a sample app I am trying. The code I am using is as follows
Site.java
public interface Site {
int getId();
String getCode();
String getName();
}
SiteImpl.java
public class SiteImpl implements Site {
private int id;
private String code;
private String name;
public SiteImpl(int id, String name, String code) {
this.id = id;
this.name = name;
this.code = code;
}
public int getId() {
return id;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
And finally SiteFactory.java
@Service
public class SiteFactory implements FactoryBean<SiteImpl> {
@Autowired
private HttpServletRequest currentRequest;
@Override
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES)
public SiteImpl getObject() throws Exception {
SiteImpl site = null;
if (currentRequest != null) {
site = new SiteImpl(1, currentRequest.getServerName(), currentRequest.getServerName());
}
return site;
}
@Override
public Class<?> getObjectType() {
return SiteImpl.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
I keep getting the following error
Cannot create scoped proxy for bean 'scopedTarget.getObject': Target type could not be determined at the time of proxy creation.
Any pointers towards what I am doing wrong.
I tried setting the @Component and @Scoped annotations on the SiteImpl, but then the factory is not being called to initialize it
I also tried removing the @Scope, @Component and @Bean annotations, and instead depend on the XML configuration
<bean id="site" factory-bean="siteFactory" scope="request">
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
And I still get the same error message.
Your code has 2 problems
FactoryBean
and are adding annotations, basically mixing strategies which is (generally speaking) not a good ideaHttpServletRequest
into a singleton object.To fix these problems remove the FactoryBean
interface which will give precedence to your @Bean
annotated method. You can now also remove the isSingleton
and getObjectType
methods as those aren't needed.
The second problem is by simply removing the @Autowired
attribute and simply add it as a method parameter to your getObject
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With