See more here. The words “private” and “public” were written backwards. And it feels like it’s backwards in some places.


The main purpose of this title is to facilitate the search for matching keywords. Here is my understanding.

1. For attributes

In the case of Controller, it makes no difference whether a service imported using @autowired or other annotations is private or public.

2. For methods

2.1 General Conditions

For basic use, there is no difference between using private and public methods in controller.

2.2 Special Cases

If aop is used, the public method is unaffected, but the private modified method calls no operations and imports null objects such as service (only affects the private method, Debug allows you to observe that service objects are not null when calling other public methods.

2.3 the reason

JDK proxies or cglib proxies generate proxy objects that do not contain private methods, so calling a private method calls a real object’s method:

  1. Calling real objects does not enter the aspect, that is, some of the pre-processing and post-processing operations defined by the aspect are not performed;
  2. Real objects are not managed by the Spring container, so imported services are null.

Calling a public or protected method invokes the corresponding method of the proxy object. Proxy objects are Spring-managed beans into which other beans such as Services are injected.