Why doesn't Spring connect my @Autowired elements in the dependent jar?

I am building a Google App Engine application using Spring 3.1 and I am having trouble getting members in one of my mailboxes.

I have three projects:

  • server
  • server.model
  • server.persistence

I have an ant build script, so when creating my workspace it creates jars for server.model and server.persistence and puts them in the correct lib directory for the server project.

In server I can auto-assure things from both server.model and server.persistence , but my server.persistence beans are not connected to server.model , although they are exactly the same as in server .

fragment from my servlet application configuration:

 <context:component-scan base-package="com.impersonal.server"/> <bean autowire="byType" id="appEngineDataStore" class="com.impersonal.server.persistance.AppEngineDataStore"/> <bean autowire="byType" id="userList" class="com.impersonal.server.model.UserList"/> 

I have the following code in both the server project and the server.model project, and only the server is running. Here one does not work:

 package com.impersonal.server.model; import java.util.ArrayList; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import com.impersonal.server.persistance.AppEngineDataStore; import com.impersonal.server.persistance.IDataStore; public class UserList extends ArrayList<User> { private UserList(){} //this is always null, but the same line in a class in the other project works private @Autowired AppEngineDataStore _dataStore; public UserList(UUID userId, String tempId) { String poo = "poo"; poo.concat("foo "); int i = 3; } } 

Edit: Just ran a test in the server.model project, trying @Autowired that I did not define as a bean in my application configuration and did not receive any errors. I should have gotten a "bean found error" like me if I did the same for the server project.

Any ideas why?

+6
source share
5 answers

I did not create objects correctly. For infrastructure objects such as MVC controllers, you don’t need to do anything to get your @Autowired members to connect.

For the objects that I created on the fly, I did not go through the IOC container, so their dependencies were not fulfilled.

+3
source
Tag

<context:component-scan/> searches for annotated classes.

If you intend to use the autowire class using the @Autowire annotation, the @Autowire class must be annotated with one of the stereotype annotations (@Component, @Controller, @Service, @Repository). Spring enables the first annotation configuration and then the xml configuration. It is written in Spring doc as

Annotation injection is performed before XML is introduced, so the last configuration will override the first for properties related between the two approaches.

Check confirmation on spring doc .

So, you need to add annotations for classes from the server project, as well as server.model . The same goes for your third server.persistence project. Add annotations according to layers or functionality.

+1
source

Try:

 @Autowired(required = true) private AppEngineDataStore _dataStore; 

Instead:

 private @Autowired AppEngineDataStore _dataStore; 

EDIT 1:

When using autwire above, in spring xml, try:

 <bean id="appEngineDataStore" class="com.impersonal.server.persistance.AppEngineDataStore" scope="prototype"></bean> 

Instead:

 <bean autowire="byType" id="appEngineDataStore" class="com.impersonal.server.persistance.AppEngineDataStore"/> 
0
source

autowire in xml means a slightly different thing. Instead of defining a bean in xml, you can annotate it as @Service , it will be detected by component checking, and @Autowired will work.

0
source

In your xml configuration, use the autowire-candidate property

 <bean autowire="byType" id="appEngineDataStore" class="com.impersonal.server.persistance.AppEngineDataStore" autowire-candidate="true" /> 
0
source

Source: https://habr.com/ru/post/918642/


All Articles