问题
I am working on SpringBoot, Hibernate application and when I am trying to the run the application. I am getting error "Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException"
Error log that I am getting:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
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:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
at com.spring.main.SpringLogging3Application.data(SpringLogging3Application.java:97)
at com.spring.main.SpringLogging3Application.main(SpringLogging3Application.java:53)
... 5 more
Below is the source code that I am trying to run. Just assume that I am having two entity classes Question and Answer with it's getter and setter methods.
import java.util.ArrayList;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
@ComponentScan("com.spring.main")
public class SpringLogging3Application {
@Autowired
static SessionFactory factory;
@Value("${db.driver}")
private String DB_DRIVER;
@Value("${db.url}")
private String DB_URL;
@Value("${db.username}")
private String DB_USERNAME;
@Value("${db.password}")
private String DB_PASSWORD;
@Value("${hibarenate.dialect}")
private String HIBERNATE_DIALECT;
@Value("${hibarenate.show_sql}")
private String HIBERNATE_SHOW_SQL;
@Value("${hibarenate.hbm2ddl.auto}")
private String HIBERNATE_HBM2DDL_AUTO;
@Value("${entitymanager.packagesToScan}")
private String ENTITYMANAGER_PACKAGES_TOSCAN;
public static void main(String[] args) {
SpringApplication.run(SpringLogging3Application.class, args);
data();
}
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource datasource) throws Exception {
Properties hibernateproperties = new Properties();
hibernateproperties.put("hibarenate.dialect", HIBERNATE_DIALECT);
hibernateproperties.put("hibarenate.show_sql", HIBERNATE_SHOW_SQL);
hibernateproperties.put("hibarenate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
LocalSessionFactoryBean factorybean = new LocalSessionFactoryBean();
factorybean.setDataSource(dataSource());
factorybean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TOSCAN);
factorybean.setHibernateProperties(hibernateproperties);
factorybean.afterPropertiesSet();
SessionFactory sf = factorybean.getObject();
System.out.println("##getSessionFactory" +sf);
return sf;
}
@Autowired
@Bean(name="datasource")
public DataSource dataSource() {
DriverManagerDataSource datasources = new DriverManagerDataSource();
datasources.setDriverClassName(DB_DRIVER);
datasources.setUrl(DB_URL);
datasources.setUsername(DB_USERNAME);
datasources.setPassword(DB_PASSWORD);
System.out.println("## getDataSource: " +datasources);
return datasources;
}
@Autowired
@Bean(name="transactrionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionfactory) {
HibernateTransactionManager manager = new HibernateTransactionManager(sessionfactory);
//manager.setSessionFactory(sessionFactory().getObject());
return manager;
}
public static void data() {
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Answer an1=new Answer();
an1.setAnswername("Java is programming language");
an1.setPostedBy("Ravi Malik");
Answer an2=new Answer();
an2.setAnswername("Java is a platform");
an2.setPostedBy("Sudhir Kumar");
Question q1=new Question();
q1.setQuestion("What is Java?");
ArrayList<Answer> l1=new ArrayList<Answer>();
l1.add(an1);
l1.add(an2);
q1.setAnswer(l1);
Answer ans3=new Answer();
ans3.setAnswername("Servlet is an Interface");
ans3.setPostedBy("Jai Kumar");
Answer ans4=new Answer();
ans4.setAnswername("Servlet is an API");
ans4.setPostedBy("Arun");
Question q2=new Question();
q2.setQuestion("What is Servlet?");
ArrayList<Answer> l2=new ArrayList<Answer>();
l2.add(ans3);
l2.add(ans4);
q2.setAnswer(l2);
session.persist(q1);
session.persist(q2);
t.commit();
session.close();
System.out.println("Success");
}
}
Updated code - After implementing commandlineRunner
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
@ComponentScan("com.spring.main")
public class SpringLogging3Application implements CommandLineRunner {
SessionFactory factory;
@Value("${db.driver}")
private String DB_DRIVER;
@Value("${db.url}")
private String DB_URL;
@Value("${db.username}")
private String DB_USERNAME;
@Value("${db.password}")
private String DB_PASSWORD;
@Value("${hibarenate.dialect}")
private String HIBERNATE_DIALECT;
@Value("${hibarenate.show_sql}")
private String HIBERNATE_SHOW_SQL;
@Value("${hibarenate.hbm2ddl.auto}")
private String HIBERNATE_HBM2DDL_AUTO;
@Value("${entitymanager.packagesToScan}")
private String ENTITYMANAGER_PACKAGES_TOSCAN;
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource datasource) throws Exception {
Properties hibernateproperties = new Properties();
hibernateproperties.put("hibarenate.dialect", HIBERNATE_DIALECT);
hibernateproperties.put("hibarenate.show_sql", HIBERNATE_SHOW_SQL);
hibernateproperties.put("hibarenate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
LocalSessionFactoryBean factorybean = new LocalSessionFactoryBean();
factorybean.setDataSource(dataSource());
factorybean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TOSCAN);
factorybean.setHibernateProperties(hibernateproperties);
factorybean.afterPropertiesSet();
SessionFactory sf = factorybean.getObject();
System.out.println("##getSessionFactory" +sf);
return sf;
}
@Autowired
@Bean(name="datasource")
public DataSource dataSource() {
DriverManagerDataSource datasources = new DriverManagerDataSource();
datasources.setDriverClassName(DB_DRIVER);
datasources.setUrl(DB_URL);
datasources.setUsername(DB_USERNAME);
datasources.setPassword(DB_PASSWORD);
System.out.println("## getDataSource: " +datasources);
return datasources;
}
@Autowired
@Bean(name="transactrionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionfactory) {
HibernateTransactionManager manager = new HibernateTransactionManager(sessionfactory);
//manager.setSessionFactory(sessionFactory().getObject());
return manager;
}
public static void main(String[] args) {
SpringApplication.run(SpringLogging3Application.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("Inside run method");
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Answer an1=new Answer();
an1.setAnswername("Java is programming language");
an1.setPostedBy("Ravi Malik");
Answer an2=new Answer();
an2.setAnswername("Java is a platform");
an2.setPostedBy("Sudhir Kumar");
Question q1=new Question();
q1.setQuestion("What is Java?");
ArrayList<Answer> l1=new ArrayList<Answer>();
l1.add(an1);
l1.add(an2);
q1.setAnswer(l1);
Answer ans3=new Answer();
ans3.setAnswername("Servlet is an Interface");
ans3.setPostedBy("Jai Kumar");
Answer ans4=new Answer();
ans4.setAnswername("Servlet is an API");
ans4.setPostedBy("Arun");
Question q2=new Question();
q2.setQuestion("What is Servlet?");
ArrayList<Answer> l2=new ArrayList<Answer>();
l2.add(ans3);
l2.add(ans4);
q2.setAnswer(l2);
session.persist(q1);
session.persist(q2);
t.commit();
session.close();
//return "Success";
}
}
回答1:
The problem is this.
@Autowired
static SessionFactory factory;
You can not auto wire static fields.
来源:https://stackoverflow.com/questions/62238830/exception-in-thread-restartedmain-java-lang-reflect-invocationtargetexception