Why is Spring not wiring my @Autowired members in a dependent jar?

10,807

Solution 1

I was instantiating my objects incorrectly. For framework objects and such like MVC controllers, you don't need to do anything to get your @Autowired members wired.

For objects I was creating on the fly, I wasn't going through the IOC container, that's why their dependencies weren't being fulfilled.

Solution 2

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

If you are going to autowire class using @Autowire annotation, Autowiring class should be annotated with one of stereotype annotation (@Component,@Controller,@Service,@Repository). Spring resolves first annotation configuration and then xml configuration. This is written in spring doc as

Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches.

Check proof on spring doc.

So what you need to do is add annotations for classes from server project as well as server.model. Same in case of your third project server.persistence. Add annotations according to layers or functionality.

Share:
10,807
null
Author by

null

My name is Mark Stoddard, and I'm President and Software Laborer at Wonder Ward Inc. in Milwaukee's Third Ward. I typically use Microsoft products including ASP.NET, SQL Server, Azure, and Visual Studio. Visit the links below to connect with me, but if you want to connect on LinkedIn, do make it personal ('hey, I found you on stackoverflow!'). Feel free to reach out with any size project. I do large projects to keep the lights on, and small projects for fun and dreams. Mark Stoddard on LinkedIn Mark Stoddard on Twitter (@mstodd66) Wonder Ward Inc on Twitter (@wward_dev) Wonder Ward Inc

Updated on June 19, 2022

Comments

  • null
    null almost 2 years

    I'm building a Google App Engine app using Spring 3.1 and am having a problem getting members in one of my jars wired.

    I have three projects:

    1. server
    2. server.model
    3. server.persistence

    I have an ant build script so that when my workspace builds, 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 autowire things from both server.model and server.persistence, but in server.model my server.persistence beans aren't getting wired even though they're the exact same as in server.

    snippet from my servlet application config:

    <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 one gets fulfilled. Here's the one failing:

    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 did a test in the server.model project trying to @Autowired something that I don't have defined as a bean in my application config, and didn't get any errors. I should have got a 'no such bean found' error like I do if I do the same thing for the server project.

    Any ideas why?