Posso pensare a due possibilità.
Uno è (è una specie di modifica), se non si hanno molti bean che devono essere collegati come quelli nell'esempio, è possibile inserire johnWork nel bean johnHome e in johnHome.setPhone si potrebbe aggiornare la proprietà telefono johnWork, qualcosa di simile:
public class Contact {
private Contact myWorkContact;
private String phone;
public void setPhone(String phone) {
this.phone = phone;
if (this.myWorkContact != null) {
this.myWorkContact.setPhone(phone);
}
}
public void setWorkContact(Contact c) {
this.myWorkContact = c;
}
}
Oppure si potrebbe avere HomeContact e WorkContact sia estendere una classe di contatto e fare lo stesso con quella di iniezione.
Se hai tonnellate e tonnellate di fagioli che ne avranno bisogno (come se la tua applicazione in realtà STA occupandosi di informazioni di contatto), con AOP (avrai bisogno di AspectJ per l'esempio dato) Penso che potresti fare qualcosa come questo (sarà un po 'impegnativo in termini di memoria se ottieni un sacco di oggetti, ma puoi vedere come funzionerebbe qualcosa):
Attenzione: questo in realtà è diventato complicato velocemente, ma sono abbastanza sicuro che funzionerebbe dopo aver lavorato fuori alcuni intoppi
public class Contact {
...
private String phone;
private String name;
private Integer id;
public Contact(Integer id, String name, String phone) {
this.phone = phone;
this.name = name;
this.id = id;
}
public void setPhone(String phone) {
this.phone = phone.
}
//Other getters, setters, etc
...
}
@Aspect
public class ContactPhoneSynchronizer {
//there is probably a more efficient way to keep track of contact objects
//but right now i can't think of one, because for things like a tree, we need to
//be able to identify objects with the same name (John Smith), but that
//have different unique ids, since we only want one of each Contact object
//in this cache.
private List<Contact> contacts = Collections.synchronizedList(new ArrayList<Contact>());
/**
This method will execute every time someone makes a new Contact object.
If it already exists, return it from the cache in this.contacts. Otherwise,
proceed with the object construction and put that object in the cache.
**/
@Around("call(public Contact.new(Integer,String,String)) && args(id,name,phone)")
public Object cacheNewContact(ProceedingJoinPoint joinPoint, Integer id, String name, String phone) {
Contact contact = null;
for (Contact c : contacts) {
if (id.equals(c.getId()) {
contact = c;
break;
}
}
if (contact == null) {
contact = (Contact) joinPoint.proceed();
this.contacts.add(contact);
}
return contact;
}
/**This should execute every time a setPhone() method is executed on
a contact object. The method looks for all Contacts of the same
name in the cache and then sets their phone number to the one being passed
into the original target class.
Because objects are passed by reference until you do a reassociation,
calling c.setPhone on the object in the cache should update the actual
instance of the object in memory, so whoever has that reference will
get the updated information.
**/
@After("execution(example.Contact.setPhone(String) && args(phone)")
public void syncContact(JoinPoint joinPoint, String phone) {
Contact contact = joinPoint.getTarget();
for (Contact c : this.contacts) {
if (c.getName().equals(contact.getName()) {
c.setPhone(phone);
}
}
}
}
Anche in questo caso, c'è probabilmente 100 wa Si potrebbe ottimizzare questo, dal momento che sto digitandolo dalla cima della mia testa; cioè, se volessi percorrere questa strada in primo luogo. In teoria dovrebbe funzionare ma non l'ho ancora provato.
In ogni caso, Happy Springing!