Spring @Transaction method call by the method within the same class, does not work?

It’s a limitation of Spring AOP (dynamic objects and cglib). If you configure Spring to use AspectJ to handle the transactions, your code will work. The simple and probably best alternative is to refactor your code. For example one class that handles users and one that process each user. Then default transaction handling with Spring … Read more

Spring Boot Configure and Use Two DataSources

Here you go. Add in your application.properties file: #first db spring.datasource.url = [url] spring.datasource.username = [username] spring.datasource.password = [password] spring.datasource.driverClassName = oracle.jdbc.OracleDriver #second db … spring.secondDatasource.url = [url] spring.secondDatasource.username = [username] spring.secondDatasource.password = [password] spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver Add in any class annotated with @Configuration the following methods: @Bean @Primary @ConfigurationProperties(prefix=”spring.datasource”) public DataSource primaryDataSource() { return … Read more

Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

Spring lets you define multiple contexts in a parent-child hierarchy. The applicationContext.xml defines the beans for the “root webapp context”, i.e. the context associated with the webapp. The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet’s app context. There can be many of these in a webapp, one per Spring … Read more

Downloading a file from spring controllers

@RequestMapping(value = “/files/{file_name}”, method = RequestMethod.GET) public void getFile( @PathVariable(“file_name”) String fileName, HttpServletResponse response) { try { // get your file as InputStream InputStream is = …; // copy it to response’s OutputStream org.apache.commons.io.IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (IOException ex) { log.info(“Error writing file to output stream. Filename was ‘{}'”, fileName, ex); throw new RuntimeException(“IOError … Read more

What is a NoSuchBeanDefinitionException and how do I fix it?

The javadoc of NoSuchBeanDefinitionException explains Exception thrown when a BeanFactory is asked for a bean instance for which it cannot find a definition. This may point to a non-existing bean, a non-unique bean, or a manually registered singleton instance without an associated bean definition. A BeanFactory is basically the abstraction representing Spring’s Inversion of Control … Read more

Spring JSF integration: how to inject a Spring component/service in JSF managed bean?

@ManagedBean vs @Controller First of all, you should choose one framework to manage your beans. You should choose either JSF or Spring (or CDI) to manage your beans. Whilst the following works, it is fundamentally wrong: @ManagedBean // JSF-managed. @Controller // Spring-managed. public class BadBean {} You end up with two completely separate instances of … Read more

Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”?

Your standard Spring MVC application will serve all requests through a DispatcherServlet that you’ve registered with your Servlet container. The DispatcherServlet looks at its ApplicationContext and, if available, the ApplicationContext registered with a ContextLoaderListener for special beans it needs to setup its request serving logic. These beans are described in the documentation. Arguably the most … Read more

Why is my Spring @Autowired field null?

The field annotated @Autowired is null because Spring doesn’t know about the copy of MileageFeeCalculator that you created with new and didn’t know to autowire it. The Spring Inversion of Control (IoC) container has three main logical components: a registry (called the ApplicationContext) of components (beans) that are available to be used by the application, … Read more

What’s the difference between @Component, @Repository & @Service annotations in Spring?

From Spring Documentation: The @Repository annotation is a marker for any class that fulfils the role or stereotype of a repository (also known as Data Access Object or DAO). Among the uses of this marker is the automatic translation of exceptions, as described in Exception Translation. Spring provides further stereotype annotations: @Component, @Service, and @Controller. … Read more

SPRING MVC getting 404 error in when you hits the submit button

index.jsp <html> <body> <form action=”add” method=”post”> <input type=”text” name=”t1″> <input type=”text” name=”t2″> <input type=”submit”> </form> </body> </html> controller.java package com.test; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class addController { @RequestMapping(“/add”) @ResponseBody public String add() { return “display.jsp”; } } web.xml <?xml version=”1.0″ encoding=”UTF-8″?> <!– webapp/WEB-INF/web.xml –> <web-app xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd” version=”3.0″> <display-name>Archetype … Read more