Java 获取Spring数据源上的空指针
我想要数据源实例,但我得到了NPE xml: 课程表类别:Java 获取Spring数据源上的空指针,java,spring-mvc,nullpointerexception,datasource,Java,Spring Mvc,Nullpointerexception,Datasource,我想要数据源实例,但我得到了NPE xml: 课程表类别: public class Schedule { LoginLog l = new LoginLog(); public void call(){ System.out.println("In SC"); l.loginEventLogging(); } } 登录: @Service public class LoginLog{ @Autowired privat
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)
线程“main”java.lang.IllegalArgumentException中的异常:需要属性“dataSource”
位于org.springframework.jdbc.support.jdbccessor.AfterPropertieSet(jdbccessor.java:135)
位于org.springframework.jdbc.core.JdbcTemplate(JdbcTemplate.java:169)
位于ngl.jms.dbLog.LoginLog.logineventloging(LoginLog.java:30)
位于ngl.jms.dbLog.Schedule.call(Schedule.java:13)
位于ngl.jms.application.Main.Main(Main.java:13)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:606)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
我不明白为什么它会给我一个错误NPE。
帮助。问题是您正在自己实例化
LoginLog
(请参见计划中的LoginLog l=new LoginLog();
)
这意味着LoginLog
不是由Spring管理的,因此不会发生依赖注入
您需要执行以下操作(将导致所有相关类都由Spring管理):
假设Schedule和LoginLog位于包“nl.jms”或任何子包下: 您应该更改:
@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 {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
Schedule s = applicationContext.getBean(Schedule.class.getName());
s.call();
}
}
通过这种方式,spring context知道您要做什么,并将为您进行连接。您在path中是否需要postgres库?是的,我添加了maven。您不应该使用“new”操作符创建类。相反,您应该从spring上下文中获取它。@sinisa229mihajlovski-那么我必须做什么?那么我必须做什么,先生?仍然无法使用此代码,先生,您对日程安排有何了解?@javakillerღ 检查我的最新答案。我已经添加了所有相关代码伟大的先生,它的工作就像一个魅力。非常感谢你。
@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();
}
}
@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 {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
Schedule s = applicationContext.getBean(Schedule.class.getName());
s.call();
}
}