Questions tagged [spring-annotations]
A common placeholder for issues related to the use of annotations with the Spring framework
1,318
questions
395
votes
18
answers
176k
views
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 link.
https://github....
393
votes
13
answers
475k
views
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....
170
votes
5
answers
155k
views
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 ...
149
votes
9
answers
238k
views
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 ...
126
votes
9
answers
312k
views
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 ...
108
votes
8
answers
197k
views
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 ...
108
votes
6
answers
111k
views
How to Autowire Bean of generic type <T> in Spring?
I have a bean Item<T> which is required to be autowired in a @Configuration class.
@Configuration
public class AppConfig {
@Bean
public Item<String> stringItem() {
return ...
103
votes
8
answers
135k
views
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(...
86
votes
31
answers
380k
views
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 ...
85
votes
4
answers
82k
views
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<User, Integer> {
@Transactional
@...
82
votes
7
answers
98k
views
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....
74
votes
5
answers
61k
views
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.
69
votes
5
answers
63k
views
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 ...
67
votes
2
answers
90k
views
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'...
59
votes
3
answers
39k
views
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 ...
54
votes
2
answers
33k
views
Benefits of JavaConfig over XML configurations in Spring?
Earlier the configurations used to be in hard coded in the code, later it was externalized to .property files (for sake of avoiding hard coded values, avoiding changing code for the sake of changing ...
53
votes
2
answers
39k
views
What is a Spring "stereotype"?
On a SpringSource blog entry, the following sentence references a stereotype.
Because @Controller is a specialization of Spring's @Component
Stereotype annotation, the class will automatically ...
51
votes
2
answers
57k
views
Difference between @EntityScan and @ComponentScan
I am trying to understand the difference here. I see that a class has been annotated with both of them with same package example :
@Configuration
@EntityScan("some.known.persistence")
@ComponentScan({...
50
votes
1
answer
127k
views
Is it possible to set a bean name using annotations in Spring Framework?
I have a bean like this:
@Bean
public String myBean(){
return "My bean";
}
I want to autowire it:
@Autowired
@Qualifier("myBean")
public void setMyBean(String myBean){
this.myBean=myBean;
}
...
43
votes
5
answers
34k
views
Difference between Spring annotations
Questions:
1) Difference between @Component and @Configuration?
I have read that both remove the necessity of wiring code to be put in XML, but did not get the difference between these.
2) What ...
38
votes
4
answers
30k
views
@Value Annotation Is Not Working In The Constructor
Why is Spring @Value Annotation not working in the constructor?
I have this Email class that is reading some email related configuration.
In the constructor, if I put a break-point the values are ...
38
votes
2
answers
44k
views
Multiple DataSource and JdbcTemplate in Spring Boot (> 1.1.0)
I would like to inject a specific JdbcTemplatein a Spring Boot project. I tried to follow this example for multiple DataSourceconfiguration : http://spring.io/blog/2014/05/27/spring-boot-1-1-0-m2-...
38
votes
4
answers
3k
views
Struts2 + Spring Security 2.06: Valuestack is null when attempting to use @Secured on an Action method
Before I begin, I've to say the closest answer I've found is here but honestly I don't really understand what's going on there.
I'm using Struts2 + Spring Security 2.06 with a custom authentication ...
37
votes
6
answers
86k
views
How to get error text in controller from BindingResult?
I have an controller that returns JSON. It takes a form, which validates itself via spring annotations. I can get FieldError list from BindingResult, but they don't contain the text that a JSP would ...
37
votes
8
answers
142k
views
Spring Boot @Value Properties
I have a Spring Boot application and in one of the classes, I try to reference a property from the application.properties file using @Value. But, the property does not get resolved. I have looked at ...
37
votes
5
answers
102k
views
Spring 4 - addResourceHandlers not resolving the static resources
My maven spring project directory structure is shown below. I am using Spring-4 annotation based configuration. I configure the resources like below. I tried many ways that are suggested in many ...
36
votes
6
answers
71k
views
Can @PathVariable return null if it's not found?
Is it possible to make the @PathVariable to return null if the path variable is not in the url? Otherwise I need to make two handlers. One for /simple and another for /simple/{game}, but both do the ...
36
votes
4
answers
44k
views
Spring @Value empty list as default
Is there a way to set an empty list as default value for a property in Spring, something like:
@Value("${my.list.of.strings :" + new ArrayList<>() + "}")
private List<String> myList;
...
36
votes
3
answers
57k
views
Dynamically changing the @ResponseStatus in annotation driven Spring MVC
I am really not sure if this is feasible using Spring 3.2 MVC.
My Controller has a method declared as below:
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @...
35
votes
6
answers
27k
views
Spring Security @PreAuthorization pass enums in directly
My question is a duplicate of Custom annotation with spring security but it went unanswered and I believe there should be a simple solution to the problem.
Basically instead of doing:
@PreAuthorize("...
33
votes
7
answers
53k
views
@EnableTransactionManagement annotation with 2 transaction managers
I am using @Configuration annotation for configuration of spring instead of xml file. I am configuring 2 datasources with different session factory and different transaction managers. I am stuck with ...
33
votes
4
answers
34k
views
how to specify a bean as non lazy with annotations
Does anyone know how to specify a bean as non lazy when using annotations to configure the bean?
32
votes
5
answers
19k
views
Using Spring cache annotation in multiple modules
I have a util module that produces a jar to be used in other applications. I'd like this module to use caching and would prefer to use Spring's annotation-driven caching.
So Util-Module would have ...
31
votes
1
answer
17k
views
AbstractWizardFormController using Annotated @Controllers
In Spring Framework , AbstractWizardFormController seems deprecated. How to implement multiple pages form in the Spring MVC Framework. (I am not using webflow)
any example or pointer would help ...
31
votes
4
answers
24k
views
PreAuthorize error handling
I'm using Spring Oauth2 and Spring Pre-post Annotations With Spring-boot
I Have a service class MyService. one of MyService methods is:
@PreAuthorize("#id.equals(authentication.principal.id)")
...
30
votes
4
answers
18k
views
How to do Spring Lookup Method Injection with Annotations?
Is there any way to use Lookup Method Injection using annotations?
Given the following class:
@Service
public abstract class A {
protected abstract createB();
}
In order to get it to work I ...
29
votes
1
answer
29k
views
Custom Spring annotation for request parameters
I would like to write custom annotations, that would modify Spring request or path parameters according to annotations. For example instead of this code:
@RequestMapping(method = RequestMethod.GET)
...
27
votes
1
answer
44k
views
@Transient annotation, @org.springframework.data.annotation.Transient annotation, transient keyword and password storing
Currently I'm learning the Spring framework, mainly focusing on it's Security Module. I've watched some guides in connection with registration and login. I saw this common usage of transient keyword ...
27
votes
5
answers
48k
views
Spring @Value annotated method, use default value when properties not available
Situation
I am injecting properties from .properties file into fields annotated with @Value. However this properties present sensitive credentials, so I remove them from repository. I still want that ...
26
votes
5
answers
20k
views
Using static variables in Spring annotations
I'm using spring's PreAuthorize annotation as follows:
@PreAuthorize("hasRole('role')");
However, I already have 'role' defined as a static String on another class. If I try to use this value:
@...
25
votes
6
answers
49k
views
FactoryBeans and the annotation-based configuration in Spring 3.0
Spring provides the FactoryBean interface to allow non-trivial initialisation of beans. The framework provides many implementations of factory beans and -- when using Spring's XML config -- factory ...
24
votes
8
answers
18k
views
Spring @Scheduled annotation random delay
I am using the @Scheduled annotation from Spring framework to invoke a method. But I have multiple nodes in my setup and I do not want them to all run at exactly the same time. So I'd like to set a ...
24
votes
2
answers
20k
views
Spring boot component scan include a single class [duplicate]
I am using spring component scan to auto detect beans as:
@ComponentScan({"com.org.x, com.org.y"})
The issue is I want all classes in com.org.x to be scanned but I want a single class, com.org.y....
24
votes
2
answers
91k
views
Maven: package io.swagger.annotations does not exist
I want to document my project with swagger. I add swagger annotations and io.springfox dependencies to my project but when I run mvn clean package I have a lot of errors:
PS D:\parent-project> mvn ...
23
votes
3
answers
20k
views
Reference an Annotated Spring Component in an XML Bean Definition
I am using an external library that requires that I configure it using an XML Bean definition; in the Bean definition I need to provide an external class with a Bean from my project. I am using spring ...
22
votes
3
answers
36k
views
applicationContext not finding Controllers for Servlet context
I have a Spring web app with an applicationContext.xml and a dispatcher-servlet.xml configuration. I've defined the <context:component-scan /> in applicationContext.xml, but when I run my app ...
22
votes
1
answer
18k
views
@Autowired by constructor looks for beans by type. How to inject a bean by name to a constructor using autowired annotation
@Autowired by constructor looks for beans by type. How to inject a bean by name to a constructor using autowired annotation?
I have 2 beans of same type but I need to inject it to constructor of ...
22
votes
1
answer
11k
views
Can you specify multiple @Conditional annotations?
With Spring Boot, can one use multiple @Conditional annotations on one @Bean definition? If so, is there a predictable order that they are resolved? In other words, can I specify both @...
22
votes
3
answers
12k
views
What is "persistence exception translation" for @Repository beans
I was reading Spring with annotation part and I came across @Repositoryannotation
I read that @Repository beans differ from @Component beans in the sense that they are eligible for persistence ...
22
votes
2
answers
4k
views
Meaning of Spring @ManagerResource: persistPolicy, persistPeriod, persistLocation and currencyTimeLimit
What does the following fields mean in Spring @ManagedResource used for JMX? There is no Javadoc or related description in Spring documentation.
1. String persistPolicy() default "";
2. int ...