kaushik
kaushik

Reputation: 2492

How to use PIcocontainer

I am using Picocontainer in a study project. I am having doubts about how to use it.

The following is the class I have :

public class DependencySupplier {

    public static MutablePicoContainer pico;

    static {
        pico = new DefaultPicoContainer();
        pico.registerComponentImplementation(CollectionDao.class, CollectionDaoImpl.class);
        pico.registerComponentImplementation(ReadingDao.class, ReadingDaoImpl.class);
        pico.registerComponentImplementation(CollectionDetails.class, CollectionDetailsImpl.class);
        pico.registerComponentImplementation(Reading.class, ReadingImpl.class);
    }

    public static CollectionDao getCollectionDao() {
        return (CollectionDao) pico.getComponentInstance(CollectionDao.class);
    }

    public static ReadingDao getReadingDao() {
        return (ReadingDao) pico.getComponentInstance(ReadingDao.class);
        }   
    }

My doubts are:

  1. Is this the right way to use pico ?
  2. The AddressImpl class is as follows:

    public class AddressImpl implements Address {
    
        private String address1;
        private String address2;
        private String address3;
        private String address4;
    
        public AddressImpl(String address1, String address2, String address3,
            String address4) {
            super();
            this.address1 = address1;
            this.address2 = address2;
        }
    
        public String getAddress1() {
            return address1;
        }
        public void setAddress1(String address1) {
            this.address1 = address1;
        }
        public String getAddress2() {
            return address2;
        }
        public void setAddress2(String address2) {
            this.address2 = address2;
        }
        public String getAddress3() {
            return address3;
        }
    }
    

How can I instantiate the Address object with the above implementation as 'address1' and 'address2' has to be supplied by user and will be available on run time ?

Upvotes: 2

Views: 1194

Answers (1)

xeye
xeye

Reputation: 1256

Well, actually it's not a right way to use pico...

In most cases you should never directly lookup components from the pico context.

You need to register in pico your DAOs, services, other logic classes... They need to obtain referenced DAOs etc just declaring them as constructor arguments. Then in your bootstrap class that registers components you need to obtain from container your main logic class and call its method(s) (or use Startable lifecycle) As for address instances, I'm not sure you need to instantiate them from the pico at all (cause I don't see ANY dependencies that container may fulfill for the Address, so what point?)

But still if you need to, you may register ready instances like pico.registerComponentInstance(new AddressImpl(...)) then you can inject all available instances with constructor argument Address[] addrs. There's another way, to instantiate several instances directly from the pico, but I think you just don't need it

Upvotes: 1

Related Questions