While adding some unit tests to older Spring code, I noticed that the production Spring configuration was wiring together some pretty simple Factory objects that had private init() methods (took a few minutes to figure out why I kept getting wavy lines), that looked like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private void init(){ try { StringBuilder sb = new StringBuilder(); for(String server : servers){ sb.append(server.trim()); sb.append(BLANK_SPACE_DELIMITER); } memCacheClient = new MemcachedClient(AddrUtil.getAddresses(sb.toString())); } catch (IOException e) { log.error("Unable to Connect to memCache " ,e); throw new RuntimeException("Unable to Connect to memCache " , e); } } |
It turns out that AbstractAutowireCapableBeanFactory does a bit of reflection when processing ‘init-method’ to allow it to invoke private methods (assuming you don’t have any SecurityMangers running, which you probably don’t)
1 2 3 4 | ReflectionUtils.makeAccessible(initMethod); try { initMethod.invoke(bean, (Object[]) null); } |
So… magic…