applicationContext.xml
和spring-servlet.xml
在Spring框架无论如何有关系吗?applicationContext.xml
是否可用DispatcherServlet
?*-servlet.xml
?为什么applicationContext.xml
单独不足?将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)
这是正确的假设吗?
我正在阅读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吗?你应该指定路径吗?
我正在尝试使用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) 我之前有一个关于这个问题的帖子已经解决了.但是,自从使用自动连线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 Security上下文,Spring Context,Servlet Context等(还有更多的上下文吗?)
我知道这个问题在这里和互联网上已被多次询问,我已经阅读了很多这些答案,但我仍然不明白解决这个问题的正确方法.我正在尝试使用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 MVC设计了standered servlets
,并促进相同的功能servlet context
和application context
.在春季存在两种类型的上下文ApplicationContext
和WebApplicationContext
-
ApplicationContext
初始化ContextLoaderListener
,每个应用程序的单个实例.
WebApplicationContext
每个人加载DispatcherServlet
.
我们可以理解上面这样ApplicationContext
延伸,WebApplicationContext
所以ApplicationContext
最终与之相关的东西都是其中的一部分WebApplicationContext
.
疑惑
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)context
并且container
似乎是我们大多数人的同义词,我想举个例子.假设我们有两个调度程序servlet一个用于
rest
和另一个用于mvc
.
第一个调度员 -
public class …
Run Code Online (Sandbox Code Playgroud)我在网上查看了几乎所有与此问题相关的答案,但无法在我的代码中找出问题所在.
这是我的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) 我无法使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,但没有列出任何控制器。下拉列表为空,如下所示
**我目前面临的问题是
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) spring ×9
java ×5
spring-mvc ×3
hibernate ×2
angularjs ×1
annotations ×1
file-upload ×1
jpa ×1
rest ×1
spring-4 ×1
spring-form ×1
swagger ×1
swagger-2.0 ×1
swagger-ui ×1