相关疑难解决方法(0)

Spring Framework中applicationContext.xml和spring-servlet.xml之间的区别

  • applicationContext.xmlspring-servlet.xml在Spring框架无论如何有关系吗?
  • 声明的属性文件applicationContext.xml是否可用DispatcherServlet
  • 在相关的说明中,为什么我需要一个*-servlet.xml?为什么applicationContext.xml单独不足?

java spring

361
推荐指数
5
解决办法
17万
查看次数

将applicationContext拆分为多个文件

将Spring的配置拆分为多个xml文件的正确方法是什么?

目前我有

  • /WEB-INF/foo-servlet.xml
  • /WEB-INF/foo-service.xml
  • /WEB-INF/foo-persistence.xml

web.xml有以下几点:

<servlet>
    <description>Spring MVC Dispatcher Servlet</description>
    <servlet-name>intrafest</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/foo-*.xml
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/foo-*.xml
    </param-value>
</context-param>


<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

实际问题:

  • 这种方法是正确/最好的吗?
  • 我真的需要同时指定中的配置位置DispatcherServlet context-param板块?

为了能够引用foo-servlet.xml从中定义的bean,我需要记住foo-service.xml什么?这是否与指定contextConfigLocation有关web.xml

更新1:

我正在使用Spring framework 3.0.我的理解是,我不需要像这样进行资源导入:

 <import resource="foo-services.xml"/> 
Run Code Online (Sandbox Code Playgroud)

这是正确的假设吗?

configuration spring

58
推荐指数
4
解决办法
11万
查看次数

Spring:web.xml中的namespace vs contextConfigLocation init参数

我正在阅读Spring MVC的文档,我有一个关于init params的问题.如果重要,我正在使用Spring 3.2.contextConfigLocation和命名空间有什么区别?contextConfigLocation是否仅用于指定上下文类可以找到XML定义的文件夹,而namespace属性用于指定文件名?

<servlet>
        <servlet-name>AppServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF</param-value>
        </init-param>
        <init-param>
            <param-name>namespace</param-name>
            <param-value>application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
Run Code Online (Sandbox Code Playgroud)

它是否正确?它应该使用/WEB-INF/application-context.xml吗?你应该指定路径吗?

spring dependency-injection spring-mvc

57
推荐指数
1
解决办法
8万
查看次数

Spring文件上传 - 获得预期的MultipartHttpServletRequest:是否配置了MultipartResolver?错误

我正在尝试使用angular-file-upload在我的Angular Web应用程序中合并多个文件上传功能.目前,前端功能有效,但每次上传尝试都会抛出一个

java.lang.IllegalStateException,java.io.IOException]: 
    java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: 
        is a MultipartResolver configured?
Run Code Online (Sandbox Code Playgroud)

例外.

上传控制器定义为

@Controller
@PropertySource("classpath:application.properties")
public class FileUploadController {

    @Resource
    private Environment env;

    @RequestMapping(value = "/fileupload", method = RequestMethod.POST)
    @ResponseBody
    public List<String> fileUpload(@RequestParam("file") MultipartFile[] uploadFiles) throws IllegalStateException, IOException {
        //file processing logic
    }
 }
Run Code Online (Sandbox Code Playgroud)

在我的AppConfig.java课上,我宣布了豆子

@Bean
public CommonsMultipartResolver commonsMultipartResolver(){
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    commonsMultipartResolver.setDefaultEncoding("utf-8");
    commonsMultipartResolver.setMaxUploadSize(50000000);
    return commonsMultipartResolver;
}
Run Code Online (Sandbox Code Playgroud)

并启动Web应用程序

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(AppConfig.class);
    servletContext.addListener(new ContextLoaderListener(ctx));

    ctx.setServletContext(servletContext); …
Run Code Online (Sandbox Code Playgroud)

java spring file-upload angularjs

25
推荐指数
2
解决办法
3万
查看次数

Spring @Transactional无法正常工作

我之前有一个关于这个问题的帖子已经解决了.但是,自从使用自动连线bean和较少的XML配置重建项目后,我发现我正在重新审视此问题.我已按照我之前的项目实施此方式的方式,但它不起作用.有人可以帮助我解决为什么或我应该改变什么来使它工作?

我故意在插入用户详细信息方法中使用不存在的表名来故意抛出异常.但是,不会回滚插入用户和插入用户角色的语句.请帮忙.


我目前的注册设计是这样的.

部分servlet.xml中:

<context:component-scan base-package="com.doyleisgod.golfer.controllers"/>
<context:component-scan base-package="com.doyleisgod.golfer.dao"/>
<context:component-scan base-package="com.doyleisgod.golfer.services"/>
<context:component-scan base-package="com.doyleisgod.golfer.validators"/>
Run Code Online (Sandbox Code Playgroud)


部分应用程序上下文:

<context:annotation-config />
<tx:annotation-driven />    

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)


注册控制器:

package com.doyleisgod.golfer.controllers;

import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.doyleisgod.golfer.formdata.RegistrationForm;
import com.doyleisgod.golfer.services.IRegistrationService;
import com.doyleisgod.golfer.validators.RegistrationFormValidator;

/**
 * …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc transactional spring-transactions

23
推荐指数
1
解决办法
5万
查看次数

Spring中不同语境的目的和关系

我正在使用春季启动项目.我想了解不同背景的目的和关系?

例如,Spring Security上下文,Spring Context,Servlet Context等(还有更多的上下文吗?)

java spring spring-security

18
推荐指数
2
解决办法
1901
查看次数

JPA和Hibernate中的LazyInitializationException

我知道这个问题在这里和互联网上已被多次询问,我已经阅读了很多这些答案,但我仍然不明白解决这个问题的正确方法.我正在尝试使用Spring MVC和JPA,每次访问一个延迟加载的属性时,我都会得到一个LazyInitializationException.

以下是我正在尝试的一些代码:

@Repository
public class MyDAO {
    private static final Logger logger = LoggerFactory.getLogger(MyDAO.class);

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void logDOI() {
        DOI myDOI = em.find(DOI.class, Long.valueOf(1));

        // This line gives the expected output
        logger.info("Fetched DOI: " + myDOI.getDoiKey());

        // This line throws the LazyInitalizationException
        for(DOIMembership m : myDOI.getDoiMemberships()) {
            logger.info("Got DOI Membership id: " + m.getId());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在访问的实体:

@Entity
@Table(name="DOI")
public class DOI implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="DOI_ID_GENERATOR", …
Run Code Online (Sandbox Code Playgroud)

spring hibernate jpa

14
推荐指数
1
解决办法
3万
查看次数

之间的关联:SpringIocContainer | ApplicationContext | 的WebApplicationContext

背景

阅读1 2 3 4 5 6链接后我得出以下结论 -

由于Spring MVC设计了standered servlets,并促进相同的功能servlet contextapplication context.在春季存在两种类型的上下文ApplicationContextWebApplicationContext-

ApplicationContext初始化ContextLoaderListener,每个应用程序的单个实例. WebApplicationContext每个人加载DispatcherServlet.

我们可以理解上面这样ApplicationContext延伸,WebApplicationContext所以ApplicationContext最终与之相关的东西都是其中的一部分WebApplicationContext.

疑惑

  1. ApplicationContextAware提供哪个context对象.

    public class SomeThing implements ApplicationContextAware{
    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeanException{
    //this context object is `ApplicationContext` or `WebApplicationContext`?
     }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. context并且container似乎是我们大多数人的同义词,我想举个例子.假设我们有两个调度程序servlet一个用于 rest和另一个用于mvc.

    第一个调度员 -

    public class …
    Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc ioc-container

11
推荐指数
1
解决办法
936
查看次数

java.lang.IllegalStateException:BindingResult和bean名称'category'的普通目标对象都不可用作请求属性

我在网上查看了几乎所有与此问题相关的答案,但无法在我的代码中找出问题所在.

这是我的JSP页面.

<form:form method="POST" commandName="category" modelAttribute="category" action="search_category">
    <form:input path="category_name" /> 
    <input type="submit" value="Submit">  
</form:form>
Run Code Online (Sandbox Code Playgroud)

当我删除

<form:input path="category_name" /> 
Run Code Online (Sandbox Code Playgroud)

它工作正常.我可以与我的控制器通信.所以问题与这条线有关.

@Controller
public class SearchCategory {

    @Autowired      
    private CategoryService categoryService;

    @RequestMapping(value = "/search_category",  method = RequestMethod.POST)
    public @ResponseBody String searchCategoryFromDatabase(@ModelAttribute("category") Category category, BindingResult result){        

        return "something";
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

     <!-- The definition of the Root …
Run Code Online (Sandbox Code Playgroud)

java spring annotations hibernate spring-form

6
推荐指数
1
解决办法
4万
查看次数

Swagger UI没有列出任何控制器/端点,尽管我能够在v2 / api-docs端点下看到json

我无法使Swagger UI与我的项目一起使用。Swagger UI很好用,但是它没有列出我的任何REST控制器。

我正在使用SPRING 4.2.6.RELEASE和Swagger 2.5.0。我的其余服务已部署到Tomcat 7.0.54。

当Tomcat 7.0.54出现时,它能够获取迅速的端点。我能够找到提取json消息的端点v2 / api-docs。我也可以点击swagger-ui,但没有列出任何控制器。下拉列表为空,如下所示

在此处输入图片说明

**我目前面临的问题是

  1. 我无法获取/ swagger-resources / configuration / ui,而当我启动swagger UI时却遇到404(未找到)错误,而该UI试图获取/ swagger-resources / configuration / ui。我为swagger-resources安装了资源处理程序,但这似乎无济于事。您能否让我知道可能会丢失什么?
  2. 我应该在扩展的WAR中看到META-INF下的resources文件夹吗?META-INF内应该有任何与springfox相关的文件/文件夹吗?**

Swagger的Maven依赖项
io.springfox springfox-swagger2 2.5.0
io.springfox springfox-swagger-ui 2.5.0

以下是我的SwaggerCongifuration

@EnableSwagger2
public class SwaggerConfiguration {

@Bean
public Docket api() {
    List<SecurityContext> security = new ArrayList<SecurityContext>();
    security.add(securityContext());
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .pathMapping("/").securityContexts(security);
}

private SecurityContext securityContext() {
    return SecurityContext.builder()
            .forPaths(PathSelectors.regex("/"))
            .build();
 }
}
Run Code Online (Sandbox Code Playgroud)

下面是我的WebConfig.xml

@EnableWebMvc
@Configuration
@Import(SwaggerConfiguration.class)
@ComponentScan("com.bank.direct.services")

public class WebConfig extends WebMvcConfigurerAdapter {


@Override …
Run Code Online (Sandbox Code Playgroud)

rest swagger swagger-ui spring-4 swagger-2.0

6
推荐指数
2
解决办法
6670
查看次数