java - 在 Spring dataSource 上获取空指针

标签 java spring-mvc nullpointerexception datasource

我想要数据源实例,但我得到了 NPE。

xml:

    <context:component-scan base-package="nl.jms" />
    <context:annotation-config />    

    <!--DataSource Bean Initialization-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/nl"/>
        <property name="username" value="postgres"/>
        <property name="password" value="pwd"/>
    </bean>

主要方法:

public class Main {
    public static void main(String[] args) throws IOException {
        new ClassPathXmlApplicationContext("spring/applicationContext.xml");

        Schedule s = new Schedule();
        s.call();
    }
}

安排类(class):

public class Schedule {
    LoginLog l = new LoginLog();
    public void call(){
        System.out.println("In SC");
        l.loginEventLogging();
    }
}

登录日志:

@Service
public class LoginLog{

    @Autowired
    private IMailEvent mailEvent;

    @Autowired
    DataSource dataSource;

    @Override
    public void loginEventLogging(){
        System.out.println("In Log");
        String checkoutSql = "select *  from transaction where data_date::date = current_date;";
        System.out.println("HERE");
        System.out.println("KKK" + dataSource);
        org.springframework.jdbc.core.JdbcTemplate template = new org.springframework.jdbc.core.JdbcTemplate(dataSource);        
    }
}

错误:

Exception in thread "main" java.lang.IllegalArgumentException: Property 'dataSource' is required
    at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:135)
    at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:169)
    at ngl.jms.dbLog.LoginLog.loginEventLogging(LoginLog.java:30)
    at ngl.jms.dbLog.Schedule.call(Schedule.java:13)
    at ngl.jms.application.Main.main(Main.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我不明白为什么它会给我一个错误 NPE。 帮助。

最佳答案

问题是您正在自己实例化 LoginLog

(请参阅Schedule 中的LoginLog l = new LoginLog();)。

这意味着 LoginLog 不受 Spring 管理,因此不会发生依赖注入(inject)。

您需要执行以下操作(将导致所有相关类都由 Spring 管理):

@Component
public class Schedule {
    @Autowired
    private LoginLog l;

    public void call(){
        System.out.println("In SC");
        l.loginEventLogging();
    }
}

public class Main {
    public static void main(String[] args) throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");

        Schedule s = context.getBean(Schedule.class)
        s.call();
    }
}

关于java - 在 Spring dataSource 上获取空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24573480/

相关文章:

java - Spring 3 MVC @Controller 与 AOP 拦截器?

java - Spring/Hibernate 应用程序仅在没有 @Transactional 的情况下工作

java - 空指针协助。

java - 什么是NullPointerException,我该如何解决?

java - libJdbcOdbc SQLAllocEnv 未定义和一般动态链接

java - java servlet 调用永远不会到达 servlet

java - 是否可以通过 post 协议(protocol)获取返回值?

java - Spring提交后Servlet无法访问POST方法

java - 什么是NullPointerException,我该如何解决?

java - ViewPager 中替换 fragment 的问题