1. Spring Container type: applicationcontext.xml (this XML is built in SRC directory)
<?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:context= "Http://www.springframework.org/schema/context"
Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:jee= "Http://www.springframework.org/schema/jee"
xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd "
Default-lazy-init= "true" ><!--default-lazy-init= "true" setting is loaded when called
</beans>
=============================================================================================================== ===================================
2, the container instantiation:
String str = "Applicationcontext.xml";
ApplicationContext context = new Classpathxmlapplicationcontext (str);
Context.getbean ("id", classname.class);//Get an instance of the class and automatically call the parameterless constructor to create the object
=============================================================================================================== ===================================
3. Injection:
<bean id= "Hello" class= "Org.great.ben.HelloWorld" >
<property name= "UserName" value= "QQQQQQ" ></property><!--injecting values into a class using setter injection--
<!--the way the constructor is injected, index represents the location of the constructor parameter, or you can use the Name property--
<constructor-arg index= "0" value= "constructor injection" ></constructor-arg>
<constructor-arg index= "1" value= "5" ></constructor-arg>
</bean>
=============================================================================================================== ===================================
4, automatic assembly:
<bean id= "Refbean" class= "Org.great.ben.RefBean" autowire= "byname" ><!--using Autowire, automatic assembly, here by property name Assembly--
<!--<property name= "Hello" ref= "Hello" ></property>--><!--inject an object into a property in the class, where automatic assembly is not used, you need to set the Ref Property Association -
</bean>
ByName: Automatically assembled according to the attribute name (if there is a Stu attribute in the bean, a bean definition named Stu is found);
Bytype: If there is a bean in the container that is the same as the specified property type, it will be automatically assembled with the property (the Bean's property is the type of another bean);
Constructor: Applied to the constructor parameter, if the bean not found in the container with the same constructor parameter will report the exception;
AutoDetect: To decide whether to use constructor or bytype through an introspective mechanism
=============================================================================================================== ===================================
5. Close resources: (the method to destroy is not called until the resource is closed)
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Classpathxmlapplicationcontext CAC = (classpathxmlapplicationcontext) context;
Cac.close ();
=============================================================================================================== ===================================
6. Annotations:
1) Dependency Injection:
@Autowired: Constructor injection and setter injection (constructor injection preferred) can be handled (injected by Bytype)
@Resource: can only handle setter injection (Setter injection preferred) (default by ByName injection, type can be set)
--------------------------------------------------------------------------------------------------------------- -----------------------------------
2) Automatic Scan mark: (There is a secondary mark on the class, can be automatically scanned into the spring container for definition, no need to customize in the container again)
@Component General Annotations
@Named General Annotations
@Repository Persistence Layer Component annotations
@Service Business layer Component annotations
@Controller Control Layer Component annotations
@PostConstruct
public void init () {}//Initialization callback method
@PreDestroy
public void Destroy () {}//Destroy callback method
--------------------------------------------------------------------------------------------------------------- -----------------------------------
3) The value in @Value ("#{user.username}") "" can be a base type, or it can be an ID referenced from a container
--------------------------------------------------------------------------------------------------------------- -----------------------------------
=============================================================================================================== ===================================
7, set injection: <util:list/>, <util:set/>, <util:map/>, <util:properties/>
1) Use annotation @value ("#{stulist}") to assign the value of this list to the attributes in the bean (<util:set/>, <util:map/> similar)
<util:list id= "Stulist" >
<value>A</value>
<value>B</value>
</util:list>
--------------------------------------------------------------------------------------------------------------- -----------------------------------
2) Use Util:properties to read the file, then use #{user.username} to get the attribute value in the file
<util:properties id= "user" location= "classpath:userText.properties" ></util:properties>
=============================================================================================================== ===================================
The configuration usage of spring