Spring 依赖注入(DI)

依赖注入,可注入的类型有:
字符串/整数/小树/布尔值
数组
List集合
Map集合
Propert对象
JvaBean
List+JavaBean

首先创建实体类
package star.july.c_di;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Student {
          private String name;
          private int age;
          public Student() {
                   System.out.println("执行了Student构造方法");
          }
          public Student(String name, int age) {
                   System.out.println("调用有参构造方法");
                   this.name = name;
                   this.age = age;
          }
          // 注入字符串类型
          private String stuname;
          public void setStuname(String stuname) {
                   this.stuname = stuname;
          }
          // 整型类型
          private int length;
          public void setLength(int length) {
                   this.length = length;
          }
          // 数组
          private String[] array;
          public void setArray(String[] array) {
                   System.out.println("数组注入");
                   this.array = array;
          }
          // 集合
          private List list;
          public void setList(List list) {
                   this.list = list;
          }
          // Map集合
          private Map<String, String> map;
          public void setMap(Map<String, String> map) {
                   System.out.println("Map注入");
                   this.map = map;
          }
          // Properties注入
          private Properties prop;
          public void setProp(Properties prop) {
                   this.prop = prop;
          }
          //List+JavaBean
          private List<Course> ljb;
          
          public void setLjb(List<Course> ljb) {
                   this.ljb = ljb;
          }
          @Override
          public String toString() {
                   return "Student [name=" + name + ", age=" + age + ", stuname="
                                      + stuname + ", length=" + length + ", array="
                                      + Arrays.toString(array) + ", list=" + list + ", map=" + map
                                      + ", prop=" + prop + ", ljb=" + ljb + "]";
          }
}




JavaBean:

package star.july.c_di;
public class Course {
          private String name;
          public String getName() {
                   return name;
          }
          public void setName(String name) {
                   this.name = name;
          }
          @Override
          public String toString() {
                   return "Course [name=" + name + "]";
          }
          
          
}


然后配置xml

<?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">
          <!-- 依赖注入 使用构造方法注入(不推荐) -->
          <bean id="student" class="star.july.c_di.Student">
                   <constructor-arg index="0" value="狗娃" type="String"></constructor-arg>
                   <constructor-arg index="1" value="22" type="int"></constructor-arg>
          </bean>
          <!-- 使用setter方法注入(推荐) -->
          <!-- 字符串类型注入 -->
          <bean id="s1" class="star.july.c_di.Student">
                   <property name="stuname" value="徐凤年"></property>
          </bean>
          <!-- 整型注入 -->
          <bean id="s2" class="star.july.c_di.Student">
                   <property name="length">
                             <value>22</value>
                   </property>
          </bean>
          <!-- 数组 -->
          <bean id="s3" class="star.july.c_di.Student">
                   <property name="array">
                             <array>
                                      <value>19</value>
                                      <value>20</value>
                             </array>
                   </property>
          </bean>
          <!-- 集合:List -->
          <bean id="s4" class="star.july.c_di.Student">
                   <property name="list">
                             <list>
                                      <value>小一</value>
                                      <value>小二</value>
                                      <value>小三</value>
                             </list>
                   </property>
          </bean>
          <!-- Map集合 -->
          <bean id="s5" class="star.july.c_di.Student">
                   <property name="map">
                             <map>
                                      <entry key="gd">
                                                <value>广东</value>
                                      </entry>
                                      <entry key="gx">
                                                <value>广西</value>
                                      </entry>
                             </map>
                   </property>
          </bean>
          <!-- Properties注入 -->
          <bean id="s6" class="star.july.c_di.Student">
                   <property name="prop">
                             <props>
                                      <prop key="hn">湖南</prop>
                                      <prop key="fh">凤凰</prop>
                             </props>
                   </property>
          </bean>
          
          <!-- List+JavaBean -->
          <bean id="s7" class="star.july.c_di.Student">
                   <property name="ljb">
                             <list>
                                      <ref bean="ljb1"></ref>
                                      <ref bean="ljb2"></ref>
                             </list>
                   </property>
          </bean>
          
          <bean id="ljb1" class="star.july.c_di.Course">
                   <property name="name"><value>Java开发</value></property>
          </bean>
          
          <bean id="ljb2" class="star.july.c_di.Course">
                   <property name="name"><value>UI设计</value></property>
          </bean>
</beans>

最后测试:
package star.july.c_di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
          public static void main(String[] args) {
                   ApplicationContext ac = new ClassPathXmlApplicationContext("/star/july/c_di/applicationContext.xml");
//                Object s1 = ac.getBean("s1");
//                Object s2 = ac.getBean("s2");
//                Object s3 = ac.getBean("s3");
//                Object s4 = ac.getBean("s4");
//                Object s5 = ac.getBean("s5");
//                Object s6 = ac.getBean("s6");
                   Object s7 = ac.getBean("s7");
                   System.out.println(s7);
                   
          }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值