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
32
votes
5 answers

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 something like…
Snekse
  • 15,474
  • 10
  • 62
  • 77
32
votes
4 answers

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 myList; Obviously not new ArrayList, but I need an empty list there…
Andrei Roba
  • 2,156
  • 2
  • 16
  • 33
31
votes
1 answer

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 considering my limited knowledge in Spring.
Sourabh
  • 4,545
  • 11
  • 39
  • 45
30
votes
4 answers

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 have to declare in spring applicationContext.xml…
Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84
30
votes
3 answers

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)") public SomeResponse getExampleResponse(String…
28
votes
1 answer

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) public String test(@RequestParam("title") String…
arminas
  • 381
  • 1
  • 5
  • 13
27
votes
1 answer

@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 or @Transient annotation on the password field in…
F3R1
  • 493
  • 1
  • 5
  • 15
25
votes
5 answers

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: @PreAuthorize("hasRole(OtherClass.ROLE)"); I get an…
RobEarl
  • 7,862
  • 6
  • 35
  • 50
24
votes
6 answers

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 beans are easy to use. However, in Spring 3.0, I…
Andrew Newdigate
  • 6,005
  • 3
  • 37
  • 31
24
votes
5 answers

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 in case someone wants to run project and doesnt…
RenatoIvancic
  • 1,798
  • 3
  • 21
  • 36
23
votes
2 answers

Spring boot component scan include a single class

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.SomeService.class, alone to be scanned from…
Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150
23
votes
8 answers

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 random value to the initial delay to offset them…
troymass
  • 1,022
  • 3
  • 11
  • 24
23
votes
3 answers

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 annotations with component scanning. How do I…
Rylander
  • 19,449
  • 25
  • 93
  • 144
22
votes
2 answers

spring 3.1 @Cacheable example

I am new to spring annotation and i want to create a sample example which shows the use of @Cacheable annotation in spring 3.1 does any one have guidance to create this ?
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
22
votes
1 answer

@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 another same class based on the bean name. How do I…
user2972319
  • 223
  • 1
  • 2
  • 6
1 2
3
86 87