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 ();