目录
出现的问题(org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 36; schema_reference.4)
对于bean的注入,除了使用xml配置以外,可以使用注解配置。注解的配置,可以简化配置文件,提高开发的速度,使程序看上去更简洁。对于注解的解释,Spring对于注解有专门的解释器,对定义的注解进行解析,实现对应bean对象的注入。通过反射技术实现。
一,配置xml文件
- "1.0" encoding="UTF-8"?>
"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">
-
-
-
-
package="com.lsf"/> -
-
-
-
"userService" class="com.lsf.service.UserService"> -
-
"userdao" class="com.lsf.dao.Userdao">
二,使用注解
- package com.lsf.service;
-
- //导入需要的类
- import com.lsf.dao.Userdao;
- //导入注解
- import javax.annotation.Resource;
-
- public class UserService {
- //使用自动注入(尽量属性字段名称与配置文件中的bean的id一致)
- @Resource
- private Userdao userdao;
-
- public void test(){
- System.out.println("userService.....");
- userdao.test();
- }
- }
三,启动
- package com.lsf;
-
- import com.lsf.service.UserService;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class Starter {
-
- public static void main(String[] args) {
- BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
- UserService userService = (UserService) factory.getBean("userService");
- userService.test();
- }
- }
现象

出现问题的原因:xml配置文件出现了问题,导致找不到文档
解决办法(自己打容易打错所有,还是使用复制粘贴吧):
xml配置
- "1.0" encoding="UTF-8"?>
"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">
-
-
-
-
package="com.lsf"/> -
-
"userService" class="com.lsf.service.UserService"> -
"userdao" class="com.lsf.dao.Userdao">