2013-05-21 32 views
20

Mới đến mùa xuân và tôi biết câu hỏi này đã được hỏi nhiều lần, nhưng tôi phải hỏi lại câu hỏi này một lần nữa, nhưng tôi phải hỏi lại một lần nữa. vẫn phải đối mặt với lỗi "The prefix "context" for element "context:component-scan" is not bound." có một câu hỏi tương tự here, nhưng tôi dint có được câu trả lờingữ cảnh: quét thành phần "không bị ràng buộc

đây là tài liệu xml của tôi, là nó mà không gian tên của tôi là không đúng?

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



    <bean id="point1" class="org.sri.proj.Point"> 
     <property name="x" value="0" /> 
     <property name="y" value="0" /> 
    </bean> 

    <bean id="point2" class="org.sri.proj.Point"> 
     <property name="x" value="10" /> 
     <property name="y" value="10" /> 
    </bean> 

    <context:component-scan base-package="org.sri.proj"/> 

</beans> 
+2

Bạn đang thiếu khai báo không gian tên cho 'ngữ cảnh'. –

+0

Tôi hơi bối rối về những không gian tên này, bạn có thể vui lòng xây dựng? –

+0

Các câu trả lời dưới đây bao gồm hầu hết. –

Trả lời

69

Thêm tờ khai context namespace để số beans định nghĩa thẻ trong bối cảnh ứng dụng nộp

<beans xmlns="http://www.springframework.org/schema/beans" 
    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-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
+0

Điều này giải quyết được vấn đề của tôi. Cảm ơn bạn –

+0

Tiện ích của không gian tên là gì? tôi có phải cập nhật chúng khi tôi muốn chuyển sang mùa xuân 4 không? – Aguid

+0

Bạn có ý nghĩa gì với "tiện ích"? Spring cần biết rằng không gian tên đang được sử dụng trước khi sử dụng nó, tương tự như khai báo một biến.Tuyên bố không gian tên cần thiết cho tất cả các phiên bản của Spring – Reimeus

3

Bạn đang thiếu Context (http://www.springframework.org/schema/context) namespace có:

<beans:beans xsi:schemaLocation="http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd 


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

Thêm dòng cuối cùng từ mã này.

8

Vâng, bạn phải thêm

http://www.springframework.org/schema/context 

Trước

http://www.springframework.org/schema/context/spring-context-3.0.xsd 

Vì vậy, nó trông giống như:

<beans xmlns="http://www.springframework.org/schema/beans" 
    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-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
+0

Tôi đã thêm "http://www.springframework.org/schema/context" trước "http://www.springframework.org/schema/context/spring-context-3.0.xsd" nhưng nó vẫn hiển thị cho tôi lỗi tương tự, tôi thậm chí còn làm sạch dự án của tôi, vẫn không tốt –

+1

Tôi đã bỏ lỡ "xmlns: context =" http://www.springframework.org/schema/context "trước đó :) –

3

For All thời gian Sake không chỉ đơn giản là sao chép và quá khứ. Nhưng.

Đầu tiên thấy cái comment bạn sử dụng trong xml của bạn file.Let nói xml của bạn có chứa những điều sau ...

  1. <context:component-scan base-package="com.spring.study" />
  2. <context:annotation-config/>
  3. <mvc:annotation-driven />

Sau đó, trước khi bạn mã sao chép và quá khứ thấy rằng những gì bạn thực sự cần trong tiêu đề <beans:beans section ...

Vì vậy, Thực hiện như thế này đáp ứng cài đặt cấu hình ở trên và

Đảm bảo bạn dọn dẹp và xây dựng mọi lúc sau khi bạn thay đổi tệp của mình !!

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

    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.spring.study" /> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

Chúc mừng! Dman!

2

Bạn nên thêm xmlns:context="http://www.springframework.org/schema/context" vào xml đậu của mình.

Các vấn đề liên quan