Spring框架学习第一天

1.Spring框架

  Spring框架是一站式框架正是因为Spring框架性质是属于容器性质的

  容器中装什么对象就有什么功能,Spring不光不排斥其他框架,还帮助其他框架管理对象

  还对AOP支持

2.导包

      

3.HelloWord

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  <bean name="user" class="cn.xiaou.domain.User" ></bean>
<beans>

demo.java

public void test1() {
        //创建Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //从容器中取出名为user的对象
        User user = (User) context.getBean("user");
        //User user2 = (User) context.getBean("user");
        //System.out.println(user == user2);
        System.out.println(user);
    }

4.Spring配置文件

<!--
        name:取名字,用于后续的取出本对象,允许使用特殊符号
        class:全包名 
        scope:默认是单例模式(singleton) 被标识单例对象在Spring容器中只会存在一个实例
                    多例模式(prototype) 被标志的多例的对象每次的获得都会获得一个新的对象,
                                        而且只有在获得的时候才会被创建
     -->
    <bean name="user" class="cn.xiaou.domain.User" ></bean>
    <bean name="user2" class="cn.xiaou.domain.User">
        <property name="name" value="zjj"/>
        <property name="age" value="18"/>
    </bean>
    <bean name="user3" class="cn.xiaou.domain.User">
        <property name="name" value="zjj"/>
        <property name="age" value="18"/>
        <!-- 通过ref来关联name="car" -->
        <property name="car" ref="car"/>
    </bean>
    <bean name="car" class="cn.xiaou.domain.Car">
        <property name="carName" value="啦啦"/>
        <property name="color" value="红色"/>
    </bean>
<!-- 导入其他的Spring配置文件 -->
    <import resource="cn/xiaou/demo2/ApplicationContext.xml"/>

5.Spring属性注入的方式

<!-- set注入 -->
    <bean name="user2" class="cn.xiaou.domain.User">
        <property name="name" value="zjj"/>
        <property name="age" value="18"/>
    </bean>
<!-- 构造方法注入 -->
    <!--
        name:构造参数的参数名
        index:构造参数的的索引
        type:构造函数的参数类型
        值类型使用的是value
        对象类型用的ref
     -->
    <bean name="user5" class="cn.xiaou.domain.User">
        <constructor-arg name="name" index="0" value="zjj"/>
        <constructor-arg name = "age" index = "1" value="18"/>
        <constructor-arg name = "car" index = "2" ref="car" type="cn.xiaou.domain.Car"></constructor-arg>
    </bean>
<!--
        p名称空间注入,使用bean类的set方法
        p:属性名="值"
        对象类型:p:属性名-ref="bean名称" 
     -->
    <bean name="user4" class="cn.xiaou.domain.User" p:name="zjj" p:age = "3" p:car-ref="car"/>

复杂类型注入

 1 <!-- 复杂类型对值的注入 -->
 2         <bean name="person1" class="cn.xiaou.domain.Person">
 3             <property name="hobby">
 4                 <array>
 5                     <value>OW</value>
 6                     <value>xjh</value>
 7                 </array>
 8             </property>
 9             <property name="set">
10                 <set>
11                     <value>lala</value>
12                     <value>aaaa</value>
13                 </set>
14             </property>
15             <property name="map">
16                 <map>
17                     <entry key="url" value="www.baidu.com"/>
18                     <entry key="key" value="xjh"/>
19                 </map>
20             </property>
21             <property name="properties">
22                 <props>
23                     <prop key="username">root</prop>
24                     <prop key="password">root</prop>
25                 </props>
26             </property>
27         </bean>    
复杂类型对值的注入
 1 <!-- 复杂类型对象的注入 -->
 2         <bean name="person2" class="cn.xiaou.domain.Person">
 3             <property name="hobby">
 4                 <array>
 5                     <value>OW</value>
 6                     <value>xjh</value>
 7                 </array>
 8             </property>
 9             <property name="set">
10                 <set>
11                     <value>lala</value>
12                     <value>aaaa</value>
13                 </set>
14             </property>
15             <property name="map">
16                 <map>
17                     <entry key="user" value-ref="user2"/>
18                     <entry key-ref="user3" value-ref="car" />
19                 </map>
20             </property>
21             <property name="properties">
22                 <props>
23                     <prop key="username">root</prop>
24                     <prop key="password">root</prop>
25                 </props>
26             </property>
27         </bean>    
复杂类型对象的注入

6.将ApplicationContext容器绑定到web.xml中让容器随Tomcat的启动而创建

在xml中配置监听器

 <!-- 配置让Spring容器随着项目的启动而启动,随项目的销毁而销毁 ,配置的监听器-->
<listener>
    <listener-class>
            org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>    
<!-- 指定加载Spring容器的参数 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

在Action中获得容器

//1.获得serviceContext对象
    ServletContext sc = ServletActionContext.getServletContext();
    System.out.println(sc);
//2.通过sc中获得ApplicationContext容器
    WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
    System.out.println(ac);
//3.从容器中取出对象
    CustomerService service =(CustomerService) ac.getBean("customerService");        

猜你喜欢

转载自www.cnblogs.com/FlyBlueSky/p/9185349.html