About spring-managed classes how to create objects

Source: Internet
Author: User

There was a null pointer error in the project today, but I knew what was wrong when I saw the mistake. And I used to say, no real amends. Today, we share the improvement method with everyone. Now our project, most of our management method is to the spring to manage, as for the benefits I do not more than the repeat, with the people know that spring also put his management to the extreme, really is to the extreme ah. To spring Management if the instantiation of its object will be an error, the error is the null pointer errors, because it is estimated that because there is no injection, we instantiate the object when the management of the spring is broken.

Http://blog.csdn.net/kobe_lzq/article/details/4516362 said a bunch of crap, the following talk about the solution, after encountering such a thing, we do not worry, since we can not create its object, we may choose another One way. is to get this object by reading the spring configuration file and getting the bean in it so that it can be resolved.

For example:

ApplicationContext ac = new Filesystemxmlapplicationcontext ("Applicationcontext.xml");
      Ac.getbean ("Beanid");  
Use this method to read the Applicationcontext.xml configuration file. It is important to note that Ac.getbean ("Beanid"); Get up is an object
It is also important to note that the configuration file that he reads is from the root path of the system file. If you want to use the virtual path classpath need to write the following pattern:
        
ApplicationContext ac = new Filesystemxmlapplicationcontext ("Classpath:applicationContext.xml");
      Ac.getbean ("Beanid");  
You can also use:
ApplicationContext factory=new classpathxmlapplicationcontext ("Classpath:appcontext.xml");
This type of pattern goes to read the configuration file.
The idea is that we can't instantiate his object directly, but we can read them out of the configuration.
Another pattern you can use is:

Ac1.getbean ("");
This method can be given to all the spring management to get out, only need to Ac1.getbean (""); "" The name in the configuration file is the same as the <bean id= "" ></bean>id.
For the time being, I only use either of these methods.

About spring-managed classes how to create objects

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.