关闭

Spring 依赖注入(DI)

标签: Spring依赖注入DISpringDI
313人阅读 评论(0) 收藏 举报
分类:
依赖注入,可注入的类型有:
字符串/整数/小树/布尔值
数组
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);
                   
          }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:101544次
    • 积分:2266
    • 等级:
    • 排名:第18373名
    • 原创:122篇
    • 转载:0篇
    • 译文:0篇
    • 评论:33条
    文章分类
    最新评论