Questions tagged [spring]
The Spring Framework is an open-source framework for application development on the Java platform. At its core is rich support for component-based architectures, and it currently has over twenty highly integrated modules. For Spring Boot related questions, also include the tag [spring-boot].
212,121
questions
2578
votes
30
answers
1.2m
views
What's the difference between @Component, @Repository & @Service annotations in Spring?
Can @Component, @Repository, and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?
In other words, if I have a ...
1103
votes
9
answers
487k
views
What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
What is the difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
When I see the examples on the web, I see them there used kind of interchangeably.
What is the ...
1069
votes
65
answers
1.1m
views
How to configure port for a Spring Boot application
How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.
804
votes
12
answers
391k
views
What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?
I am going through some blogs on SpringSource and in one of the blogs, the author is using @Inject and I suppose he can also use @Autowired.
Here is the piece of code:
@Inject private ...
780
votes
21
answers
748k
views
Why is my Spring @Autowired field null?
Note: This is intended to be a canonical answer for a common problem.
I have a Spring @Service class (MileageFeeCalculator) that has an @Autowired field (rateService), but the field is null when I try ...
725
votes
16
answers
478k
views
Spring: @Component versus @Bean
I understand that @Component annotation was introduced in Spring 2.5 to get rid of XML bean definition by using classpath scanning.
@Bean was introduced in spring 3.0 and can be used with @...
654
votes
17
answers
290k
views
What exactly is Spring Framework for? [closed]
I hear a lot about Spring, people are saying all over the web that Spring is a good framework for web development. What exactly is Spring Framework for in a nutshell? Why should I used it over just ...
597
votes
19
answers
281k
views
Where does the @Transactional annotation belong?
Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes that are using the DAO objects? Or does it make sense to annotate both ...
588
votes
11
answers
425k
views
How does Spring achieve IOC with autowiring?
I'm a little confused as to how this inversion of control (IoC) works in Spring.
Say I have a service class called UserServiceImpl that implements the UserService interface.
How would this be @...
553
votes
10
answers
405k
views
Spring @Transactional - isolation, propagation
Can someone explain the isolation & propagation parameters in the @Transactional annotation via a real-world example?
Basically when and why I should choose to change their default values?
534
votes
17
answers
250k
views
What in the world are Spring beans?
I am yet to find a high-level definition of Spring beans that I can understand. I see them referenced often in Grails documentation and books, but I think that understanding what they are would be ...
532
votes
25
answers
675k
views
How can I log SQL statements in Spring Boot?
I want to log SQL statements to a file.
I have the following properties in application.properties:
spring.datasource.url=...
spring.datasource.username=user
spring.datasource.password=1234
spring....
479
votes
14
answers
493k
views
JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object
I am getting the following error when trying to get a JSON request and process it:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com....
457
votes
16
answers
604k
views
Downloading a file from spring controllers
I have a requirement where I need to download a PDF from the website. The PDF needs to be generated within the code, which I thought would be a combination of a freemarker and a PDF generation ...
457
votes
13
answers
539k
views
How to respond with an HTTP 400 error in a Spring MVC @ResponseBody method returning String
I'm using Spring MVC for a simple JSON API, with a @ResponseBody based approach like the following. (I already have a service layer producing JSON directly.)
@RequestMapping(value = "/matches/{...
448
votes
6
answers
285k
views
Spring - @Transactional - What happens in background?
I want to know what actually happens when you annotate a method with @Transactional?
Of course, I know that Spring will wrap that method in a Transaction.
But, I have the following doubts:
I heard ...
446
votes
10
answers
478k
views
Only using @JsonIgnore during serialization, but not deserialization
I have a user object that is sent to and from the server. When I send out the user object, I don't want to send the hashed password to the client. So, I added @JsonIgnore on the password property, but ...
443
votes
11
answers
295k
views
@Resource vs @Autowired
Which annotation, @Resource (jsr250) or @Autowired (Spring-specific) should I use in DI?
I have successfully used both in the past, @Resource(name="blah") and @Autowired @Qualifier("blah")
My ...
437
votes
9
answers
627k
views
How to use OrderBy with findAll in Spring Data
I am using spring data and my DAO looks like
public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
public findAllOrderByIdAsc(); // I want to use some thing like ...
435
votes
19
answers
495k
views
Running code after Spring Boot starts
I want to run code after my spring-boot app starts to monitor a directory for changes.
I have tried running a new thread but the @Autowired services have not been set at that point.
I have been ...
431
votes
15
answers
256k
views
Difference between spring @Controller and @RestController annotation
Difference between spring @Controller and @RestController annotation.
Can @Controller annotation be used for both Web MVC and REST applications?
If yes, how can we differentiate if it is Web MVC or ...
417
votes
9
answers
551k
views
@RequestParam vs @PathVariable
What is the difference between @RequestParam and @PathVariable while handling special characters?
+ was accepted by @RequestParam as space.
In the case of @PathVariable, + was accepted as +.
397
votes
18
answers
786k
views
Spring RestTemplate GET with parameters
I have to make a REST call that includes custom headers and query parameters. I set my HttpEntity with just the headers (no body), and I use the RestTemplate.exchange() method as follows:
HttpHeaders ...
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....
390
votes
6
answers
181k
views
Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
Are applicationContext.xml and spring-servlet.xml related anyhow in Spring Framework?
Will the properties files declared in applicationContext.xml be available to DispatcherServlet?
On a related note, ...
389
votes
15
answers
542k
views
How to check String in response body with mockMvc
I have simple integration test
@Test
public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName() throws Exception {
mockMvc.perform(post("/api/users").header("Authorization", ...
386
votes
31
answers
691k
views
Spring Boot - How to log all requests and responses with exceptions in single place?
I'm working on REST API with spring boot. I need to log all requests with input params (with methods, eg. GET, POST, etc.), request path, query string, corresponding class method of this request, also ...
378
votes
10
answers
794k
views
Using env variable in Spring Boot's application.properties
We are working on a Spring Boot web application, and the database we are using is MySQL;
the setup we have is we first test it locally (means we need to install MySQL on our PC);
then we push to ...
376
votes
9
answers
177k
views
What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?
What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?
In logging.config case, the application works differently.
358
votes
52
answers
1.2m
views
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
I am working on a Spring Boot Batch example with MongoDB and I have already started the mongod server.
When I launch my application, I am getting the error below.
Any pointers for this issue?
******...
352
votes
3
answers
468k
views
Understanding Spring @Autowired usage
I am reading the spring 3.0.x reference documentation to understand Spring Autowired annotation:
3.9.2 @Autowired and @Inject
I am not able to understand the below examples. Do we need to do ...
345
votes
6
answers
181k
views
What's the difference between Hibernate and Spring Data JPA
What are the main differences between Hibernate and Spring Data JPA?
When should we not use Hibernate or Spring Data JPA?
Also, when may Spring JDBC template perform better than Hibernate and Spring ...
340
votes
10
answers
555k
views
Spring Boot configure and use two data sources
How can I configure and use two data sources?
For example, here is what I have for the first data source:
application.properties
#first db
spring.datasource.url = [url]
spring.datasource.username = [...
332
votes
10
answers
522k
views
Type safety: Unchecked cast
In my spring application context file, I have something like:
<util:map id="someMap" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java....
332
votes
4
answers
175k
views
Difference between Role and GrantedAuthority in Spring Security
There are concepts and implementations in Spring Security, such as the GrantedAuthority interface to get an authority to authorize/control an access.
I would like that to permissible operations, ...
330
votes
10
answers
209k
views
Spring @Autowire on Properties vs Constructor
So since I've been using Spring, if I were to write a service that had dependencies I would do the following:
@Component
public class SomeService {
@Autowired private SomeOtherService ...
325
votes
17
answers
485k
views
Reading a List from properties file and load with Spring annotation @Value
I want to have a list of values in a .properties file, i.e.:
my.list.of.strings=ABC,CDE,EFG
And to load it in my class directly, i.e.:
@Value("${my.list.of.strings}")
private List<String&...
323
votes
17
answers
608k
views
Setting active profile and config location from command line in Spring Boot
I have a Spring Boot application.
I have three profiles in my application-> development, staging and production. So I have 3 files
application-development.yml
application-staging.yml
application-...
314
votes
14
answers
405k
views
Why is Spring's ApplicationContext.getBean considered bad?
I asked a general Spring question: Auto-cast Spring Beans and had multiple people respond that calling Spring's ApplicationContext.getBean() should be avoided as much as possible. Why is that?
How ...
311
votes
35
answers
691k
views
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
I included these in the Build Path:
all Spring libs
Apache Tomcat 7.0 library
The project still fails during startup:
SEVERE: Error configuring application listener of class org.springframework.web....
310
votes
19
answers
509k
views
How do I activate a Spring Boot profile when running from IntelliJ?
I have 5 environments:
- local (my development machine)
- dev
- qc
- uat
- live
- staging
I want different application properties to be used for each environment, so I have the following ...
310
votes
19
answers
791k
views
How can I inject a property value into a Spring Bean which was configured using annotations?
I have a bunch of Spring beans which are picked up from the classpath via annotations, e.g.
@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
// ...
308
votes
22
answers
497k
views
Spring Boot - Loading Initial Data
I'm wondering what the best way to load initial database data before the application starts? What I'm looking for is something that will fill my H2 database with data.
For example, I have a domain ...
308
votes
5
answers
514k
views
How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?
I was working on my Spring boot app project and noticed that, sometimes there is a connection time out error to my Database on another server(SQL Server).
This happens specially when I try to do some ...
307
votes
7
answers
227k
views
How to use multiple @RequestMapping annotations in spring?
Is it possible to use multiple @RequestMapping annotations over a method?
Like :
@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
return "...
306
votes
17
answers
185k
views
When using Spring Security, what is the proper way to obtain current username (i.e. SecurityContext) information in a bean?
I have a Spring MVC web app which uses Spring Security. I want to know the username of the currently logged in user. I'm using the code snippet given below . Is this the accepted way?
I don't ...
304
votes
25
answers
310k
views
Injecting Mockito mocks into a Spring bean
I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on ...
303
votes
6
answers
579k
views
Spring cron expression for every day 1:01:am
I'm trying to have my code execute on a fixed schedule, based on a Spring cron expression. I would like the code to be executed every day at 1:01:am. I tried the following expression, but this didn't ...
298
votes
4
answers
285k
views
What is this spring.jpa.open-in-view=true property in Spring Boot?
I saw spring.jpa.open-in-view=true property in Spring Boot documentation for JPA configuration.
Is the true default value for this property if it's not provided at all?;
What does this really do? I ...