Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
@自动连线SessionFactory在Java配置中返回null spring_Java_Spring_Spring Mvc_Nullpointerexception - Fatal编程技术网

@自动连线SessionFactory在Java配置中返回null spring

@自动连线SessionFactory在Java配置中返回null spring,java,spring,spring-mvc,nullpointerexception,Java,Spring,Spring Mvc,Nullpointerexception,当我尝试在sessionFactory中使用DI时,我在sessionFactory中遇到空指针异常,我必须将Spring3项目转换为Spring4,而不使用xml。我不知道我面临什么问题,SessionFactory根本没有自动连线。当我尝试测试通用doa Add方法时 这是我的bean配置文件 @Configuration @EnableTransactionManagement @EnableWebMvc @ComponentScan(basePackages = "io.github.b

当我尝试在sessionFactory中使用DI时,我在sessionFactory中遇到空指针异常,我必须将Spring3项目转换为Spring4,而不使用xml。我不知道我面临什么问题,SessionFactory根本没有自动连线。当我尝试测试通用doa Add方法时

这是我的bean配置文件

@Configuration
@EnableTransactionManagement
@EnableWebMvc
@ComponentScan(basePackages = "io.github.bibekshakya35.ehealth")
public class EhealthCofiguration {

@Bean
public ViewResolver getViewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/view/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

@Bean(name = "dataSource")
public javax.sql.DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUrl("jdbc:postgresql://localhost:5432/ehealth");
    dataSource.setUsername("test");
    dataSource.setPassword("test123");
    return dataSource;
}

@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(javax.sql.DataSource dataSource) {
    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
    sessionBuilder.scanPackages("io.github.bibekshakya35.ehealth.model");
    sessionBuilder.addProperties(getHibernateProperties());
    return sessionBuilder.buildSessionFactory();
}

private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.show_sql", "true");
    properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
    properties.put("hibernate.hbm2ddl.auto", "update");
    return properties;
}

@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(
        SessionFactory sessionFactory) {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager(
            sessionFactory);

    return transactionManager;
}
}
加载我创建的这个类

public class EhealthWebAppIntializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
    applicationContext.register(EhealthCofiguration.class);
    ServletRegistration.Dynamic dispatcher =servletContext.addServlet("SpringDispatcher", new DispatcherServlet(applicationContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
}

}
这是我的通用doa类,当SessionFactory尝试访问
SessionFactory.getCurrentSession()时,我在其中注入SessionFactoryNPE发生时

@Repository
@Transactional
public class HibernateDAO<T extends Serializable> implements IGenericDao<T> {

private Class<T> clazz;

Session session;

@Autowired
SessionFactory sessionFactory;

private static final Logger LOG = Logger.getLogger(HibernateDAO.class.getName());

@Override
public void setClazz(Class<T> clazzToSet) {
    this.clazz = clazzToSet;
}

@Override
public void create(T entity) {
    session = getCurrentSession();
    LOG.log(Level.INFO, "inside create entity and you just bind your session to the current one{0}", session.toString());
    session.saveOrUpdate(entity);
    LOG.info("saved");
    session.flush();
    session.refresh(entity);
}



protected Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
}  
}
这是我的用户实体

@Entity
@Table(name = "users")
public class User implements Serializable, AbstractEntity {


@Id
@Column(name = "username",unique = true)
private String userName;

@NotNull(message = "password cannot be empty")
@Column(name = "users_password")
private String userPassword;

@Embedded
private UserProfile userProfile;

@Embedded
private AuditInfo auditInfo;

@Enumerated(EnumType.STRING)
private UserType userType;

@Column(name = "is_active")
private boolean active = true;

//getter n setter

}

在聊天中,您向我提供了以下代码:

 public static void main(String[] args) {
     User user = new User();
     IGenericDao<User> iGenericDao = new HibernateDAO<>();
     user.setUserName("bibekshakya35");
     user.setUserPassword("ros3");
     user.setUserType(UserType.GUEST);
     user.setActive(true);
     UserProfile userProfile = new UserProfile();
     userProfile.setAge(21);
     userProfile.setBasicInfo("kdhsa");
     userProfile.setEmailId("dsadas@gmail.com");
     userProfile.setUserGender(UserGender.Male);
     userProfile.setFullname("bibek shakya");
     userProfile.setMobileNumber("45454545");
     userProfile.setLandLineNumber("445444");
     userProfile.setUserProfilePic("index.jsp");
     user.setUserProfile(userProfile);
     AuditInfo auditInfo = new AuditInfo();
     auditInfo.setCreatedOn(new Date());
     auditInfo.setModifiedOn(new Date());
     auditInfo.setVerifiedOn(new Date());
     user.setAuditInfo(auditInfo);
     iGenericDao.create(user);
   }

HibernateDAO所在包的名称是什么?@shazin它位于“io.github.bibekshakya35.ehealth.DAO.genericdao”包下如何使用HibernateDAO?通过接口Igenericdao可以添加stackTrace吗?
 public static void main(String[] args) {
     User user = new User();
     IGenericDao<User> iGenericDao = new HibernateDAO<>();
     user.setUserName("bibekshakya35");
     user.setUserPassword("ros3");
     user.setUserType(UserType.GUEST);
     user.setActive(true);
     UserProfile userProfile = new UserProfile();
     userProfile.setAge(21);
     userProfile.setBasicInfo("kdhsa");
     userProfile.setEmailId("dsadas@gmail.com");
     userProfile.setUserGender(UserGender.Male);
     userProfile.setFullname("bibek shakya");
     userProfile.setMobileNumber("45454545");
     userProfile.setLandLineNumber("445444");
     userProfile.setUserProfilePic("index.jsp");
     user.setUserProfile(userProfile);
     AuditInfo auditInfo = new AuditInfo();
     auditInfo.setCreatedOn(new Date());
     auditInfo.setModifiedOn(new Date());
     auditInfo.setVerifiedOn(new Date());
     user.setAuditInfo(auditInfo);
     iGenericDao.create(user);
   }
@Controller
@RequestMapping(value="/someURL")
public class mainClass {
    @Autowired
    HibernateDAO abc;
    //Code (methods) to test Your CRUD Operations
}