spring inheritance injection and automatic injection

1, inherited injection

  Inherit injected into two categories: general and abstract inherit inheritance inject injection

  1-1, ordinary succession injection

  Common inherited injection, only need to set the parent of the child bean class attributes of the parent class can be a bean

  

<! - parent class the bean -> 
< the bean ID = "Person" class = "com.parentchild.Person" 
 P: name = "Bob" P: Age = "15" > 
</ the bean > 
<! - Sub class: class attribute inherited from the parent class will be provided after the parent -> 
< the bean ID = "man" class = "com.parentchild.Man"  
P: Sex = "M" parent = "Person" > 
</ the bean > 

< ! - may not be used to set the parent class parent bean, is a subclass of spring managed automatically inherit the properties of the parent class 
  can set the properties of the parent class directly in a subclass, as follows, but is not recommended -> 
<bean id="man" class="com.parentchild.Man"
p:sex="男" p:name="小明" p:age="115">
</bean>

 

  1-2, abstract inherit injection

  Abstract parent class may not exist, the abstract set to true, the premise is a subclass of the abstract need to have a parent class in the bean property, or injection failure.

  If the abstract parent class exists, set to true for the abstract parent class bean.

<bean id="myperson" abstract="true">
        <property name="name" value="p1"></property>
        <property name="age" value="2"></property>
</bean>
<bean id="man2" class="com.parentchild.Man"
p:sex
="男" parent="myperson">
</bean>

2, the automatic injection

spring automatically inject the bean must be managed by the spring bean

Automatically import can not have the same import the bean id


default-autowire-candidates = "* dao" match any dao end of the bean
default-the autowire = "constructor" in constructor match the
default-autowire = "byName" Attribute names match the
default-autowire = "byType" attribute type match
default-autowire = "no" non-
default-autowire = "default" default matching

 

Set in the beans

default-autowire = "byType" Automatic Matching mode

default-autowire-candidates = "* dao" Automatic candidate

It means: all the bean to byType will automatically match ends with dao bean.

<?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"
default-autowire="byType" default-autowire-candidates="*dao">
    <bean id="empdao" class="com.autowire.EmpDaoImpl">

    </bean>

    <bean id="empservice" class="com.autowire.EmpServiceImpl">

    </bean>
</beans>

You may be provided only in the automatic injector in the autowire bean This bean

<bean id="empservice" class="com.autowire.EmpServiceImpl" autowire="byType">

</bean>

 

Guess you like

Origin www.cnblogs.com/liweixml/p/11788738.html