1. 程式人生 > >元素“context:component-scan”的字首“context”未繫結

元素“context:component-scan”的字首“context”未繫結

  首先報這個錯誤,你得明白,是什麼原因導致的?

  答:未引入名稱空間,和約束檔案

解決方法:

<?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: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/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 元件掃描器,主要是spring使用,用來掃描帶有指定註解的類,將這些載入成BeanDefinition -->
    <context:component-scan base-package="com.cyb.spring.service" />
</beans>

標籤中加上

<!--名稱空間-->
xmlns:context="http://www.springframework.org/schema/context" <!--別名和約束檔案--> xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"

約束檔案的話,請看:http://www.springframework.org/schema/beans/spring-beans.xsd

<!--別名-->
http://www.springframework.org/schema/context
<!--公網地址-->
http://www.springframework.org/schema/context/spring-context.xsd

若不加上名稱空間的話bean得這樣寫

<beans:bean id="" class=""></beans>