Spring框架

关于Spring

提供对象容器。

1.Spring的概述?

 

2.Spring框架的获取?

  通过maven来获取。

3.开发spring的程序?

Spring的注入(所谓的注入就是给对象加入数据。)

 1开发spring程序,主要是通过spring来获取对象。

               

实现的步骤:

1)在项目中引入spring的相应的jar包:

2)编写实体类:

public class Girl {

    public String getGirlname() {

return girlname;

}

public void setGirlname(String girlname) {

this.girlname = girlname;

}

private String girlname;

}

3)在spring的配置文件里,applicationContext文件里做bean对象的注册:

<!-- 将对象注册到spring的容器里 -->

    <bean id="girl1" class="com.jinglin.spring.bean.Girl">

         <property name="girlname">

            <value>凤姐</value>

         </property>

    </bean>

4)在测试类编写通过spring来获取刚刚注册的这个对象:

//首先要获取spring的配置

ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});

Girl girl1 =   (Girl) ctx.getBean("girl1");

System.out.println("这女生的姓名:"+girl1.getGirlname());

4.Spring的注入问题?

 所谓的注入,就是在spring中如何注册对象(构建对象的方式)。

 1)属性的注入。也就是在构建对象的时候加入属性值。

<bean id="girl1" class="com.jinglin.spring.bean.Girl">

         <property name="girlname">

            <value>凤姐</value>

         </property>

    </bean>

2)构造器注入,构造方法的注入。

<bean id="girl2" class="com.jinglin.spring.bean.Girl">

        <constructor-arg type="java.lang.String" value="范冰冰"></constructor-arg>

    </bean>

3)如果属性是集合,那么数据如何注入。

<bean id="girl3" class="com.jinglin.spring.bean.Girl">

         <property name="girlname">

            <value>张白痴</value>

         </property>

         <property name="habbies">

            <list>

                <value>吃饭</value>

                <value>睡觉</value>

            </list>

         </property>

         <!-- 键值对集合的注入 -->

         <property name="score">

             <map>

                <entry>

                   <!-- 这是一个键 -->

                   <key>

                      <value>数学</value>

                   </key>

                   <!-- 这是一个值 -->

                   <value>78</value>

                </entry>

                <entry>

                    <key>

                      <value>英语</value>

                    </key>

                    <value>92</value>

                </entry>

             </map>

         </property>

    </bean>

4)引用其他bean

<bean id="boy1" class="com.jinglin.spring.bean.Boy">

        <property name="name">

          <value>张三</value>

        </property>

        <!-- bean中引用其它的bean -->

        <property name="girl" ref="girl3"></property>

    </bean>

5.bean的作用域?

 1)如果没有设置beanscope属性,这个bean的作用域是属于共享实例的,所谓的共享实例指的是所有的bean对象都引用的是同一个实例对象。默认的beanscope值为singleton

2)非共享实例,也就是每次在产生的bean对象都是不一样的。

 scope的值为prototype

6.自动装配

  属性中的名字和bean的名字一样的时候,那么就自动注入数据。

<!-- 自动装配的示例,autowire:byName,通过名字自动装配,也就是husband里有一个wife的属性名。

     自动装配的意思就是需要去找wifebeanid名字,如果找到了就自动把bean的数据给这个属性-->

    <bean id="husband" class="com.jinglin.bean.Husband" autowire="byName">

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

         <!-- <property name="wife" ref="wife"></property> -->

    </bean>

    <bean id="wife" class="com.jinglin.bean.Wife">

         <property name="name" value="凤姐"></property>

    </bean>

原文地址:https://www.cnblogs.com/bwcx1375/p/7594421.html