Getting Started with Spring (3) Spring load Configuration Web

Source: Internet
Author: User

Spring initializes the container. Three kinds of implementations that are often used:

First, Classpathxmlapplicationcontext: loading from the classpath.

Second, Filesystemxmlapplicationcontext: loading from the file system.

Third, Xmlwebapplicationcontext: loading from the web system.

Using the 1, Bean Factory: The simplest container provides the foundation for dependency injection support. Create various types of beans.

Beanfactory factory = null; Statement

Classpathresource resource = new Classpathresource ("Spring.xml"); Class path

Filesystemresource Filesystemresource =

New Filesystemresource ("d:\\ncode\\mcode\\sday02 \\src\\spring.xml");

System path

Inputstreamresource Res

= New Inputstreamresource (

New FileInputStream ("d:\\ ncode \ Mcode \\sday02\\ src \\spring.xml"));

factory= new Xmlbeanfactory (Resource);

Xmlbeanfactory (Parameters can be resource or filesystemresource, etc.)

But can not be res reason to view: Document part Iii. Core Technologies 6. Resources

6.2 The Resource interface The description of the IsOpen method);

Factory = new Classpathxmlapplicationcontext ("Spring.xml");

Loading from the Classpath

Factory = new

Filesystemxmlapplicationcontext ("D:\\ncode\\mcode\\sday02\\src\\spring.xml"); Loading from the file system

HelloService HelloService =

Factory.getbean ("Helloserviceimpl", Helloserviceimpl. Class);

Helloservice.sayhello ();

Use 2,

Application context: Built on the bean factory basis, providing system architecture services. Applicationcotext,spring a more advanced container. Strong function:

1. Provides text information parsing tools, including support for internationalization.

2. Provide a generic method for loading file resources, such as pictures.

3. You can send events to a bean registered as a listener.

In rare cases, use beanfactory, such as in mobile devices.

ApplicationContext context = new

Filesystemxmlapplicationcontext ("File:d:\\ncode\\mcode\\sday02\\src\\spring.xml");

context = new

Classpathxmlapplicationcontext ("Classpath:spring.xml");

HelloService HelloService =

Context.getbean ("Helloserviceimpl", Helloserviceimpl. Class);

Helloservice.sayhello ();

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.