Spring Learning (2) --- Assembling Beans in the IoC Container (2)

##Detailed explanation of four injection parameters ###1.   There are 5 special characters in
literal XML, namely: &<>"'. If the injection value in the configuration file includes these special characters, special processing is required. There are two
One solution: first, use the <![CDTA[]]> special tag to encapsulate the string containing special characters; second, use XML Zhuangyi sequence to represent these special characters.
 
###2. Quoting others Bean

```XML
<bean id="car" class="com.baobaobao.attr.Car"/>
<bean id="boss" class="com.baobaobao.attr.Boss">
  <property name="car ">
  <!-- Refers to the car Bean defined above-->
    <ref bean="car"></ref>
  </property>
</bean>
```
The <ref> element can refer to the container through the following three properties Other beans in the bean
- bean: through this attribute, you can refer to the bean in the same container or parent container,
- local: through this attribute, you can only refer to the bean defined in the same configuration file
- parent: refer to the bean in the parent container

###3. Internal Bean
If the car bean paper cup boss bean is referenced and not referenced by any other bean in the container, the car can be injected into the boss as an internal bean:
```XML






















<bean id="boss" class="com.baobaobao.attr.Boss">
<property name="favorites">
   <list>
    <value>aaa</value>
    <value>bbb</value>
    <value>ccc</value>
   </list>
</property>
</bean>
```
####set
```XMl
<bean id="boss" class="com.baobaobao.attr.Boss">
<property name="favorites">
   <set>
    <value>aaa</value>
    <value>bbb</value>
    <value>ccc</value>
   </set>
</property>
</bean>
```
####map
```XMl
<bean id="boss" class="com.baobaobao.attr.Boss">
<property name="favorites">
   <map>
    <entry>
       <key><value>Am</value></key>
       <value>Meet customers</value>
    </entry>
   </map>
</property>
</bean>
```
# ###Properties
```XMl
<bean id="boss" class="com.baobaobao.attr.Boss">
<property name="favorites">
   <props>
    <prop key="jobMail">sss@qq. com</prop>
   </props>
</property>
</bean>
```
There are several types of usage that are basically similar to the above, not listed one by one

##Five method injection
Method injection is used to solve the prototype type Bean injection.
###1.lookup method injection
<bean id="car" class="com.baobaobao.injectfun.Car"
  p:brand="红旗" p:price="20000" scope="prototype"/>
//implement method injection
<bean id="magicBoss" class="com.baobaobao.injectfun.MagicBoss">
 <lookup-method name="getCar" bean="car"/>
</bean>

Provide a dynamic implementation for MagicBoss' getCar() through the lookup-method element tag, and return the prototype carBean, so that Spring will provide a dynamic implementation for the MagicBoss interface at runtime.


The relationship between six <bean>
1. Inheritance
//Define abstract bean
<bean id="abstractCar" class="com.baobaobao.Car"
    p:brand="红旗" p:price="20000" p:color="黑色" abstract="true"/>
//Inheritance and abstract
<bean id="car" p:color="红色" parent="abstractCar">

The main function of the parent <bean> is to simplify the configuration of the child <bean0>, so it is generally declared as abstract="true", indicating that the <bean> is not instantiated as a corresponding bean. If the user does not specify abstract="true", the Spring IoC container will instantiate a bean named abstractCar.

2. Dependency Two tags are mainly used: 1. <ref> 2. <depends=on>
3. Reference

Seven Bean scopes
category illustrate
singleton There is only one Bean instance in the Spring IoC container, and the Bean exists as a single instance
prototype Every time the Bean is called from the container, a new instance will be returned, that is, every time getBean() is called, it is equivalent to executing the operation of new XXXBean().
request A new bean is created every HTTP request, this scope is only available in the WebApplicationContext environment
session The same HTTP Session shares a Bean, and different HTTP Sessions use different Beans. The scope only applies to the WebApplicationContext environment
globalSession The same global session shares a bean, which is generally used in the Protlet application environment. The scope only applies to the WebApplicationContext environment


Eight Annotation-based configuration
1. Using annotations to define beans
  is now basically used @Repository: used to annotate DAO implementation classes; @Service: used to annotate Service implementation classes; @Controller: used to annotate Controller implementation classes . But it can also be annotated with @Component

2. Start the Spring container with configuration information
//Scan the class package to apply the Bean defined by the annotation first declare the context namespace
<context:component-scan base-package="com.baobaobao.anno"/>

//filter  
//1. Scan a specific package
<context:component-scan base-package="com.baobaobao" resource-pattern="anno/*.class"/>

//2.
<context:component-scan base-package="com.baobaobao.anno">
 <context:include-filter type="regex" expression="com\.baobaobao\.anno."/>
 <context:exclude-filter typr="aspectj" expression="com.baobaobao..*COntroller+"/>
</context>

Guess you like

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