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].

Filter by
Sorted by
Tagged with
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 ...
Colin McCree's user avatar
  • 25.8k
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 ...
kseeker's user avatar
  • 11.1k
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.
Paul Verest's user avatar
  • 62.1k
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 ...
Rachel's user avatar
  • 102k
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 ...
chrylis -cautiouslyoptimistic-'s user avatar
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 @...
user1396576's user avatar
  • 7,261
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 ...
Maksim's user avatar
  • 16.7k
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 ...
Thomas Einwaller's user avatar
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 @...
Blankman's user avatar
  • 264k
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?
MatBanik's user avatar
  • 26.7k
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 ...
grantmcconnaughey's user avatar
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....
Oleg Pavliv's user avatar
  • 20.8k
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....
Lucky Murari's user avatar
  • 12.7k
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 ...
MilindaD's user avatar
  • 7,613
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/{...
Jonik's user avatar
  • 81k
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 ...
peakit's user avatar
  • 29k
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 ...
chubbsondubs's user avatar
  • 38.2k
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 ...
mlo55's user avatar
  • 6,779
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 ...
Prashant Shilimkar's user avatar
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 ...
stewsters's user avatar
  • 4,525
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 ...
Srikanth's user avatar
  • 4,409
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 +.
user1857181's user avatar
  • 4,181
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 ...
Elwood's user avatar
  • 4,611
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....
Kanagavelu Sugumar's user avatar
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....
Kyle's user avatar
  • 14.4k
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, ...
user448070's user avatar
  • 4,035
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", ...
pbaranski's user avatar
  • 23.9k
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 ...
Teimuraz's user avatar
  • 9,115
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 ...
Eazy's user avatar
  • 3,809
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.
Rys's user avatar
  • 5,034
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? ******...
Jeff Cook's user avatar
  • 8,646
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 ...
NewQueries's user avatar
  • 4,881
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 ...
hard coder's user avatar
  • 5,587
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 = [...
juventus's user avatar
  • 3,412
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....
Saj's user avatar
  • 18.5k
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, ...
Chinmay's user avatar
  • 4,842
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 ...
GSUgambit's user avatar
  • 4,609
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&...
JackDev's user avatar
  • 11.1k
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-...
Vinod Mohanan's user avatar
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 ...
Vinnie's user avatar
  • 12.6k
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....
emeraldhieu's user avatar
  • 9,451
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 ...
dleerob's user avatar
  • 5,151
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 { // ...
Dónal's user avatar
  • 186k
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 ...
Lithicas's user avatar
  • 3,933
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 ...
METTAIBI's user avatar
  • 3,381
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 "...
wuntee's user avatar
  • 12.3k
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 ...
Scott Bale's user avatar
  • 10.7k
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 ...
teabot's user avatar
  • 15.4k
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 ...
d-man's user avatar
  • 57.8k
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 ...
Carlos Alberto's user avatar

1
2 3 4 5
4243