मैं दो संभावनाओं के बारे में सोच सकता हूं।
एक है (यह एक हैक की तरह है), यदि आपके पास बहुत से बीन्स नहीं हैं जिन्हें आपके उदाहरण में किसी तरह से जोड़ने की आवश्यकता है, तो आप जॉइन वर्क को जॉन होम बीन में और जॉनहोम.सेटफोन में इंजेक्ट कर सकते हैं आप johnWork फोन संपत्ति, की तरह कुछ अद्यतन कर सकते हैं:
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;
}
}
या आप HomeContact और WorkContact है दोनों एक वर्ग से संपर्क का विस्तार करने और उस के साथ ही इंजेक्शन कर सकता है।
यदि आपके पास टन और टन बीन्स हैं, तो इसकी आवश्यकता होगी (जैसे कि आपका आवेदन वास्तव में संपर्क जानकारी से निपट रहा है), एओपी के साथ (आपको दिए गए उदाहरण के लिए एस्पेक्टजे की आवश्यकता होगी) मुझे लगता है कि आप ऐसा कुछ कर सकते हैं (यदि आप वस्तुओं का एक टन प्राप्त करते हैं, तो यह थोड़ा स्मृति गहन होगा, लेकिन आप देख सकते हैं कि यह कैसा काम करेगा):
चेतावनी: यह वास्तव में जटिल तेज़ हो गया, लेकिन मुझे पूरा यकीन है कि यह काम करेगा तुम्हारे जाने के बाद कुछ अड़चनों
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);
}
}
}
}
फिर बाहर काम किया है, वहाँ शायद 100 वा है हाँ आप इसे अनुकूलित कर सकते हैं, क्योंकि मैं इसे अपने सिर के ऊपर से टाइप कर रहा हूं; यही है, अगर आप इस मार्ग को पहली जगह जाना चाहते थे। सिद्धांत रूप में यह काम करना चाहिए लेकिन मैंने इसका परीक्षण नहीं किया है।
वैसे भी, हैप्पी स्प्रिंगिंग!