我在一个研究项目中使用 Picocontainer。我对如何使用它有疑问。
以下是我的课程:
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);
}
}
我的疑问是:
- 这是使用 pico 的正确方法吗?
AddressImpl 类如下:
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; } }
如何使用上述实现将地址对象实例化为“地址1”和“地址2”必须由用户提供并且在运行时可用?