Questions tagged [spring-annotations]

A common placeholder for issues related to the use of annotations with the Spring framework

The Spring Framework () provides several annotation-driven configuration options from wiring beans to managing transaction.

Annotation types

JSR support

  • JSR 107
    • @CacheResult
    • @CachePut
    • @CacheRemove
    • @CacheRemoveAll
    • @CacheDefaults
    • @CacheKey
    • @CacheValue
  • JSR 250:
    • @Resource
  • JSR 303:
    • @Valid
    • @NotNull
    • @Size, etc
  • JSR 330:
    • @Inject
    • @Named

Useful links

1301 questions
396
votes
18 answers

Spring MVC @PathVariable with dot (.) is getting truncated

This is continuation of question Spring MVC @PathVariable getting truncated Spring forum states that it has fixed(3.2 version) as part of ContentNegotiationManager. see the below…
Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
374
votes
13 answers

Populating Spring @Value during Unit Test

I'm trying to write a Unit Test for a simple bean that's used in my program to validate forms. The bean is annotated with @Component and has a class variable that is initialized using @Value("${this.property.value}") private String thisProperty; I…
Kyle
  • 14,036
  • 11
  • 46
  • 61
161
votes
5 answers

Can I set null as the default value for a @Value in Spring?

I'm currently using the @Value Spring 3.1.x annotation like this: @Value("${stuff.value:}") private String value; This puts an empty String into the variable if the attribute is not present. I would like to have null as the default instead of an…
Kevin Schmidt
  • 2,111
  • 2
  • 14
  • 12
144
votes
9 answers

Is there a way to @Autowire a bean that requires constructor arguments?

I'm using Spring 3.0.5 and am using @Autowire annotation for my class members as much as possible. One of the beans that I need to autowire requires arguments to its constructor. I've looked through the Spring docs, but cannot seem to find any…
Eric B.
  • 23,425
  • 50
  • 169
  • 316
124
votes
9 answers

What does the @Valid annotation indicate in Spring?

In the following example, the ScriptFile parameter is marked with an @Valid annotation. What does @Valid annotation do? @RequestMapping(value = "/scriptfile", method = RequestMethod.POST) public String create(@Valid ScriptFile scriptFile,…
Gary Ford
  • 1,389
  • 2
  • 9
  • 9
104
votes
6 answers

How to Autowire Bean of generic type in Spring?

I have a bean Item which is required to be autowired in a @Configuration class. @Configuration public class AppConfig { @Bean public Item stringItem() { return new StringItem(); } @Bean public Item
user3374518
  • 1,359
  • 3
  • 11
  • 11
103
votes
8 answers

How to inject a Map using the @Value Spring Annotation?

How can I inject values into a Map from the properties file using the @Value annotation in Spring? My Spring Java class is and I tried using the $, but got the following error message: Could not autowire field: private java.util.Map Test.standard;…
yathirigan
  • 5,619
  • 22
  • 66
  • 104
101
votes
8 answers

Scheduling a job with Spring programmatically (with fixedRate set dynamically)

Currently I have this : @Scheduled(fixedRate=5000) public void getSchedule(){ System.out.println("in scheduled job"); } I could change this to use a reference to a property @Scheduled(fixedRateString="${myRate}") public void getSchedule(){ …
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
79
votes
4 answers

why do we have to use @Modifying annotation for queries in Data Jpa

for example I have a method in my CRUD interface which deletes a user from the database: public interface CrudUserRepository extends JpaRepository { @Transactional @Modifying @Query("DELETE FROM User u WHERE u.id=:id") …
Artyom Emelyanenko
  • 1,323
  • 1
  • 11
  • 16
79
votes
6 answers

Overriding an Autowired Bean in Unit Tests

Is there a simple way I can easily override an autowired bean in specific unit tests? There is only a single bean of every type in the compile classes so it's not a problem for autowiring in this case. The test classes would contain additional…
samblake
  • 1,517
  • 3
  • 16
  • 33
74
votes
29 answers

Parameter 0 of constructor in required a bean of type 'java.lang.String' that could not be found

I am working on spring batch with spring boot 2.X application, actually its existing code i am checked out from git. While running the application it fails due to below error only for me and same code is working for…
Ganesh
  • 903
  • 1
  • 8
  • 18
74
votes
5 answers

What is the proper annotation since @SpringApplicationConfiguration, @WebIntegration, is deprecated in Spring Boot Framework?

What is the proper annotation since @SpringApplicationConfiguration and @WebIntegration are deprecated as of Spring Boot Framework 1.4? I'm trying to play around with unit testing.
Person
  • 880
  • 1
  • 6
  • 12
67
votes
2 answers

How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml?

I am using jsf and spring together in web application. I have configured datasource and session factory in one configuration class which uses annotations like @Configuration, @ComponentScan etc. I don't have any applicationContext.xml file in my…
Mital Pritmani
  • 4,880
  • 8
  • 38
  • 39
66
votes
5 answers

Spring MVC: please explain difference between @RequestParam and @ModelAttribute

I'm new to Spring MVC. Please help me unpack the documentation. Documentation Spring MVC Documentation states (emphasis mine): @ModelAttribute on a method argument indicates the argument should be retrieved from the model. If not present in the…
Lydia Ralph
  • 1,455
  • 1
  • 17
  • 33
57
votes
3 answers

Spring @Value escape colon(:) in default value

I have the following property annotated with @Value. I have a default value defined using the default separator of ':" @Value("${prop.url:http://myurl.com}") Is there a way to escape the ':' in http://myurl.com or do I have to define a different…
mjj1409
  • 3,075
  • 6
  • 28
  • 28
1
2 3
86 87