尝试在spring-mvc上的应用程序中注册Config时出错?

时间:2018-09-25 13:07:51

标签: spring-mvc tomcat

在spring-mvc + tomcat上的应用(不带spring-boot)

有项目结构: enter image description here

因此,当我尝试将战争部署到本地tomcat时,会出现错误消息

  

原因:java.lang.NoClassDefFoundError:   org / aspectj / lang / annotation / Around at   org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory。(ReflectiveAspectJAdvisorFactory.java:77)     在   org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.initBeanFactory(AnnotationAwareAspectJAutoProxyCreator.java:78)     在   org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.setBeanFactory(AbstractAdvisorAutoProxyCreator.java:60)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1648)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1617)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)     ... 56更多原因:java.lang.ClassNotFoundException:   org.aspectj.lang.annotation.Around在   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)     在   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)     ...另外62个

package init;

    @Configuration
    @EnableWebMvc
    @ComponentScan("com")
    public class WebAppInitalizer implements WebApplicationInitializer {
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
            rootContext.register(
                    WebConfig.class);

            FilterRegistration charEncodingfilterReg = servletContext.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class);
            charEncodingfilterReg.setInitParameter("encoding", "UTF-8");
            charEncodingfilterReg.setInitParameter("forceEncoding", "true");
            charEncodingfilterReg.addMappingForUrlPatterns(null, false, "/*");

            servletContext.addListener(new ContextLoaderListener(rootContext));

            ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
            dispatcher.setLoadOnStartup(1);
            dispatcher.addMapping("/*");
        }
    }

WebConfig

@Configuration
@ComponentScan(basePackages = {"com"})
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter());
        converters.add(new StringHttpMessageConverter());
    }

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping mhm = new RequestMappingHandlerMapping();
        //suffix parser is off for urls like .../search/timeKey.key:some_key
        mhm.setUseSuffixPatternMatch(false);
        return mhm;
    }
}

和控制器:

@RestController
public class GreetingController {
    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    public String greeting() {
        return "greeting";
    }
}

applicationContext.xml和web.xml为空。 因此,尝试在WebAppInitializer中注册WebConfig时会发生错误。

1 个答案:

答案 0 :(得分:0)

添加

// some file .../Utils
class Utils{
  static someMethod({arg1, arg2, ...}){
    ...do something
  }
}
export default Utils;

// In my resolvers
import Utils from '.../Utils'
const resolvers = {
  Mutation: {
    const a = ...;
    const b = ...;
    const c = Utils.someMethod({a, b, ...}); 
  }
}

解决了我的问题。