spring初始

开发工具:

我们可以再Idea intelliJ中开发、可以在myeclipse中增加spring插件,便于开发spring tool。

也可以直接下载基于eclipse的开发软件https://spring.io/tools

   

Spring 的基础开发环境:

所需要的spring jar包:bean,context,core,express,aop

第三方jar包: commons-loging

Spring的IOC(超级工厂)

控制反转也称之为DI,无论需要什么对象,直接去spring里去拿。

   

1、向往spring里放对象

   

  1. <!-- 此行为等同于 Student stu = new Student();下面为配置参数 -->
  2. <bean id="stu" class="cn.tri.vo.Student">
  3.       <property name="id" value="2"></property>
  4.       <property name="name" value="zhaosi"></property>
  5.         <property name="age" value="23"></property>
  6.   </bean>

      

   

2、有需要再去拿

   

  1.  //通过spring的IOC容器配置获取对象
  2.  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

       

  3.  //此处获取一个id="stu"的对象,不需要重新获取,创建
  4.  Student stu1 = (Student) context.getBean("stu");
  5.  System.out.println(stu1);

   

三种依赖注入方式:

   

1、property 必须有属性的set方法

2、contructor 必须有类有参的构造器 字符串优先

3、p标签 必须有类无参的构造器

   

<bean id="tea" class="cn.tri.vo.Teacher" p:name="yefan" p:age="18"><!--

1、 利用property注入 ,实际调用的是set方法,通过反射实现

<property name="name" value="张三"></property>

<property name="age" value="20"></property> -->

   

<!--2、利用Contructor,构造器。 可以由name\index控制对应关系,若不配置,则会按照构造器参数顺序

<constructor-arg value="30" name="age"></constructor-arg>

<constructor-arg value="lisi" name="name"></constructor-arg> -->

</bean>

   

<bean id="stu" class="cn.tri.vo.Student" p:id="6" p:name="liming" p:age="8" p:teacher-ref="tea">

<!-- 此行等同于 Student stu = new Student();下面为配置参数 -->

<!-- <property name="id" value="2"></property>

<property name="name" value="zhaosi"></property>

<property name="age" value="23"></property>

//DI注入teacher依赖 ref=""引用id

<property name="teacher" ref="tea"></property> -->

   

<!-- <constructor-arg ref="tea"></constructor-arg>

<constructor-arg value="3"></constructor-arg>

<constructor-arg value="xiaoer"></constructor-arg>

<constructor-arg value="10"></constructor-arg> -->

</bean>

-非基本类型的依赖注入

   

  • <--  赋空值:-->
  • <property name="strs">
  •     <value></value>  //""
  •     <null/> //null
  • </property>
  • <bean id="typeCollection" class="cn.tri.vo.TypeCollection">
  •         <!-- 数组注入 -->
  • <property name="strs">
  •             <array>
  •                 <value>足球arra</value>
  •                 <value>篮球arra</value>
  •                 <value>乒乓球arra</value>
  •             </array>
  •         </property>

       

  •         <!-- List注入 -->
  •         <property name="list">
  •             <list>
  •                 <value>足球list</value>
  •                 <value>篮球list</value>
  •                 <value>乒乓球list</value>
  •             </list>
  •         </property>
  •         
  • <!-- Set注入 -->
  •         <property name="setString">
  •             <set>
  •                 <value>足球setString</value>
  •                 <value>篮球setString</value>
  •                 <value>乒乓球setString</value>
  •             </set>
  •         </property>
  •         
  • <!-- Map注入 -->
  •         <property name="map">
  •             <map>
  •                 <entry>
  •                     <key>
  •                         <value>1</value>
  •                     </key>
  •                     <value>天上</value>
  •                 </entry>
  •                 
  •                 <entry>
  •                     <key>
  •                         <value>2</value>
  •                     </key>
  •                     <value>地下</value>
  •                 </entry>
  •             </map>
  •         </property>
  •         <!-- properties注入 -->
  •         <property name="props">
  •             <props>
  •                 <prop key="1">
  •                     上天入地
  •                 </prop>
  •                 
  •                 <prop key="2">
  •                     入地上天
  •                 </prop>
  •             </props>
  •         </property>
  •     </bean>

   

posted @ 2020-10-17 16:05  黑质白章  阅读(82)  评论(0编辑  收藏  举报