zoukankan      html  css  js  c++  java
  • Spring注解运行时抛出null

    关于Spring的注解其实不难,大致需要以下几个流程:

    一、配置Spring的注解支持

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     4     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     6         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     7         http://www.springframework.org/schema/context 
     8         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
     9 
    10     <context:component-scan base-package="com.ximesun.jee" />
    11     <context:annotation-config />
    12 
    13 </beans>

    二、写对象时注解标识各种类,尽可能按照用途来区分

    1 @Controller
    2 public class HomeAction {...}
    3 @Service
    4 public class HomeService {...}
    5 @Repository
    6 public class User {...}
    7 @Component
    8 public class MenuDao extends DbUtilsTemplate {...}

    三、注解的注入和引用

     1 @Service
     2 public class HomeService {
     3     @Autowired
     4     private MenuDao dao;    
     6     public List<Menu> getAllMenu(){
     8         List<Menu> mlist = new ArrayList<Menu>();
     9         mlist = dao.findAll();
    10         return mlist;
    11 }

    OK,理论上这样就可以,然而很多时候会出现各种问题,例如我刚遇到这样一个抛出:

    四、一个问题

    1 java.lang.NullPointerException
    2     at com.ximesun.jee.fsdiserver.HomeService.getAllMenu(HomeService.java:20)
    3     at com.ximesun.jee.fsdiserver.FSdiserver.main(FSdiserver.java:23)

    JAVA的童鞋熟悉的不能再熟悉了吧~我确认相关的对象定义是没问题的,那引用呢?找到问题了:

    1 public static void main(String[] args) throws InterruptedException {
    2     AbstractApplicationContext applicationContext = new FileSystemXmlApplicationContext(
    3             "conf/context.xml");        
    4     List<Menu> mlist = new ArrayList<Menu>();
    5     HomeService h = new HomeService();
    6     mlist = h.getAllMenu();
    7     applicationContext.registerShutdownHook(); 
    8 }

    问题就是这里new了HomeService,而new的类是不会根据注解自动引入的,那怎么办呢?

    将上面的第五行代码修改为如下的方法初始化,问题就解决了。

    1 HomeService h = applicationContext.getBean("homeService",HomeService.class);

    用Spring的环境里初始化这个对象,就可以顺理成章的使用这个对象中的关于Spring的注解了。

    五、关于带参数的构造器的类的@AutoWired

    首先这个类一定要有不带参数的构造器,然后就是要为构造器相关的参数写set方法。

    错误的用法如下:

    1 @AutoWired
    2 TcpClientSocketThread clientThread;
    3 ......
    4 clientThread = new TcpClientSocketThread(id, clientSocket);
    5 clientThread.start();

    正确的用法如下:

    1 @AutoWired
    2 TcpClientSocketThread clientThread;
    3 ......
    4 clientThread.setClientId(id);
    5 clientThread.setClientSocket(clientSocket);
    6 clientThread.start();

    更多关于Spring注解的内容和应用,建议参考《Spring in action》。

  • 相关阅读:
    for,while循环嵌套二次练习
    break语句;continue语句
    实际运用for、while循环嵌套
    for,while循环嵌套
    while循环
    if语法分支
    js读取cookie,并利用encrypt和decrypt加密和解密方法
    instance of的java用法
    objectmapper使用
    日志组件slf4j介绍及配置详解
  • 原文地址:https://www.cnblogs.com/yoyotl/p/4708599.html
Copyright © 2011-2022 走看看