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
17
votes
3 answers

How to debug Spring Security authorization annotations?

I have spring security application in which want to enable annotations security (pre and post authorization). I also have small sample application in which i have implemented it already. Everything works. But moving configs to main applications…
17
votes
3 answers

Is the @Query annotation in spring SQL Injection safe?

Do the parameters of a string passed to the @Query annotation, for Spring, get treated as pure data as they would if, for example, you were using the PreparedStatement class or any method meant to prevent SQL injection? final String MY_QUERY =…
Usman Mutawakil
  • 4,993
  • 9
  • 43
  • 80
17
votes
2 answers

Is there any way to override a bean discovered by component scan?

I have a java configuration class providing fooBean directly and barBean by component scan. @Configuration @ComponentScan(basePackages = { "com.blah" }) public class Config { @Bean public FooBean fooBean { return new FooBean(); …
mmoossen
  • 1,237
  • 3
  • 21
  • 32
16
votes
1 answer

Inject parameters to constructor through annotation in Spring

I am using Spring Boot annotation configuration. I have a class whose constructor accepts 2 parameters (string, another class). Fruit.java public class Fruit { public Fruit(String FruitType, Apple apple) { this.FruitType =…
vishnumanohar
  • 671
  • 2
  • 8
  • 14
16
votes
1 answer

Spring has @Component annotation, what is the real purpose of the annotations @Repository, @Service, @Controller?

I've developed web applications for a few years with Spring framework. Recently, a fresher of my team asked me a question, Spring has @Component annotation, what is the real purpose of the annotations @Repository, @Service, @Controller? I tried to…
Brady Zhu
  • 1,305
  • 5
  • 21
  • 43
16
votes
5 answers

how to read properties file in spring project?

Before post this Question, I google to get Properties from Spring project(Its NOT web-based project). I am confused as every one are talking about application-context.xml and have configuration like However, I am working on normal Java Project…
Sriks
  • 658
  • 4
  • 9
  • 18
15
votes
1 answer

Spring MVC controller inheritance and routing

In my Spring MVC webapp I have a generic RESTful controller for CRUD operations. And each concrete controller had to declare only a @RequestMapping, for example /foo. Generic controller handled all request to /foo and /foo/{id}. But now I need to…
Nofate
  • 2,714
  • 3
  • 32
  • 32
15
votes
4 answers

Inject application properties without Spring

I would like a simple, preferably annotation-based way to inject external properties into a java program, without using the spring framework (org.springframework.beans.factory.annotation.Value;) SomeClass.java @Value("${some.property.name}") private…
ealfonso
  • 6,622
  • 5
  • 39
  • 67
15
votes
3 answers

How to check request parameter is not empty String in @RequestMapping params?

In my controller I have String parameter, containing some id, that should not be null of empty string. I'm wondering, is there any way to check it is not empty String in @RequestMapping params? I have tried to solve it in some ways…
WeGa
  • 801
  • 4
  • 10
  • 24
15
votes
2 answers

How to define a Spring bean using annotation instead of XML?

I have defined in a xml config file: this works fine. The bootsrap class : public class Bootstrap { @PostConstruct public void onServerStart() { …
mjs
  • 21,431
  • 31
  • 118
  • 200
14
votes
2 answers

@Secured does not work in controller, but intercept-url seems to be working fine

It doesn't look like @Secured on methods in my @Controller are being read. When security filtering based on sec:intercept-url is being used, this seems to be working just fine. The following code results in Spring Security giving me this log entry:…
kareblak
  • 412
  • 1
  • 3
  • 12
14
votes
3 answers

Prevent Spring Batch automatic job trigger after context creation without Spring Boot

I am setting up a project with Spring Batch without using Spring Boot. When the Spring application context is created, all the jobs get executed. I tried adding spring.batch.job.enbled=false to the application.properties to prevent this but it still…
14
votes
2 answers

Spring framework @Configurable vs @Configuration

I seems have problem understanding these 2 annotation. I have try to read the javadocs but still cannot figure out. Can anyone help to explain with simple code about these 2 ? Thank so much in advance.
Hieu Lam
  • 423
  • 1
  • 7
  • 17
14
votes
2 answers

Spring : @Transactional @Scheduled method throws TransactionException

(Still a bit new to Spring) I need to have a service method that is at the same time @Scheduled and @Transactional, so that I get to call a DAO in it. Declarative transactions are enabled, the transaction manager is a…
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
13
votes
4 answers

How to validate date in the format "MM/dd/yyyy" in Spring Boot?

I need to validate the given date has valid month, date and year. Basically, date format would be MM/dd/yyyy. But date coming like 13/40/2018 then I need to throw an error message like: invalid start date Is there any annotation available in…
Saravanan
  • 11,372
  • 43
  • 143
  • 213