Multiple annotations found at this line:
- The content of element type "beans" must match "(description?,(import|alias|bean)*)".
- Attribute "xsi:schemaLocation" must be declared for element type "beans".
- Attribute "xmlns:xsi" must be declared for element type "beans".
...
**同時宣告兩者會導致 XML 解析器困惑**,產生上述屬性未宣告、內容模型不匹配錯誤。
| 錯誤寫法 | 正確寫法 | 說明 |
|---|---|---|
| 保留 `` | **刪除整行** | DTD 不支援 `xmlns:*`,必須捨棄 |
| `spring-beans-2.5.xsd` 版號硬編碼 | `spring-beans.xsd` **無版號** | Spring 會自動解析為當前 Jar 版本對應 XSD |
| 缺少 `xmlns="https://www.springframework.org/schema/beans"` | **必須加在 ` | 預設命名空間,讓 ` |
| 缺少 `xmlns:context` | 依需求加入 | 支援 ` |
xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xmlns:mvc="https://www.springframework.org/schema/mvc"
xmlns:jpa="https://www.springframework.org/schema/data/jpa"
xmlns:security="https://www.springframework.org/schema/security"
@Configuration
@ComponentScan("com.example")
@EnableAspectJAutoProxy
@EnableTransactionManagement
@EnableJpaRepositories("com.example.repository")
public class AppConfig {
@Bean
public DataSource dataSource() { ... }
@Bean
public PlatformTransactionManager txManager(DataSource ds) {
return new DataSourceTransactionManager(ds);
}
}
Spring XML 配置錯誤根因:**DTD 與 Schema 衝突**。解決:**刪除 ``、使用無版號 XSD、補全 `xmlns` 命名空間**。現代專案**強烈建議遷移至 Java Config (`@Configuration`)**,享受類型安全、IDE 重構、編譯期檢查優勢。