• spring学习笔记


    一:spring入门

    1:新建java project,在项目名字下面建立lib,spring的jar包下载http://repo.spring.io/release/org/springframework/spring/

    http://repo.spring.io/release/org/springframework/spring/4.0.0.RELEASE/

    2:在applicationContext.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="SpringHello" class="com.wth.cn.SpringHello">(注意此处为类全名,是通过反射的方式创建对象的,所以必须在该类(SpringHello)中要有无参构造方法)

        <property name="name2" value="小猫"></property>
      </bean>
    </beans>

    3:在Main类中写

    public static void main(String[] args){
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    SpringHello springHello = (SpringHello)ctx.getBean("SpringHello");
    System.out.println(springHello.getName2());
    // SpringHello springHello=new SpringHello();
    // springHello.setName("小狗");
    // System.out.println(springHello.getName());
    }

    二:IOC

    1:IOC容器作用

     

    三:用spring创建对象的方式

    1:通过new 对象名字的方式即在application.xml

    中配置

    <bean  id="employer" class="com.wth.cn.Employer">
      <property name="name" value="慧"></property>
      <property name="sex" value="女"></property>
      <property name="salary" value="10000"></property>

       //对存在特殊符号数据的赋值

      <property name="address">
        <value> <![CDATA[<henan^>]]></value>
      </property>

      //对list对象赋值

      <property name="internships">  

        <list>
          <bean class="com.wth.cn.Internships">
            <property name="name" value="王慧"></property>
            <property name="sex" value="女"></property>
            <property name="salary" value="1000"></property>
          </bean>
          <ref bean="internship"/>
        </list>

      </property>

    //为map赋值

    <property name="carInfo">
      <map>
        <entry key="AA" value-ref="car1"></entry>
        <entry key="BB" value-ref="car2"></entry>
      </map>
    </property>

    //为properties属性赋值

    <bean id="dataSource" class="com.wth.cn.DataSource">
     <property name="properties">
      <props>
        <prop key="name">张三</prop>
        <prop key="score">98</prop>
        <prop key="dept">软件学院</prop>
      </props>
    </property>
    </bean>

    //传统的方式不能将list转为bean,但util命名空间可以实现,如此别的属性也可引用该cars属性。但是 util需要添加util命名空间文件,

    xmlns:util="http://www.springframework.org/schema/util"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <util:list id="cars">
       <ref bean="car1"/>
       <ref bean="car2"/>
    </util:list>

    为了方便赋值,可以用p,需要引入p的命名空间文件     xmlns:p="http://www.springframework.org/schema/p"

    <bean id=“internetship” p:city="beijing" p:street="鼓楼大街">

    </bean>

    //为了引入外界文件,需要引入context的命名空间

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.0.xsd 

    xmlns:context="http://www.springframework.org/schema/context"

           <context:property-placeholder location="db.properties"/>

    //为级联属性赋值(注意属性需先初始化后才可以为级联属性赋值,否则会有异常,和structs2不同)

    <property name="cadet" ref="internship"></property>
    <property name="internship.name" value="刘校庆"></property>

    </bean>

    //bean的后置处理器

    https://www.cnblogs.com/sishang/p/6576665.html

    2:通过构造方法来创建对象(可用该种方式设置null)

    <bean  id="boss" class="com.wth.cn.Boss">
      <constructor-arg > <null/></constructor-arg>

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

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

        <constructor-arg type="java.lang.String">

        <value> <![CDATA[<henan^>]]></value>

     </constructor-arg>

      <constructor-arg type="int">

        <value> 25</value>

      </constructor-arg>

    //为级联属性赋值(注意属性需先初始化后才可以为级联属性赋值,否则会有异常,和structs2不同)

    <constructor-arg ref="car"></constructor-arg>

    <property name="car.name" value="宝马"></property>

    </bean>

    3:通过静态工厂方式创建对象

    我的总结:实际上就是在一个类A中创建一个static方法,该static方法·可以获取一个对象B,可以通过p:方式给B对象赋值,也可以通过property方式直接配置属性的值,还可以通过

    <constructor-arg value="nv"></constructor-arg>方式给static方法传入参数。

    class属性:指向静态工厂方法的全类名

    factory-method:指向静态工厂方法的名字

    constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数。

    <!-- 静态工厂获取bean -->
    <bean id="person"
      class="com.wth.ceateBean.StaticCarfactory"
      factory-method="getPerson" >//调用这个静态方法可以直接
    <!-- <constructor-arg value="nv"></constructor-arg> -->
    <property name="name" value="张三"></property>
    </bean>

    4:实例方法创建对象

    <bean id="instance" class="com.wth.ceateBean.InstanceCarFactory"></bean>
    <bean id="person1"
          class="com.wth.ceateBean.InstanceCarFactory"
        factory-method="getP"
        factory-bean="instance"
    >
    <constructor-arg value="地下" ></constructor-arg>
    </bean>

    5:aop的使用https://www.cnblogs.com/qinglangyijiu/p/8425653.html

  • 相关阅读:
    open_basedir restriction in effect的错误及其解决办法
    SNMP-网络管理协议
    安装cacti监控系统
    并发时-修改Linux系统下的最大文件描述符限制
    js new date()说明
    阿里云ECS环境部署 centos 6.5
    sysbench
    http_load
    LeetCode: Spiral Matrix
    LeetCode:Length of Last Word
  • 原文地址:https://www.cnblogs.com/wth21-1314/p/10327876.html
Copyright © 2020-2023  润新知