Skip to main content

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
2591 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
1109 votes
9 answers
497k 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
1077 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.5k
808 votes
12 answers
397k 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
781 votes
22 answers
758k 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
728 votes
16 answers
487k 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,291
655 votes
17 answers
291k 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.8k
598 votes
19 answers
282k 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
589 votes
11 answers
426k 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
  • 265k
555 votes
10 answers
410k 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
540 votes
17 answers
258k 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
539 votes
25 answers
693k 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
490 votes
18 answers
929k views

How can I set the logging level with application.properties?

This is very simple question, but I cannot find information. (Maybe my knowledge about Java frameworks is severely lacking.) How can I set the logging level with application.properties? And logging ...
zeodtr's user avatar
  • 11.1k
478 votes
14 answers
495k 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
458 votes
16 answers
609k 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,643
458 votes
13 answers
545k 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
  • 81.2k
454 votes
6 answers
291k 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
  • 29.1k
448 votes
10 answers
484k 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
297k 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,799
442 votes
9 answers
642k 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
441 votes
20 answers
510k 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,585
433 votes
15 answers
258k 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,429
419 votes
9 answers
555k 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,201
397 votes
18 answers
801k 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
396 votes
13 answers
488k 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
395 votes
18 answers
177k 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
391 votes
15 answers
554k 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
  • 24.3k
390 votes
31 answers
709k 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,175
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
383 votes
10 answers
811k 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,859
380 votes
9 answers
180k 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,074
363 votes
53 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
469k 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,901
348 votes
6 answers
186k 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,625
342 votes
10 answers
569k 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,432
338 votes
10 answers
218k views

Spring @Autowired 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,729
333 votes
10 answers
531k 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.6k
331 votes
6 answers
179k 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
329 votes
17 answers
630k 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's user avatar
  • 3,977
328 votes
17 answers
495k 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
319 votes
19 answers
534k 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,241
315 votes
14 answers
407k 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
19 answers
795k 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
310 votes
35 answers
695k 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,462
310 votes
5 answers
535k 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,401
309 votes
22 answers
508k 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,953
309 votes
7 answers
231k 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.4k
308 votes
4 answers
298k 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
306 votes
18 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.8k
305 votes
6 answers
588k 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.9k

1
2 3 4 5
4256