Access control permissions in Java are:
Public: Unlimited access to anyone
Protected: only allowed to call each other in this class, and can also be called by subclasses based on the parent class (that is, the subclass can call the parameters or methods defined by protected in the parent class)
Default: The default permissions are called by the same package class (that is, com.xx can invoke other classes, properties, and methods under the COM package, but cannot invoke classes or properties or methods defined by default permissions under COM.DEMO.XX)
Private permissions, which can only be called from one another in this class and not open to the outside
Public class dem { private dem () {} publicstatic dem dem () { C11>returnNew Dem ();} }
You can define a construction method as a private method that controls the creation of an object. The object is created by using a static method Dem (), which prevents the class from being inherited (because the inherited subclass takes precedence over the construction method of the parent class).
After a bit of understanding of Java's permissions control, a sudden problem was found:
If the spring container is injected into the container by means of the constructor or set method of the class, can spring still create objects in the container if the methods are set to private?
The answer is yes.
I wrote a simple test demo to define the constructor of a class as private, and then through the @component annotations are normally scanned at the time the container is initialized.
Find the object in the container that has this bean, the description is created normally. It is a private construction method that can be created normally.
The reason is that the reflection in the spring source ignores the permissions control of Java.
Fields[i].setaccessible (true); Public voidSetaccessible (BooleanFlagthrowsSecurityException {securitymanager sm=System.getsecuritymanager ();
Gain access to private based on whether the security of the system is setif(SM! =NULL) sm.checkpermission (access_permission); SETACCESSIBLE0 ( This, flag); The unique name that is currently defined is Suppressaccesschecks, which allows for the cancellation of standard Java language access checks performed by the reflective object on its point of use-for public, default (package) access, protected, private members. Static Final PrivateJava.security.Permission access_permission =NewReflectpermission ("Suppressaccesschecks");
Reflection sets the System.setsecuritymanager system security when initializing the required instance.
So when spring creates the bean through reflection in the initial session container, it ignores the restriction of access permission!
The relationship between the spring container and Java access rights!