Spring Bean manual automatic injection (XML)

tags: spring  

<! - Manual assignment ->
  <bean id="student1" class="com.yq.shoudong.Student">
        <property name="name" value="yq"/>
        <property name="age" value="10"/>
    </bean>

    <bean id="school1" class="com.yq.shoudong.School">
        <property name="name" value="yq"/>
        <property name="address" value="Nanjing"/>
        <! - The reference type assignment REF is consistent with the ID of the assignment bean ->
         <property name="student"  ref="student1"/>
    </bean>
    <! - BYNAME Automatic Injection ->
    <! - Reference type automatic injection, Spring framework can assign a reference type according to certain rules ->
    <! - Using rules are commonly used by BYNAME BYTYPE
         1. BYNAME (according to Name Injection): The reference type attribute name in the Java class is the same as the ID name of the (configuration file) <bean> in the Spring container.
         And the data type is consistent, Spring can assign value reference types
         Grammar rules:
    <bean id=xxx  class=xxx autowired=byName/>
    -->
    <bean id="student" class="com.yq.autowired.Student">
        <property name="name" value="yq"/>
        <property name="age" value="10"/>
    </bean>

    <bean id="school" class="com.yq.autowired.School" autowire="byName">
        <property name="name" value="yq"/>
        <property name="address" value="Nanjing"/>
    </bean>

    <! - ByType Automatic Injection ->

<! - Injecting type injecting, only one type injection ->
    <! - 1, directly according to type ->
<!--    <bean id="studentByType1" class="com.yq.autowiredByType.Student">-->
<!--        <property name="name" value="yx"/>-->
<!--        <property name="age" value="10"/>-->
<!--    </bean>-->

    <! - 2, injecting sub-type ->
<!--        <bean id="Monitor" class="com.yq.autowiredByType.Monitor">-->
<! - <property name = "name" value = "Sub Type Inject" /> ->
<!--            <property name="age" value="10"/>-->
<!--        </bean>-->

    <! - 3, press the implantation ->
    <bean id="aClass" class="com.yq.autowiredByType.ClassImp">
        <property name="id" value="Implementation"/>
    </bean>

    <bean id="schoolByType1" class="com.yq.autowiredByType.School" autowire="byType">
        <property name="name" value="nanda"/>
        <property name="address" value="Nanjing"/>
    </bean>

Intelligent Recommendation

Spring construction method injection + + manual injection + + automatic injection

1. Let it call the construction method of the two parameters configured by itself 1.1 Introduction to the use of attributes 1. Commonly used name+value, note that the id name here cannot be the same a...

Spring Bean automatic assembly, injection and post processor

Bean automatic assembly is divided into 5 types: 1.byName attribute name; 2. byType attribute type; 3. Constructor construction method assembly; 4. autodetect checks the internal selection of byType o...

Automatic Injection Mechanism of Bean Objects in Spring

Use the Autowire tab to complete the automatic injection of Beans First of all, we have the following two classes: AutoWire = "byname", injecting according to the member variable name AutoWi...

Spring annotation - Automatic assembly, code injection bean

This is the location map of the file First, automatic assembly Add to CONTEXT in the bean file The bean here is a lot, I want to know all the code, see the column of Spring about Spring in my account,...

Spring bean depends on injection and automatic assembly

Dependency injection Rely: refers to the creation of the bean object depends on the container. Dependent resources of bean objects Injection: Refers to the resources dependent on the bean object, set ...

More Recommendation

Regain Java's spring's value injection through bean tags (manual + automatic)

Simple attribute assignment: First create a calss object, write the attribute values ​​we need, and write the set method 2. Create a bean tag in the xml file, and assign a value to the target class th...

spring-xml configuration bean automatic assembly ...

autowire Attribute Value is automatic assembly and three as follows: the byType (depending on the type automatic assembly): If the IOC plurality of containers consistent with the target type Bean Bean...

Spring-6 XML configuration in the automatic assembly Bean

Spring IOC container can be automatically assembled Bean. Needs to do is to specify autowire properties in the automatic assembly mode. the byType (depending on the type automatic assembly): If the IO...

Spring bean automatic assembly - xml configuration

BEAN's automatic assembly Automatic assembly, Spring meets Bean dependencies autowire Spring will automatically find in the context and automatically assemble properties to beans There are three assem...

Spring Di Dependent Injection, Bean All Properties Injection XML Templates

Di: dependency injection Dependence: The creation of the bean object is dependent on the container. Inject: All properties in the bean object are injected by the container. First, the core of Di injec...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top