标签: spring-annotations

带有点(.)的Spring MVC @PathVariable被截断了

这是Spring MVC @PathVariable被截断的问题的延续

Spring论坛声明它已修复(3.2版本)作为ContentNegotiationManager的一部分.见下面的链接.
https://jira.springsource.org/browse/SPR-6164
https://jira.springsource.org/browse/SPR-7632

在我的应用程序中,带有.com的requestParameter被截断.

谁能解释我如何使用这个新功能?它是如何在xml中配置的?

注意:spring论坛 - #1带有点(.)的Spring MVC @PathVariable正在被截断

rest spring spring-mvc spring-annotations

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

在单元测试期间填充Spring @Value

我正在尝试为我的程序中用于验证表单的简单bean编写单元测试.bean使用注释@Component并具有使用初始化的类变量@Value("${this.property.value}") private String thisProperty;

我想为这个类中的验证方法编写单元测试,但是,如果可能的话,我想在不使用属性文件的情况下这样做.我的理由是,如果我从属性文件中提取的值发生变化,我希望这不会影响我的测试用例.我的测试用例是测试验证值的代码,而不是值本身.

有没有办法在我的测试类中使用Java代码来初始化Java类并在该类中填充Spring @Value属性然后使用它来测试?

我确实发现这个How To看起来很接近,但仍然使用属性文件.我宁愿这一切都是Java代码.

谢谢

java junit spring spring-annotations

203
推荐指数
8
解决办法
18万
查看次数

是否有办法@Autowire需要构造函数参数的bean?

我正在使用Spring 3.0.5并尽可能地为我的班级成员使用@Autowire注释.我需要自动装配的bean之一需要其构造函数的参数.我查看了Spring文档,但似乎找不到任何关于如何注释构造函数参数的引用.

在XML中,我可以将其用作bean定义的一部分.@Autowire注释是否有类似的机制?

例如:

@Component
public class MyConstructorClass{

  String var;
  public MyConstructorClass( String constrArg ){
    this.var = var;
  }
...
}


@Service
public class MyBeanService{
  @Autowired
  MyConstructorClass myConstructorClass;

  ....
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,如何使用@Autowire注释在MyBeanService中指定"constrArg"的值?有没有办法做到这一点?

谢谢,

埃里克

spring constructor autowired spring-annotations spring-bean

102
推荐指数
4
解决办法
14万
查看次数

我可以在Spring中将null设置为@Value的默认值吗?

我目前正在使用@Value Spring 3.1.x注释,如下所示:

@Value("${stuff.value:}")
private String value;
Run Code Online (Sandbox Code Playgroud)

如果属性不存在,这会将空字符串放入变量中.我想将null作为默认值而不是空字符串.当然,我还想避免在未设置属性stuff.value时出错.

java spring spring-annotations

83
推荐指数
3
解决办法
6万
查看次数

@Valid注释在Spring中表示什么?

在以下示例中,ScriptFile参数标有@Valid注释.

是什么@Valid注解吗?

@RequestMapping(value = "/scriptfile", method = RequestMethod.POST)    
public String create(@Valid ScriptFile scriptFile, BindingResult result, ModelMap modelMap) {    
    if (scriptFile == null) throw new IllegalArgumentException("A scriptFile is required");        
    if (result.hasErrors()) {        
        modelMap.addAttribute("scriptFile", scriptFile);            
        modelMap.addAttribute("showcases", ShowCase.findAllShowCases());            
        return "scriptfile/create";            
    }        
    scriptFile.persist();        
    return "redirect:/scriptfile/" + scriptFile.getId();        
}    
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-annotations

70
推荐指数
5
解决办法
14万
查看次数

如何在web.xml中注册Spring @Configuration带注释的类而不是applicationContext.xml文件?

我在web应用程序中一起使用jsf和spring.我已经在一个使用注释@Configuration, @ComponentScan等的配置类中配置了数据源和会话工厂.我的项目中没有任何applicationContext.xml文件,因为我正在处理Configuration类中的每个上下文xml条目.测试用例成功运行但是当我部署我的Web应用程序时,它给了我错误

java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener?

现在如果我在web.xml中给出listener类,

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

它给了我错误,

找不到/WEB-INF/applicationContext.xml

按照的文件ContextLoaderListener,这是真的,如果我不给contextConfigLocation在参数web.xml明确,它会搜索名为默认的Spring上下文文件applicationContext.xmlweb.xml.现在,如果我不想使用spring上下文文件并使用注释进行所有配置,我该怎么办?我应该如何注册监听器类,ContextLoaderListener以便不使用xml文件和仅使用注释,我能够使用spring和jsf运行我的Web应用程序?

java spring spring-annotations spring-3

68
推荐指数
2
解决办法
9万
查看次数

如何在Spring中自动装配泛型类型<T>的Bean?

我有一个bean Item<T>需要在@Configuration课堂上自动装配.

@Configuration
public class AppConfig {

    @Bean
    public Item<String> stringItem() {
        return new StringItem();
    }

    @Bean
    public Item<Integer> integerItem() {
        return new IntegerItem();
    }

}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试时@Autowire Item<String>,我得到以下异常.

"No qualifying bean of type [Item] is defined: expected single matching bean but found 2: stringItem, integerItem"
Run Code Online (Sandbox Code Playgroud)

我应该如何Item<T>在Spring中自动加载通用类型?

java generics spring autowired spring-annotations

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

以编程方式使用Spring调度作业(动态设置fixedRate)

目前我有这个:

@Scheduled(fixedRate=5000)
public void getSchedule(){
   System.out.println("in scheduled job");
}
Run Code Online (Sandbox Code Playgroud)

我可以更改它以使用对属性的引用

@Scheduled(fixedRate=${myRate})
public void getSchedule(){
   System.out.println("in scheduled job");
}
Run Code Online (Sandbox Code Playgroud)

但是,我需要使用以编程方式获得的值,以便可以在不重新部署应用程序的情况下更改计划.什么是最好的方法?我意识到使用注释可能是不可能的......

java spring scheduled-tasks spring-annotations

63
推荐指数
5
解决办法
7万
查看次数

什么是正确的注释,因为在Spring Boot Framework中不推荐使用@SpringApplicationConfiguration,@ WebIntegration?

从Spring Boot Framework 1.4 开始@SpringApplicationConfiguration,什么是正确的注释并被@WebIntegration弃用?我正试图玩单元测试.

java junit spring-annotations spring-boot

61
推荐指数
4
解决办法
5万
查看次数

如何使用@Value Spring Annotation注入Map?

如何使用Spring中的@Value注释从属性文件中将值注入Map?

我的Spring Java类是和我尝试使用$但是,得到以下错误消息

无法自动装配字段:private java.util.Map Test.standard; 嵌套异常是java.lang.IllegalArgumentException:无法解析字符串值"$ {com.test.standard}"中的占位符'com.test.standard'

@ConfigurationProperty("com.hello.foo")
public class Test {

   @Value("${com.test.standard}")
   private Map<String,Pattern> standard = new LinkedHashMap<String,Pattern>

   private String enabled;

}
Run Code Online (Sandbox Code Playgroud)

我在.properties文件中有以下属性

com.test.standard.name1=Pattern1
com.test.standard.name2=Pattern2
com.test.standard.name3=Pattern3
com.hello.foo.enabled=true
Run Code Online (Sandbox Code Playgroud)

java spring annotations dependency-injection spring-annotations

51
推荐指数
7
解决办法
7万
查看次数