Java 创建名为';defaultServletHandlerMapping

Java 创建名为';defaultServletHandlerMapping,java,maven,spring-mvc,servlets,spring-security,Java,Maven,Spring Mvc,Servlets,Spring Security,这是eclipse上的spring(具有spring安全)+java+maven应用程序。我在提交注册表单时遇到以下错误。随后请参阅我的其他文件: HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandler

这是eclipse上的spring(具有spring安全)+java+maven应用程序。我在提交注册表单时遇到以下错误。随后请参阅我的其他文件:

HTTP Status 500 - Request processing failed; nested exception is 
org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'defaultServletHandlerMapping' defined in class 
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: 
Bean instantiation via factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[org.springframework.web.servlet.HandlerMapping]:
 Factory method 'defaultServletHandlerMapping'
 threw exception; nested exception is java.lang.IllegalArgumentException:
 A ServletContext is required to configure default servlet handling
我的文件:

AppInit

package com.myapp.config;

import org.springframework.security.web.context.*;

public class AppInit       extends AbstractSecurityWebApplicationInitializer                    

}
MyApp配置文件:

package com.myapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.myapp.JDBC.EmailJDBC;
import com.myapp.JDBC.LastIdJDBC;
import com.myapp.JDBC.LoginJDBC;
import com.myapp.JDBC.PersonJDBC;

@EnableWebMvc //mvc:annotation-driven
@Configuration

@ComponentScan(basePackages ={ "com.myapp" })//, excludeFilters = { 
//@Filter(type = FilterType.ANNOTATION, value = Configuration.class) })

public class myappConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

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

    @Bean
    public DriverManagerDataSource getDatasource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setPassword("1234567");
        dataSource.setUrl("jdbc:mysql://localhost:3306/myapp");
        dataSource.setUsername("root");
        return dataSource;
    }
    @Bean
    public LoginJDBC getLoginBean(){
        LoginJDBC bean = new LoginJDBC();
        bean.setDataSource(new myappConfig().getDatasource());
        return bean;
    }
    @Bean
    public PersonJDBC getPersonBean(){
        PersonJDBC bean = new PersonJDBC();
        bean.setDataSource(new myappConfig().getDatasource());
        return bean;
    }
    @Bean
    public EmailJDBC getEmailBean(){
        EmailJDBC bean = new EmailJDBC();
        bean.setDataSource(new myappConfig().getDatasource());
        return bean;
    }
    @Bean
    public LastIdJDBC getLastIdBean(){
        LastIdJDBC bean = new LastIdJDBC();
        bean.setDataSource(new myappConfig().getDatasource());
        return bean;
    }
}
package com.myapp.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.*;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Autowired
    private CsrfTokenRepository csrfTokenRepository() 
    { 
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
        repository.setSessionAttributeName("_csrf");
        return repository; 
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

    http
        .csrf()
            .csrfTokenRepository(csrfTokenRepository())
            .and()
        .authorizeRequests()
            .antMatchers("/resources/**", "/signuPerson").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            //.failureUrl(authenticationFailureUrl)
            .permitAll()
            .and()
        .logout()
            .permitAll();


    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}
@EnableWebMvc 
@Configuration
@ComponentScan(
  basePackages ={ "com.myapp" }, 
  excludeFilters = { 
    @Filter(type = FilterType.ANNOTATION, value = Configuration.class)
  }
)
WebInit文件:

package com.myapp.config;

import javax.servlet.Filter;

import org.springframework.core.annotation.Order;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;


public class myappWebInit extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { myappConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/","/login", "/signuPerson","/regPerson","/regPersonSuccess" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RootConfiguration.class };
    }
    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new HiddenHttpMethodFilter() };
    }
}

我很抱歉放了很多东西。我已经尝试过解决类似的问题,但没有成功。任何暗示都将不胜感激。提前谢谢

我在这个平台上的一个类似问题的解决方案的帮助下解决了这个问题

通过在myappconfig文件中添加以下注释,我排除了扫描配置筛选器:

package com.myapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.myapp.JDBC.EmailJDBC;
import com.myapp.JDBC.LastIdJDBC;
import com.myapp.JDBC.LoginJDBC;
import com.myapp.JDBC.PersonJDBC;

@EnableWebMvc //mvc:annotation-driven
@Configuration

@ComponentScan(basePackages ={ "com.myapp" })//, excludeFilters = { 
//@Filter(type = FilterType.ANNOTATION, value = Configuration.class) })

public class myappConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

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

    @Bean
    public DriverManagerDataSource getDatasource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setPassword("1234567");
        dataSource.setUrl("jdbc:mysql://localhost:3306/myapp");
        dataSource.setUsername("root");
        return dataSource;
    }
    @Bean
    public LoginJDBC getLoginBean(){
        LoginJDBC bean = new LoginJDBC();
        bean.setDataSource(new myappConfig().getDatasource());
        return bean;
    }
    @Bean
    public PersonJDBC getPersonBean(){
        PersonJDBC bean = new PersonJDBC();
        bean.setDataSource(new myappConfig().getDatasource());
        return bean;
    }
    @Bean
    public EmailJDBC getEmailBean(){
        EmailJDBC bean = new EmailJDBC();
        bean.setDataSource(new myappConfig().getDatasource());
        return bean;
    }
    @Bean
    public LastIdJDBC getLastIdBean(){
        LastIdJDBC bean = new LastIdJDBC();
        bean.setDataSource(new myappConfig().getDatasource());
        return bean;
    }
}
package com.myapp.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.*;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Autowired
    private CsrfTokenRepository csrfTokenRepository() 
    { 
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
        repository.setSessionAttributeName("_csrf");
        return repository; 
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

    http
        .csrf()
            .csrfTokenRepository(csrfTokenRepository())
            .and()
        .authorizeRequests()
            .antMatchers("/resources/**", "/signuPerson").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            //.failureUrl(authenticationFailureUrl)
            .permitAll()
            .and()
        .logout()
            .permitAll();


    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}
@EnableWebMvc 
@Configuration
@ComponentScan(
  basePackages ={ "com.myapp" }, 
  excludeFilters = { 
    @Filter(type = FilterType.ANNOTATION, value = Configuration.class)
  }
)
这样做之后,问题没有得到解决

然后我删除了
@EnableWebMvc
,并将其放入myappinit文件,问题就解决了

我猜
@EnableWebMvc
@ComponentScan(basePackages={“com.myapp”},excludeFilters={

@筛选器(type=FilterType.ANNOTATION,value=Configuration.class)})
不应位于同一配置文件中。

请尝试删除文件AppInit一秒钟,然后重新生成应用程序!告诉我它是否有用@阿琼先生,我试过了,但问题并没有解决。我想问题在于注册控制器的级别。不知何故,请求处理程序没有得到很好的定义。但我不知道该怎么做。此外,我找不到程序试图创建和实例化的“defaultHandlerBean”。谢谢!