spring icon indicating copy to clipboard operation
spring copied to clipboard

No scoped proxy for SqlSessionTemplate with Spring JavaConfig

Open FlorianSchaetz opened this issue 9 years ago • 0 comments

I am trying to make Mappers serializable (kind of). To do so, I would simply want to wrap the SqlSessionTemplate (which is the non-serializable thing) via Java config into a scoped proxy...

@Configuration
@MapperScan(basePackages={"com.example.mybatis.mappers"}, sqlSessionTemplateRef="sqlSession")
public class SpringConfiguration {

    @Bean
    @Scope(proxyMode=ScopedProxyMode.TARGET_CLASS)
    public SqlSessionTemplate sqlSession(final SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

Unfortunately, this doesn't work, since the scoped proxy for the SqlSessionTemplateseems to be created, but what's injected into the Mapper in the end is still a standard SqlSessionTemplate (and not the Proxy around it), which has somehow escaped the proxy. I guess it gets set from some sqlSessionTemplate.getMapper call.

Edit: Yep, the problematic part seems to be this method from org.mybatis.spring.SqlSessionTemplate...

    @Override
    public <T> T getMapper(Class<T> type) {
        return getConfiguration().getMapper(type, this);
    }

Of course, thishere refers to the original SqlSessionTemplate object and not the Proxy around it.

Is there a way to force my scoped proxy into the mappers?

Of course, what works is to simply make the mappers themselves scoped proxies, but that seems to be less elegant and concise, so I would prefer making the problematic object a scoped proxy, since the MapperProxyitself is already Serializable and could be serialized - it just gets prevented by the SqlSessionTemplate.

(Of course, the non-existing default constructor will also be a problem...)

FlorianSchaetz avatar Mar 19 '16 10:03 FlorianSchaetz