user6679745
user6679745

Reputation:

Spring Bean XML Declaration From Different JAR

Suppose I'm trying to declare a bean in my dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    ...

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    ...

</beans>

But here's the problem: in my own project, I've got a package named exactly org.springframework.web.servlet.view and in that package, I don't have the same classes as Spring does. What this does is it "confuses" Spring and as such, looks for the class in my project but I want it to fetch the class from the Spring libraries/jars. This results in an error along the lines of that it can't find the class. How can I tell Spring to look at its own classes and not mine?

I can't tell you how odd this is, but in my project, even my IDE can't tell where the class definition is. But in another project, I've tried yet it still works.

Upvotes: 0

Views: 453

Answers (2)

Mark D
Mark D

Reputation: 333

Can't really help you without knowing more about your environment. Your IDE may load packages in a different order than say Tomcat or just the standard JVM. Your projects manifest file may contain some clues. Here's some info on how java loads jar files and packages. Java: how classes are found

Bottom line is..

You shouldn't have a package with the name org.springframework.web.servlet.view in your project. Java Package naming standards

Upvotes: 0

gregbert
gregbert

Reputation: 446

If you don't have any classes that share a name, there shouldn't be any issues with having the same package name. I personally recommend that you change your package name because that will help you identify the problem. If your IDE can't find the Spring classes then the Spring jar that you need probably isn't on the classpath.

There are some useful takeaways on package and class collisions here that I used to form my answer.

Upvotes: 1

Related Questions