Spring provides a lightweight solution for compiling enterprise applications. It also supports declarative transactions, remote calls using RMI or web Services, and persistence of data to databases in multiple ways.
Like other frameworks, spring also has a main configuration file: applicationContext. xml.
IOC: Control inversion. This is also called; DI: dependency injection.
AOP: frontend aspect programming.
So-called control inversion: We used to call methods of a class. The first is the new class. Then call this method. Control is on our own. Reversal: As the name implies, control switches the role. Spring creates an object for us. Instead of creating it by yourself. Here we use a design pattern: factory pattern: in fact, the objects created by spring are completed by the factory. We can get this object.
First, let's look at a simple example: there is a person class. It has the work method. Now we need to call this method on the client: If spring is not needed. It must be called directly by a new object.
Now let spring create this object for us. First, you must write a bean class in applicationContext. xml.
[Java]
<Span style = "white-space: pre"> </span> <bean id = "chinese" class = "com. young. person"> </bean>
Call on the client:
[Java]
<Span style = "white-space: pre"> </span> ClassPathResource cpr = new ClassPathResource ("applicationContext. xml"); // load the configuration file
XmlBeanFactory factory = new XmlBeanFactory (cpr); // obtain the class factory object
Person chinese = (Person) factory. getBean ("chinese"); // get Class Object
System. out. println (chinese. sayHi ("Zhang San"); // CALL THE METHOD
You can. Is it very troublesome. Yes. In this case, it is really better to simply create a new object. However. If your project is too large. You can save a lot of trouble.
Then we are coming to a more complicated one. Now the person class must work with tools. Let's write another interface tool named Tools, which has the getHelp method. Implement the methods in tools in a toolsA.
First. We can write a private tool variable in the person class. Then add the setter method to him. You can use this variable to directly call the getHelp method in the work method.
We still need to configure this toolsA implementation class in the applicationContext. xml file. Then put it in the bean of person.
[Java]
<Span style = "white-space: pre"> </span> <bean id = "tool" class = "com. young. toolsA"> </bean>
<Bean id = "person" class = "com. young. person">
<Property name = "this is the variable name of the interface in the person class" ref = "tool"> </property>
</Bean>
If you want to call other functions, you can directly write the implementation class. Store a class variable in the person class. Or interface.
Configure the implementation class in the configuration file. And put it into the property value of the person class. You can call it directly.
This is the legendary dependency injection. Or control reversal. Before you call the method. It automatically creates an object for you. The above is actually one of the methods of dependency injection: the constructor can also be used to inject values. Example: www.2cto.com
[Java]
<Span style = "white-space: pre"> </span> public person (Tools t, Test tt ){
This. t = t;
This. tt = tt;
}
If so. In the configuration file, you need to change it. The property attribute cannot be reused.
[Java]
<Span style = "white-space: pre"> </span> <constructor-arg ref = "test"> </constructor-arg>
<Constructor-arg ref = "tool"> </constructor-arg>
Author: yy008871