zoukankan      html  css  js  c++  java
  • Spring的两种注入方式

    1.set注入(更灵活)

    <?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:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                               http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/tx  
                               http://www.springframework.org/schema/tx/spring-tx.xsd
                               http://www.springframework.org/schema/context 
                               http://www.springframework.org/schema/context/spring-context.xsd">
    			
    			<!-- 配置JavaBean -->
    			<bean id="Stu" class="com.hp.pojo.Student" >
    			<!-- set注入 -->
    				<property name="name" value="mm" />
    				<property name="pwd" value="123" />
    			</bean>
    </beans>
    

    2.构造器注入(实体类要有带参的构造器)

    <?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:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                               http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/tx  
                               http://www.springframework.org/schema/tx/spring-tx.xsd
                               http://www.springframework.org/schema/context 
                               http://www.springframework.org/schema/context/spring-context.xsd">
    			
    			<!-- 配置JavaBean -->
    			<bean id="Stu" class="com.hp.pojo.Student2" >
    			<!-- 构造器注入 -->
    			<!-- index构造方法参数的索引下标,从0开始 -->
    				<constructor-arg index="0" value="1"></constructor-arg>
    				<constructor-arg index="1" value="ww"></constructor-arg>
    				<constructor-arg index="2" value="123"></constructor-arg>
    			</bean>
    </beans>
    


  • 相关阅读:
    POJ NOI MATH-7650 不定方程求解
    POJ NOI MATH-7656 李白的酒
    POJ NOI MATH-7654 等差数列末项计算
    POJ NOI MATH-7827 质数的和与积
    POJ NOI MATH-7830 求小数的某一位
    POJ NOI MATH-7833 幂的末尾
    POJ NOI MATH-7829 神奇序列求和
    POJ NOI MATH-7826 分苹果
    UVALive5661 UVA668 ZOJ2037 Parliament
    POJ1032 Parliament
  • 原文地址:https://www.cnblogs.com/mlan/p/11060358.html
Copyright © 2011-2022 走看看