Spring Java weblogic 上的配置

Spring Java configuration on weblogic

我目前是 Spring 的新手,我正在尝试学习它的纯 java 配置元素。

我可以 运行 我的 Spring 应用 Tomcat 具有以下 classes:

配置class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.inka.spring.test.maven.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 *
 * @author User
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.inka.spring.test.maven")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }
}

初始化器class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.inka.spring.test.maven.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 *
 * @author User
 */
public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

}

控制器class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.inka.spring.test.maven.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author User
 */
@Controller
@RequestMapping("/")
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String sayHello(ModelMap model) {
        model.addAttribute("greeting", "Hello World from Spring 4 MVC");
        return "welcome";
    }

    @RequestMapping(value = "/helloagain", method = RequestMethod.GET)
    public String sayHelloAgain(ModelMap model) {
        model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
        return "welcome";
    }
}

这会调用一个 .jsp 页面,该页面会根据我在上述代码中输入的路径打印相应的消息。

但是,当我用 weblogic 尝试这个时,它不起作用。我得到的只是一个 403 和一个 404 错误。

我会坚持使用 Tomcat,但我们在我们的组织中使用 weblogic,并且我被指示构建我的应用程序以使用 weblogic。

拜托,我应该在 weblogic 上使用一些额外的配置吗?

好的,我终于解决了这个问题。事实证明,在你的初始化器 class 中,无论你扩展什么 class,如果你打算在 weblogic 上部署,你必须始终实现 WebApplicationInitializer class。您不必为 Tomcat(不知道 JBoss 和其他),但对于 weblogic,您必须实现 class.

所以,在改变这个之后:

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

为此:

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer {

一切正常!

更多信息,请访问spring guide