Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 675

Getting "No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?"exception in spring hibernate app

$
0
0

I am getting this error when I am trying to load a spring form other solutions that I am getting is of adding a listener in web.xml but currently I am using a java based configuration

ERROR No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered? java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?

following are my configuration files

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {    @Override    protected Class<?>[] getRootConfigClasses() {        return null;    }    @Override    protected Class<?>[] getServletConfigClasses() {        return new Class[]{HibernateConfigImpl.class};    }    @Override    protected String[] getServletMappings() {        return new String[]{"/"};    }}
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {}
import com.nucleus.service.security.PersonUserDetails;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@Configuration@EnableWebSecurity@ComponentScan("com.nucleus")public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {    @Autowired    PersonUserDetails personUserDetails;    @Autowired    public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception{        System.out.println("ffff");        authenticationManagerBuilder.userDetailsService(personUserDetails).passwordEncoder(getBCrypPasswordEncoder());    }    @Override    protected void configure(HttpSecurity http) throws Exception {        http                .csrf()                .disable()                .authorizeRequests()                .antMatchers("/maker/").hasRole("MAKER")                .antMatchers("/checker/").hasRole("CHECKER")                .anyRequest()                .authenticated()                .and()                .formLogin();    }    @Bean    public BCryptPasswordEncoder getBCrypPasswordEncoder(){        return new BCryptPasswordEncoder(10);    }}
import com.nucleus.model.security.Person;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.cfg.Environment;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.web.servlet.view.InternalResourceViewResolver;import java.io.IOException;import java.io.InputStream;import java.util.Properties;@org.springframework.context.annotation.Configuration@ComponentScan("com.nucleus")public class HibernateConfigImpl {    private static SessionFactory factory = null;    private static Session session = null;    private static final Configuration CONFIGURATION = new Configuration();    @Bean    public InternalResourceViewResolver viewResolver() {        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();        viewResolver.setPrefix("/WEB-INF/views/");        viewResolver.setSuffix(".jsp");        return viewResolver;    }    public SessionFactory getSessionFactory(){        InputStream input = getClass().getClassLoader()                .getResourceAsStream("hibernate.properties");        Properties properties = new Properties();        try {            properties.load(input);            properties.put(Environment.DRIVER, properties.get("hibernate.connection.driver_class"));            properties.put(Environment.URL, properties.get("hibernate.connection.url"));            properties.put(Environment.USER, properties.get("hibernate.connection.username"));            properties.put(Environment.PASS, properties.get("hibernate.connection.password"));            properties.put(Environment.DIALECT, properties.get("hibernate.dialect"));            properties.put(Environment.HBM2DDL_AUTO, properties.get("hibernate.hbm2ddl.auto"));            properties.put(Environment.SHOW_SQL, properties.get("hibernate.show_sql"));            properties.put(Environment.FORMAT_SQL, properties.get("hibernate.format_sql"));            CONFIGURATION.addProperties(properties);            CONFIGURATION.addAnnotatedClass(Person.class);        }catch(IOException e){            e.printStackTrace();        }        factory = CONFIGURATION.buildSessionFactory();        return factory;    }    // here we are achieving SRP as one method is responsible for one Functionality    public Session getSession(){        getSessionFactory();        session = factory.openSession();        return session;    }    public void shutdown(){        factory.close();    }}

I tried adding a web.xml file and in that I added a listener but then nothing works in the app further I even tried changing the version of the servlet and spring dependencies but still the samefollowing is the spring on accessing which I am getting the exception

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %><html><head><title>Deviation Form</title></head><body><h2>Deviation Form</h2><form:form method="post" action="/saveDeviation" modelAttribute="deviation"><table><tr><td>Deviation Code:</td><td><form:input path="deviationCode" /></td></tr><tr><td>Deviation Name:</td><td><form:input path="deviationName" /></td></tr><tr><td>Deviation Description:</td><td><form:input path="deviationDescription" /></td></tr><tr><td>Role:</td><td><form:input path="role" /></td></tr><tr><td>Deviation Level:</td><td><form:input path="deviationLevel" /></td></tr><tr><td colspan="2"><input type="submit" value="Save" /></td></tr></table></form:form></body></html>

Viewing all articles
Browse latest Browse all 675

Trending Articles