Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Autowired not working - BeanCreationException

When I try to deploy file on server error occurs. I'm confused, because this code was working.

Exception

    Failed to enable lec2ear-1.0.ear.

Unexpected HTTP response: 500

Request
{
    "address" => [("deployment" => "lecture_7")],
    "operation" => "deploy"
}

Response

Internal Server Error
{
    "outcome" => "failed",
    "failure-description" => {"JBAS014671: Failed services" => {"jboss.undertow.deployment.default-server.default-host./mart-parent" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./mart-parent: Failed to start service
    Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storageController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ru.menkin.ea.lec4.model.services.ICategoryService ru.menkin.ea.lec5.controllers.StorageController._categoryService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ru.menkin.ea.lec4.model.repositories.CategoryRepository ru.menkin.ea.lec4.model.services.CategoryService.categoryRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryRepository': Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:jboss/recipeDs
Error Code: 0

ICategoryService

public interface ICategoryService
{
    public Category create(Category category);
    public Category delete(int id) throws Exception;
    public List<Category> findAll();
    public Category update(Category category) throws Exception;
    public Category findById(int id);
}

his implementation

public class CategoryService implements ICategoryService
{
    @Autowired
    private CategoryRepository categoryRepository;
...

Controller

@Controller
@RequestMapping(value = "/rest")
public class StorageController extends BaseController {
    @Autowired
    @Qualifier("categoryService")
    private ICategoryService _categoryService;
...

database.xml

<djpa:repositories base-package="ru.menkin.ea.lec4.model" />

<bean id="categoryService" class="ru.menkin.ea.lec4.model.services.CategoryService" />

beans.xml

<context:annotation-config />
<context:component-scan base-package="ru.menkin.ea" />

Where my mistake?

like image 617
Stone Jack Avatar asked Jan 12 '16 08:01

Stone Jack


People also ask

Why is @autowired not working?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

What is Beancreationexception?

It's a very common exception thrown when the BeanFactory creates beans of the bean definitions, and encounteres a problem. This article will explore the most common causes of this exception, along with the solutions.

Can we use @autowired in pojo?

The beans can be wired via constructor or properties or setter method. For example, there are two POJO classes Customer and Person. The Customer class has a dependency on the Person. @Autowired annotation is optional for constructor based injection.

How do I enable Autowire in Spring boot?

If you want properly use @Autowired in your spring-boot application, you must do next steps: Add @SpringBootApplication to your main class. Add @Service or @Component annotation to class you want inject. Use one of two ways that you describe in question, to autowire.


1 Answers

Here last cause in your exception message:

Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException

Spring could not invoke the init method of your jpaMappingContext because a persistence exception occured - Something went wrong in the database.

So, Spring autowiring is actually working, but it cannot autowire your dependency because of a database problem that occurs when initializing one of the spring beans.

So something in the database or database settings changed since this code last worked. Analyze the full stack trace to find out the root cause of this issue - Which is a database problem.

like image 167
David Tanzer Avatar answered Oct 25 '22 20:10

David Tanzer