博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring学习笔记
阅读量:6928 次
发布时间:2019-06-27

本文共 4554 字,大约阅读时间需要 15 分钟。

一:spring入门

1:新建java project,在项目名字下面建立lib,spring的jar包下载

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="s">  

    <list>

      <bean class="com.wth.cn.Is">
        <property name="name" value="王慧"></property>
        <property name="sex" value="女"></property>
        <property name="salary" value="1000"></property>
      </bean>
      <ref bean=""/>
    </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=""></property>

<property name=".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="ar.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

转载于:https://www.cnblogs.com/wth21-1314/p/10327876.html

你可能感兴趣的文章
ArrayList与List对象用法与区别
查看>>
C++ 排序函数 sort(),qsort()的使用方法
查看>>
[备忘]Redis运行出现Client sent AUTH, but no password is set
查看>>
函数传递一维数组
查看>>
MATLAB中-27开3次方得不到-3的原因
查看>>
一个格式化字符串的函数ToString
查看>>
I/O: std::ios_base::openmode
查看>>
Web安全测试工具小集
查看>>
使用Swift模拟Window-LFU
查看>>
ElasticSearch无法启动
查看>>
mysql explain 的type解释
查看>>
jQuery遍历方式
查看>>
C++环境编译使用sqlite数据库全过程
查看>>
anaconda不错的
查看>>
大数据之 ZooKeeper原理及其在Hadoop和HBase中的应用
查看>>
vs配置D3D开发环境
查看>>
JS函数(自调函数)与闭包【高级函数】
查看>>
mysql decimal(10,2)对应java类型
查看>>
技术领导(Technical Leader)画像
查看>>
HTTP 用户认证
查看>>