The relationship between the spring container and Java access rights!

Source: Internet
Author: User

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 set
if(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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.