David Parks
David Parks

Reputation: 32081

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:

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?

Upvotes: 2

Views: 1547

Answers (1)

lalit
lalit

Reputation: 1475

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.

Upvotes: 3

Related Questions