Use MyEclipse to develop a demo for spring.
First step: Create a new Web project.
Step Two: Install the spring-developed support package.
After installing a few more things
3, define an operation interface:
Package com. Spring.demo; Public Interface ifruit { publicvoid eat ();}
4, defines the interface operation subclass.
Package com. Spring.demo; Public class Implements ifruit { @Override publicvoid eat () { System.out.println ("Eat apples" ); } }
There are no factory-operated classes, because spring itself has a factory class. The factory class should not appear after the work.
5. Modify the Applicationcontext.xml file.
<?xml version= "1.0" encoding= "UTF-8"?><beans xmlns= "http://www.springframework.org/schema/ Beans " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xmlns:p="/http/ www.springframework.org/schema/p " xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.1.xsd "> class=" com. Spring.Demo.Apple "></bean></beans>
6, then start the spring container through a specific pattern.
Find the sub-class operator interface.
Packagecom. Spring.demo;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTestapple { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); ifruit Fruit=ctx.getbean ("Apple", Apple.class); Fruit.eat (); }}
There is no clear use of the factory design pattern during the entire code operation, as all the factories are handled by spring to help the user. And in
In the Applicationcontext.xml configuration file, all objects are instantiated by default when the spring container is started.
When used, it can be taken out according to the ID name.
Classes used in the process:
1, Application Context class: ApplicationContext
All configured information needs to be read by this class.
Common methods:Getbean (String name,class<t> requiretype). Gets the Bean object with the specified name, and sets the bean type of the generic to the specified operation, avoiding downward transformation ;
2, because resource control files may be in any location, such as classpath or disk, you must use related subclasses:
Classpathxmlapplicationcontext ("ClassPath"); indicates that the resource file is read in ClassPath.
All of the spring-configured <bean> elements are automatically initialized when the spring container is started, so you have prepared the instance for use by the user before using the object.
when the user is in use, it is only necessary to get the object instance through the bean's ID and assign to the object declaration .
You can also configure the interface if you have a more stringent configuration (but it's useless)
class abstract= "true"></bean> classparent= "Fruit"></bean>
Because the interface is abstract, it means that the interface object is not instantiated. The addition of the parent to the Apple element is only illustrative.
Summarize
Spring is a very large factory , the instantiation of all objects and other assigned parts, do not require user assignment processing, all to spring complete.
The user simply needs to refer to the object instance as required.
04-spring-control reversal