2020-01-09 @Autowired注解抛异常: java.lang.NullPointerException

出现这个问题的原因有以下几点:
一:配置文件
1.applicationContext.xml
要开启注解支持!扫描器路径不要写错咯!


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p">
    <!-- 1.开启注解支持 -->
    <context:annotation-config/>
    <!--2.配置包扫描器-->
    <context:component-scan base-package="test.spring.*"/>
<beans/>


二:类上需要加注解
是否有@Service(Service的实现类上)、@Repository(Dao的实现类上)、@Component。

@Service("annotationService")
public class AnnotationService {
    private UserDao userImpl;
    public UserDao getUserImpl() {
        return userImpl;
    }

    @Resource         //    @Autowired,加在成员属性或setter方法上都可以
    public void setUserImpl(UserDao udao) {
        this.userImpl = userImpl;
    }

    public void useAnnotation(){
        userImpl.getNum();
    }
}

 

@Repository("udao")
public class UserImpl implements UserDao{
    public void getNum(){
        System.out.println(1234);
    }
}


三、测试

@Component
public class AnnotationServiceController {
    @Autowired
    private static AnnotationService annotationService;

    public static void main(String [] args){
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","spring-conf.xml");
//        AnnotationService annotationService = (AnnotationService)applicationContext.getBean("annotationService");
        annotationService.useAnnotation();
    }
}


四、使用Autowired注入需满足两个条件
1.被注入的类的对象交给了spring管理;
2.同时使用的类的对象也要交给spring管理.两个条件都满足才能注入.
五、废话
自动装载并没有把配置文件中配置的bean转载到内存中。
 Spring的自动装载是在applicationContext=new ClassPathXmlApplicationContext("");的时候自动装载。在实例化的时候,会去根据<context:component-scan base-package="test.spring.*"></context:component-scan>中配置的扫描范围,扫描所有的有@Autowired注解的bean,进行装载,装载完以后,所有地方都可以使用这个bean了。
【补充:】
事实证明第五点不是废话!!
如果注解注入还是失败,那么有可能就是这个原因了!!
【重点:】
如果你的配置文件跟我的一样,有springmvc.xml,applicationContext.xml。
那么,在web.xml里,一定要初始化spring容器:<param-value>classpath:springmvc.xml</param-value>。

因为注解只是帮我们把Bean放到了容器里,我们初始化这个容器才可以用!

就好像<bean id="" class="">只是注册到了容器里,通过new ClassPathXmlApplicationContext("")初始化了容器,才可以从容器中取东西来用!!

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <filter>
       <filter-name>CharacterEncodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>utf-8</param-value>
       </init-param>
   </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--初始化spring容器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置springmvc的前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--*.do:匹配所有以.do.结尾的文件-->
        <!--斜杠:/ 拦截所有请求-->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值