开发者

Using @autowired to store a list of beans dynamically created in another class

I have a class called Drives which dynamically instantiates FsAccess beans.

I have a service class called ServersAccessService which finds FsAccess beans and stores them in a map using @autowired. Is there a way to have the service class initiate the @autowired after Drives is finished instantiating the FsAccess beans?

Service Class:

public class ServersAccessService implements DisposableBean {

protected static final Log log = LogFactory.getLog(ServersAccessService.class);

protected s开发者_运维技巧tatic Map<String, FsAccess> servers = new HashMap<String, FsAccess>();
I
protected Map<String, FsAccess> restrictedServers = new HashMap<String, FsAccess>();

protected boolean isInitialized = false;

protected static Map<String, DrivesCategory> drivesCategories = new HashMap<String, DrivesCategory>();
@Autowired
public void setServers(List<FsAccess> servers) {
    for(FsAccess server: servers) {
        this.servers.put(server.getDriveName(), server);
    }
}

Drives class:

MyBeanFactory mbf = new MyBeanFactory();

        //loop through each drive in driveList
        for(String name:driveList)
        {

            String fullUri = "smb://naz-fs3/home/"+name;
            String icon = "/esup-portlet-stockage/img/drives/root.png";


            VfsAccessImpl drive = mbf.createInstance();

            //Set attribute information 
            drive.setDriveName(name);
            drive.setIcon(icon);
            drive.setUri(fullUri);
            drive.setContextToken(name);




        }


If the Drives bean is instantiating the FsAccess beans in it's initialization phase, you may declare the dependency of the ServersAccessService bean via depends-on to the Drives bean. This forces the initialization of the Drives bean before the initialization of the ServersAccessService bean.


I believe what you want is to make your drives class a @Configuration bean, and make the method that returns a List annotated with @Bean. That way the spring container knows about the FsAccess list and it can be available for injection via @Autowired.

EDIT:

Reference: http://blog.springsource.com/2006/11/28/a-java-configuration-option-for-spring/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜