1. Getting Started
Spring is a framework for simplifying Java development, where IOC and AOP are two important cores of spring. Because spring is non-intrusive, it manages the bean lifecycle through the IOC container and integrates many other excellent frameworks, so it greatly simplifies our development process. The core of spring includes beans, core, Context, Spel. The file name of the corresponding package is in the form of Spring-expression-4.0.0.release.jar. In spring development, we must introduce these four core packages and a log package into the referenced libraries. Select the packages and add them. Then create a new spring configuration file. The action is to add spring Bean configruation file files under Spring. So the environment is built ...
So how do you load a bean? (Here you need to know what is IOC and DI)
The first step: Configure the Bean in the XML configuration file first, as follows:
class= "Com.test.demo.Person" >2 <property name= "name" value= "Zhao Two dog" ></property >3 <property name= "age" value= "Up" ></property> 4 </bean>
Step Two: Create the IOC container and load the bean as follows:
ApplicationContext C =new classpathxmlapplicationcontext ("Context.xml"); = (person) c.getbean ("Zhao"); System.out.println (Zhao.tostring ());
2.ioc&di Overview:
The IOC is for control inversion, Di for dependency injection, and Di is a more specific description of the IOC.
The evolution of the IOC is: separation interface and implementation, abstract factory--inversion control
configuration of 3.Bean:
There are 2 types of bean configuration: one based on XML file, and one based on annotations.
The bean is configured in a way that requires a parameter-free constructor in the bean by reflection.
Spring provides the implementation of the IOC container in 2: A beanfactory based, Beanfactory-based sub-interface ApplicationContext that provides more functionality that is typically initialized with the latter.
The bean is obtained by means of the ID name and by the following class:
1 person Zhao = (person) c.getbean ("Zhao"); 2 Person zhao1 = C.getbean (person. Class);
Properties are injected in the following ways: Set method injection, constructor injection, abstract factory injection (not recommended), constructor injection configuration as follows:
Constructor injection can also have an index, type attribute for precise injection.
<= "Chen" class= "Com.test.demo.Person" index= "" type= "" > <value= "Chen San egg"></constructor-arg ></beans>
Special characters via <! [cdata[]]> to introduce.
<name= "word">
< value > <! [cdata[<HTML> number one ]]></value> </ Property >
Tags are introduced through the ref attribute, and beans can be created internally, and internal beans cannot be externally referenced
<name= "Car" ref= "Benz"></ Property >
Supports CASCADE injection:
<name= "car.name" value= "Audi Q800" ></Property>
Inject list, set, Map collection class:
< Propertyname= "Cars"> <List> <refBean= "Benz"/> <refBean= "Benz"/> </List></ Property>
How to configure Java.util.Properties: a subclass of//hashtable
<props><prop key= "" >VALUE</PROP>
external configuration <map>, configuring a standalone collection bean, introducing util
<id= "Mycars"> <bean= " Benz "/> <bean=" Benz "/> </util:list>
Use the P namespace: introduce a P-namespace to simplify configuration
<id= "Zhao" class= "Com.test.demo.Person" p:name = "Zhao Tie egg" p:cars-ref= "Mycars"></bean>
4. Automatic assembly:
SPRINGIOC containers can be automatically state. It is only necessary to specify the mode of automatic assembly in the Autowire attribute of <bean>. Disadvantage, all properties are configured, which is not very good.
Bytype (multiple assembly easy initial problem), ByName (name in ID name and setter method), constructor (not recommended)
5. Configure the bean relationship:
Parent= "" Inheritance Configuration
Abstract= "true" declares that abstract beans cannot be configured.
Configure the Affinity Depends-on property to indicate that other beans need to be configured before the bean is configured
Specifying multiple beans by commas and spaces
Spring Introduction First lesson: Spring Basics and Configuration beans