The operating principle JDK_ dynamic proxies and some small details

An abstract class caCu
an implementation of the abstract class cacufun
a proxy class Proxi

ApplicationContext ioc=new ClassPathXmlApplicationContext("apContext.xml");
	caCu test=ioc.getBean(caCu.class);
	int result=test.add(3, 4);
	System.out.println(test.toString()+"\n"+test.getClass());

The output is

cacufun [getClass()=class ProxyShows.cacufun, hashCode()=490391704, toString()=ProxyShows.cacufun@1d3ac898]

class com.sun.proxy.$Proxy18

It can be seen getBean acquired test is not cacufun itself, but the proxy class Proxy, but it's still a normal call toString (as long as the public method cacufun class can be properly invoked)

If the interface for the cacuThe same packageIn the establishment of more than one implementation classandThese two classes are marked head @Service conflict occurs.

No qualifying bean of type 'ProxyShows.caCu' available: 
expected single matching bean but found 2: cacufun,cacufun2

It can be summed upThe operating principle of the proxy class: find matching by incoming interfaces A class implements this interface in the bag marked A and Class B. @Service

Principle proxy class is based on the 23 design patterns of proxy mode, ProxySubject reference RealSubject and adding to its various services.

In addition, the reason for obtaining the abstract class object getBean returned instead of the implementation class is because the operating principle of the above proxy class caused all kinds of information that is often say: Acting classes can only have contact via the interface and implementation class to class.

Released seven original articles · won praise 0 · Views 68

Guess you like

Origin blog.csdn.net/weixin_43458072/article/details/102568166