0
votes

Whenever I try to get SpringMVC's ApplicationContext, a NullPointerException is thrown.

This is my userDao:

package com.markor.smarthome.dao;

import com.markor.smarthome.entities.Users;
import com.markor.smarthome.utilites.SpringContextUtil;
import com.markor.smarthome.utilites.StringUtilty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by litongjie on 2015/5/12.
 */
public class UserDao {

private JdbcTemplate jdbcTemplate = (JdbcTemplate)         SpringContextUtil.getBeans("jdbcTemplate");
private String isExistSql = "SELECT *  FROM users WHERE user_id=?";
private String addUserSql = "INSERT INTO users(`user_id`,`password`,`name`,`email`,`note`)VALUES(?,?,?,?,?)";

public boolean isExist(String userID) {

    Users user = jdbcTemplate.queryForObject(isExistSql, Users.class, userID);
    //如果为空 返回false不存在
    if (StringUtilty.isNullOrEmpty(user.getUser_id())) {
        return false;
    } else return true;
}

public int AddUser(Users user) {
    System.out.println("begen");
    Map<String, Object> map = new HashMap<>();
    map.put("user_id", user.getUser_id());
    map.put("password", user.getPassword());
    map.put("name", user.getName());
    map.put("email", user.getEmail());
    map.put("note", user.getNote());
    int i = jdbcTemplate.update(addUserSql, map);
    return i;
}

public static void main(String[] args) {
    System.out.println("main begin");
    Users users = new Users();
    users.setUser_id("123");
    users.setName("LI");
    users.setNote("444");
    System.out.println("user end"+users);
    UserDao userDao = new UserDao();
    System.out.print(userDao.AddUser(users));
}
}

This is my SpringContextUtil

package com.markor.smarthome.utilites;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;

/**
    * 获取Spring上下文及国际化
 *
 * @author bingchuan -->www.vijun.com
 */
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext application = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)     throws BeansException {
    this.application = applicationContext;
}

public static ApplicationContext getApplicationContext() {
    return application;
}

public static Object getBeans(String beanname) {
    ApplicationContext applicationContext = getApplicationContext();
    return applicationContext.getBean(beanname);
}

I tried using some other's answers, but it didn't work either. Here is my exception:

Exception in thread "main" java.lang.NullPointerException at com.markor.smarthome.utilites.SpringContextUtil.getBeans(SpringContextUtil.java: 31) at com.markor.smarthome.dao.UserDao.(UserDao.java:18) at com.markor.smarthome.dao.UserDao.main(UserDao.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

1

1 Answers

0
votes

That looks completely wrong. Where did you start up your spring context? If you want to use it somewhere in the main() method, you have to write something like this:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");

(but in that case you don't need SpringContextUtil, because you already have your context).

But why don't you add UserDao to the context and let spring inject jdbcTemplate bean into it?

something like this: UserDao:

public class UserDao {
@Autowire
private JdbcTemplate jdbcTemplate;
.....

spring context:

<bean id="jdbcTemplate" class=.../>
<bean id="userDao" class="com.markor.smarthome.dao.UserDao"/>