spring--IOC

IOC:
The so-called inversion of control is to hand over the right to create an object to spring. When we want to use this object, we can get it directly from the spring factory.

Writing steps:
1. Import the jar package
under the jar package spring-framework-3.2.0.RELEASE\libs: 4 core packages
spring-beans-3.2.0.RELEASE.jar; spring-context-3.2.0.RELEASE. jar
spring-core-3.2.0.RELEASE.jar; spring-expression-3.2.0.RELEASE.jar
spring-framework-3.0.2dependencies\org.apache.commons\com.springsource.org.apache.commons.logging\ A 1-dependent package under 1.1.1 :
com.springsource.org.apache.commons.logging-1.1.1.jar
2. Main configuration file:
Location: Any, usually placed under src during
development Name: Any, during development Usually applicationContext.xml
content: schema constraints must be added.
Constraint file location: spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\ xsd-config.html
For example:
<?xml version=" 1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "
       xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
       xsi:schemaLocation=" http://www.springframework.org/ schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd ">
 <!-- Configuration needs to create objects-->
 <bean id="userServiceId" class="cn.itcast.a_ioc .UserService"></bean>
</beans>
3. Test API:
test code:
  //spring create
  //1 load configuration file
  String xmlPath = "cn/itcast/a_ioc/beans.xml";
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext (xmlPath);
  //2 Obtained from the spring factory, obtained through the bean id
  UserService userService = (UserService) applicationContext.getBean("userServiceId");
  userService.addUser();
BeanFactory: It is the core interface of spring, the infrastructure of the spring framework, and it is oriented to spring itself. It is a generic class factory used to create beans.
ApplicationContext: Based on BeanFactory, it provides more application-oriented functions. It also provides internationalization support and framework event system,
making it easier to create practical applications. It is aimed at developers who use the spring framework to develop, and it is used in general development situations instead of the underlying BeanFactory

The instantiation difference between BeanFactory and ApplicationContext:
BeanFactory: When initializing the container, the bean is not instantiated, but the target bean is instantiated when getBean() is called for the first time, that is, when the instance is accessed for the first time.
applicationContext: in All beans are instantiated when the context is initialized.

 

DI:
Dependency: is a: inheritance relationship; has a dependency
Dependency Injection Dependency Injection: When A is created, B in A is also created and B is set to A
<bean id="bId" class="" ></bean>
<bean id="aId" class="">
 <property name="b" ref="bId"></property>
</bean>

Myeclipse schema constraint configuration:
MyEclipse"--->"Files and Editors"---->"XML"--->"XML
Catalog-->"User Specified Entries"" to add constraints.
Note:
key type: select schema location
key: find the corresponding constraint namespace copy URL address.
If there is a problem, you can remove All cached constraint configuration in Cache.

 

Assembly bean: based on xml
mode:
1), default assembly
2), static factory assembly:
create a factory, all methods of the factory are static.
Configuration:
<!-- Static factory instance bean -->
 <bean id="personService" class="cn.itcast.a_ioc.factory.MyFactory" factory-method="createPersonService"></bean>
3), custom Factory assembly:
 <!-- custom factory instance bean -->
 <!-- get custom factory -->
 <bean id="myFactory" class="cn.itcast.a_ioc.factory.MyFactoryy"></bean >
 <bean id="personServicee" factory-bean="myFactory" factory-method="createPersonService"></bean>
 
Scope:
 <!-- Configure the objects to be created-->
 <bean id="userServiceId" class ="cn.itcast.a_ioc.UserService" scope="scope"><
/bean> scope value:
 singleton, singleton (default value).
 prototype, multiple cases. Every time you execute getBean() you will get a new instance. Application: When integrated with struts, there are many cases of action.
 request: servlet request scope, requesting a new object at a time.
 session: servlet session scope, a new object at a time.
 
The life cycle of assembly bean:
<bean id="" class="" init-method="initialization method" destroy-method="destroy method">
The premise of destruction:
1), must be a singleton. 2) The spring container must be closed.
Note:
The closing of the spring container can be done through the close() method provided by the subclass of ApplicationContext ClassPathXmlApplicationContext

 

Attribute injection:
manual injection:
manual injection is based on xml configuration:
1), constructor injection:
<!-- constructor injection
   <constructor-arg> A parameter used to configure the constructor
    name: the name of the parameter, generally not used.
    index: The parameter index value, starting from 0.
     If only index is used, use the type of the first matching constructor
    type: parameter, it is recommended to use the fully qualified class name. java.lang can omit
    value: ordinary value
    ref: reference value, generally another bean object
  such as: name
   <constructor-arg name="uid" value="1"></constructor-arg>
   <constructor-arg name=" username" value="2"></constructor-arg>
  For example: index + type, commonly used in development
   <constructor-arg index="0" type="java.lang.String" value="1"></ constructor-arg>
   <constructor-arg index="1" type="java.lang.Integer"



  <property> To inject properties, you must provide a setter method
   name: the name of the property
   value: normal data
   ref: another bean reference name
 -->

Collection injection:
<!-- Collection
  <property> subtag
   Basic
    common value: <property value=""> Equivalent to <property><value>
    Reference value: <property ref=""> Equivalent to <property>< ref>
   set
    <list>Listset
    <array>array
    <set>Setset
    <map>Mapset
     <entry key="" value="">
    <props>Properties object
     <prop key="">..</prop >
 -->

3), interface injection (spring does not support)

 

p namespace: a simplified version of setter injection.
Configure the namespace before using the p namespace:
copy xmlns=" http://www.springframework.org/schema/beans "
and change it to xmlns:p=" http://www.springframework.org/schema/ p " and placed on top of xsi.
 Use: <bean id="" class="" p:property name="normal value" p:property name-ref="reference value">
                              <property value=""> <property ref="">

Assembly bean: Annotation-based
 Scan class must be configured in xml
 <context:component-scan base-package="...">
1. Component, common. Instead of <bean id="" class="">
 @Component 2. web
development,
 @Controller web layer
 @Service service layer
 @Repository dao layer
3. Inject
 common values: @Value
 reference value
  @Autowired injects
  @Qualifier by type by default ( name) + @Autowired combined with injection by name to replace <property>
  @Resource(name)
4. Other
 scopes
  @Scope("prototype") replace <bean id="" class="" scope="">
 initialize or destroy
  @ PostConstruct replaces <bean id="" class="" init-method="">
  @PreDestroy replaces <bean id="" class=""

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326554095&siteId=291194637