开发者

What beans go in the Application Context vs. the Web Context in Spring?

The separation between application context and web context (and the class loader issues that ensue) are a constant source of problems for me. I'm using Spring in my first project, migrating from a badly written JSP-based webapp to Spring-based.

I just want to know if this configuration makes sense:

  • I have the controllers, form objects, and such defined using annotations and scanned in the web application context.
  • I moved the DAOs (data access objects) to the application context after initially having them in the web app context - this was because I needed to use them to get the user/password for spring security, which is an application context bean.
  • Spring security if defined (as per the docs) in the application context, necessitating DAO to go with it.

Now I'm running into classloader issues where I pass an object to JDO/DataNucleus and it's created by the web app classloader, 开发者_开发知识库but the DAOs are all part of the application context, thus that component gets its own classloader and can't match up the same objects.

Simple method from DAO:

@Override
public boolean userExists(String username) {
    Query query = pm.newQuery(User.class);
    query.setFilter( "username == :usernameParam" );
    query.setResult( "count(username)" );
    query.setResultClass(Long.class);
    System.out.println(username);
    Long result = (Long)query.execute(username);

    return (result!=null && result>0);
}

javax.jdo.JDOUserException: The Query will return a single field but it is not of a consistent type as the ResultClass (java.lang.Long) : It is java.lang.Long

I ask because this is not the first classloader issue (and I fear not the last) to pop up because of the way spring is configured right now, so I wonder if I'm doing things poorly.

Or perhaps there are some configurations that address these kinds of class loader issues that I'm not yet aware of?


The classloader should have nothing to do with the Spring context. Webapplication context is a spring container which generally contains controllers and view resolvers. Application context contains dao's. Web application context has application context as parent, so that it can access the dao and service beans and not vice versa. However both the context are part of same war and should be loaded by same class loader.

Looking at your exception, I think, it does not seems to have anything to do with Spring.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜