Tag: 弹簧

为什么要使用Spring的ApplicationContext层次结构?

我想了解Spring的ApplicationContext层次结构。 我学到了以下 一个ApplicationContext不能有一个以上的父ApplicationContext。 当一个给定的ApplicationContext无法parsing一个bean时,它会将parsing请求传递给它的父类。 ApplicationContext的父级在其构造函数中指定。 我想了解何时使用ApplicationContext层次结构(而不​​是一个单一的ApplicationContext)。 我可以从谷歌最好的是这个 。 而我所理解的是,如果一个应用程序在各个层次上定义了大量的bean,那么每一个有自己的ApplicationContext的层将是一个好处。 不明白的是,这样做的好处是什么,好处如何? TIA,维杰

Spring 3.0 MVC绑定枚举区分大小写

如果我有一个像这样的Spring控制器RequestMapping … @RequestMapping(method = RequestMethod.GET, value = "{product}") public ModelAndView getPage(@PathVariable Product product) 产品是一个枚举。 例如。 Product.Home 当我请求该页面时,mysite.com/home 我明白了 Unable to convert value "home" from type 'java.lang.String' to type 'domain.model.product.Product'; nested exception is java.lang.IllegalArgumentException: No enum const class domain.model.product.Product.home 有没有办法让枚举types转换器明白,小写的家居然是家? 我想保持url不区分大小写和我的Java枚举与标准大写字母。 谢谢 解 public class ProductEnumConverter extends PropertyEditorSupport { @Override public void setAsText(final String text) throws […]

注入和资源和自动assembly注释

@Inject和@Resource和@Autowired注释之间有什么区别? 我们应该什么时候使用它们?

如何防止在Spring 3.0.5中解释逗号的参数绑定?

考虑下面的控制器方法: @RequestMapping(value = "/test", method = RequestMethod.GET) public void test(@RequestParam(value = "fq", required = false) String[] filterQuery) { logger.debug(fq = " + StringUtils.join(filterQuery, "|")); } 这里是不同的fq组合的输出: /test?fq=foo导致fq = foo /test?fq=foo&fq=bar得到fq = foo|bar /test?fq=foo,bar导致fq = foo|bar /test?fq=foo,bar&fq=bash得到fq = foo,bar|bash /test?fq=foo,bar&fq= fq = foo,bar| 例3是这个问题。 我期望(需要/需要)它输出fq = foo,bar 。 我已经尝试用逗号\来避开逗号,并且使用%3C但是没有任何工作。 如果我看一下HttpServletRequest对象的版本: String[] fqs = request.getParameterValues("fq"); logger.debug(fqs = […]

Spring RedirectAttributes:addAttribute vs addFlashAttribute

我的理解到目前为止是在我们的控制器请求映射方法,我们可以指定RedirectAttributes参数,并填充与请求redirect时的属性,例如: @RequestMapping(value="/hello", method=GET) public String hello(RedirectAttributes redirAttr) { // should I use redirAttr.addAttribute() or redirAttr.addFlashAttribute() here ? // … return "redirect:/somewhere"; } redirect属性将在redirect到的目标页面上可用。 不过,RedirectAttributes有两个方法:addAttribute和addFlashAttribute。 这两者之间的根本区别是什么,我该如何select使用哪一个呢? 一直在阅读春季手册文件一段时间,但我有点失落

Spring中的BeanPostProcessor和init / destroy方法有什么区别?

实现BeanPostProcessor接口与在Spring的XMLconfiguration文件中使用init / destroy方法属性还是实现InitializingBean / DisposableBean接口有什么区别?

春季testing和安全:如何模拟身份validation?

我想弄清楚如何对我的控制器的URL进行适当的保护。 以防万一有人改变事情,并意外删除安全设置。 我的控制器方法如下所示: @RequestMapping("/api/v1/resource/test") @Secured("ROLE_USER") public @ResonseBody String test() { return "test"; } 我设置了一个WebTestEnvironment,如下所示: import javax.annotation.Resource; import javax.naming.NamingException; import javax.sql.DataSource; import org.junit.Before; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.web.FilterChainProxy; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import […]

使用@PropertySource批注时,@Value未parsing。 如何configurationPropertySourcesPlaceholderConfigurer?

我有以下configuration类: @Configuration @PropertySource(name = "props", value = "classpath:/app-config.properties") @ComponentScan("service") public class AppConfig { 我有财产的服务: @Component public class SomeService { @Value("#{props['some.property']}") private String someProperty; 我想要testingAppConfigconfiguration类时收到错误 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String service.SomeService.someProperty; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested […]

如何在绑定spring mvc命令对象时自定义参数名称

我有一个命令对象: public class Job { private String jobType; private String location; } 哪一个是由spring-mvc绑定的: @RequestMapping("/foo") public Strnig doSomethingWithJob(Job job) { … } 这适用于http://example.com/foo?jobType=permanent&location=Stockholm 。 但是现在我需要使它适用于下面的url: http://example.com/foo?jt=permanent&loc=Stockholm 显然,我不想改变我的命令对象,因为字段名称必须保持很长(因为它们在代码中使用)。 我如何定制? 有没有办法做这样的事情: public class Job { @RequestParam("jt") private String jobType; @RequestParam("loc") private String location; } 这不起作用( @RequestParam不能应用于字段)。 我在想的是一个类似于FormHttpMessageConverter的自定义消息转换器,并读取目标对象上的自定义注释

如何将钩子添加到应用程序上下文初始化事件?

对于一个常规的Servlet,我想你可以声明一个上下文监听器 ,但是对于Spring MVC,Spring会让这个更简单吗? 此外,如果我定义一个上下文监听器,然后需要访问在我的servlet.xml或applicationContext.xml定义的bean,我将如何访问它们?