Spring框架--环境搭建

1.下载spring包

地址:
跳转地址
https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring/4.3.3.RELEASE
选择需要下载的版本,按照图示复制下载。
在这里插入图片描述

在这里插入图片描述
复制需要的包到框架下,其中commons-logging需要再下载。
在这里插入图片描述

2.编写测试类

package com.atguigu.spring.beans;
public class HelloWorld {
 private String name;
 public void setName(String name) {
  this.name=name;
 }
 public void hello() {
  System.out.println("hello:"+name);
  }
  //必须有一个无参的构造器
 public HelloWorld() {
  super();
  System.out.println("(HelloWorld)");
 }
}

配置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="helloWorld" class="com.atguigu.spring.beans.HelloWorld">
  <property name="name" value="Spring"></property>
 </bean>
</beans>

3.Spring配置

Spring容器

BeanFactory

IOC 容器的基本实现.
BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身

ApplicationContext

1.提供了更多的高级特性. 是 BeanFactory 的子接口.
2.ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
3.ApplicationContext在初始化上下文时就实例化所有单例的Bean。

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld=(HelloWorld)ctx.getBean("helloWorld");
//HelloWorld helloWorld=(HelloWorld)ctx.getBean("helloWorld");
//需要配置文件里的bean为唯一
ApplicationContext的主要实现类

在这里插入图片描述

ConfigurableApplicationContext

ConfigurableApplicationContext扩展于ApplicationContext,新增加两个主要方法:refresh()和close(),让ApplicationContext具有启动、刷新和关闭上下文的能力。
1.ClassPathXmlApplicationContext:从类路径下加载配置文件
2.FileSystemXmlApplicationContext:从文件系统中加载配置文件

WebApplicationContext

WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作。

4.依赖注入

依赖注入的方式

属性注入

1.属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象
2.属性注入使用 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 子节点指定属性值
3.属性注入是实际应用中最常用的注入方式

<bean id="helloWorld" class="com.atguigu.spring.beans.HelloWorld">
 <property name="name" value="Spring"></property>
</bean>

构造器注入

1.通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。
2.构造器注入在 元素里声明属性, 中没有 name 属性

package com.atguigu.spring.beans;
public class Car {
 private String brand;
 private String corp;
 private int price;
 private double maxSpeed;
 public Car(String brand, String corp, double maxSpeed) {
  super();
  this.brand = brand;
  this.corp = corp;
  this.maxSpeed = maxSpeed;
 }
 public Car(String brand, String corp, int price) {
  super();
  this.brand = brand;
  this.corp = corp;
  this.price = price;
 }
 @Override
 public String toString() {
  return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
 } 
}
<?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="car" class="com.atguigu.spring.beans.Car">
  <constructor-arg value="Audi" index="0"></constructor-arg>
  <constructor-arg value="ShangHai" index="1"></constructor-arg>
  <constructor-arg value="3000" type="int"></constructor-arg>
 </bean>
</beans>

工厂方法注入(很少使用,不推荐)

字面值

1.字面值:可用字符串表示的值,可以通过 元素标签或 value 属性进行注入。
2.基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
3.若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来。

<bean id="car" class="com.atguigu.spring.beans.Car">
  <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
  <constructor-arg type="java.lang.String">
   <value><![CDATA[<ShangHai^>]]></value>
  </constructor-arg>
  <constructor-arg type="int">
   <value>250</value>
  </constructor-arg>
 </bean>

引用其它Bean

1.组成应用程序的Bean经常需要相互协作以完成应用程序的功能.要使Bean能够相互访问,就必须在Bean配置文件中指定对Bean的引用
2.在Bean的配置文件中,可以通过元素或ref属性为Bean的属性或构造器参数指定对Bean的引用.

package com.atguigu.spring.beans;
public class Person {
 private String name;
 private int age;
 private Car car;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public Car getCar() {
  return car;
 }
 public void setCar(Car car) {
  this.car = car;
 }
 @Override
 public String toString() {
  return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
 }
}
<bean id="Person" class="com.atguigu.spring.beans.Person">
  <property name="name" value="Tom"></property>
  <property name="age" value="24"></property>
  <property name="car" ref="car"></property>
  <!-- 
  <property name="car">
   <ref="car"/>
  </property> 
  -->
 </bean>

内部Bean

1.也可以在属性或构造器里包含Bean的声明,这样的Bean称为内部Bean
2.当 Bean 实例仅仅给一个特定的属性使用时, 可以将其声明为内部 Bean. 内部 Bean 声明直接包含在 或 元素里, 不需要设置任何 id 或 name 属性
3.内部 Bean 不能使用在任何其他地方

<bean id="person2" class="com.atguigu.spring.beans.Person">
 <property name="name" value="Tom"></property>
 <property name="age" value="24"></property>
 <property name="car">
  <bean class="com.atguigu.spring.beans.Car">
   <constructor-arg value="Ford"></constructor-arg>
   <constructor-arg value="Changan"></constructor-arg>
   <constructor-arg value="200000" type="double"></constructor-arg>
  </bean>
 </property>
</bean>
<bean id="person3" class="com.atguigu.spring.beans.Person">
  <constructor-arg value="Jerry"></constructor-arg>
  <constructor-arg value="25"></constructor-arg>
  <constructor-arg ref="car"></constructor-arg>
 </bean>

注入参数详解:null 值和级联属性

1.可以使用专用的 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值

<bean id="person4" class="com.atguigu.spring.beans.Person">
 <constructor-arg value="Jerry"></constructor-arg>
 <constructor-arg value="25"></constructor-arg>
 <constructor-arg><null/></constructor-arg>
</bean>

2.和 Struts、Hiberante 等框架一样,Spring 支持级联属性的配置。
注意:属性需要先初始化才可以为级联属性赋值,否则会有异常

<bean id="person5" class="com.atguigu.spring.beans.Person">
 <constructor-arg value="Jerry"></constructor-arg>
 <constructor-arg value="25"></constructor-arg>
 <constructor-arg ref="car"></constructor-arg>
 <property name="car.maxSpeed" value="250"></property>
</bean>

集合属性

1.在 Spring中可以通过一组内置的 xml 标签(例如: , 或 ) 来配置集合属性.
2.配置 java.util.List 类型的属性, 需要指定 标签, 在标签里包含一些元素. 这些标签可以通过 指定简单的常量值, 通过 指定对其他 Bean 的引用. 通过 指定内置 Bean 定义. 通过 指定空元素. 甚至可以内嵌其他集合.
3.数组的定义和 List 一样, 都使用
4.配置 java.util.Set 需要使用 标签, 定义元素的方法与 List 一样.

public class Person {
 private String name;
 private int age;
 private List<Car> cars;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public List<Car> getCars() {
  return cars;
 }
 public void setCars(List<Car> cars) {
  this.cars = cars;
 }
 public Person(String name, int age, List<Car> cars) {
  super();
  this.name = name;
  this.age = age;
  this.cars = cars;
 }
 public Person() {
  super();
 }
 @Override
 public String toString() {
  return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
 }
}
<bean id="person6" class="com.atguigu.spring.beans.collection.Person">
  <property name="name" value="Mike"></property>
  <property name="age" value="27"></property>
  <property name="cars">
   <list>
    <ref bean="car"/>
    <ref bean="car"/>
    <bean class="com.atguigu.spring.beans.Car">
    <constructor-arg value="Ford"></constructor-arg>
    <constructor-arg value="Changan"></constructor-arg>
    <constructor-arg value="200000" type="double"></constructor-arg>
   </bean>
   </list>
  </property>
 </bean>

集合属性Map

1.Java.util.Map 通过 标签定义, 标签里可以使用多个 作为子标签. 每个条目包含一个键和一个值.
2.必须在 标签里定义键
3.因为键和值的类型没有限制, 所以可以自由地为它们指定 , , 或 元素.
4.可以将 Map 的键和值作为 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义
5.使用 定义 java.util.Properties, 该标签使用多个 作为子标签. 每个 标签必须定义 key 属性.

<bean id="newPerson" class="com.atguigu.spring.beans.collection.Person2">
 <property name="name" value="Rose"></property>
 <property name="age" value="28"></property>
 <property name="cars">
  <map>
   <entry key="AA" value-ref="car"></entry>
   <entry key="BB" value-ref="car"></entry>
  </map>
 </property>
</bean>
public class DataSource {//其他具体方法省略
 private Properties properties;}
<bean id="dataSource" class="com.atguigu.spring.beans.collection.DataSource">
 <property name="properties">
  <props>
   <prop key="user">root</prop>
   <prop key="password">1234</prop>
   <prop key="jdbcUrl">jdbc:mysql:///test</prop>
   <prop key="driverClass">com.mysql.jdbc.Driver</prop>
  </props>
 </property>
</bean>

使用 utility scheme 定义集合

1.使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合.
2.可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 根元素里添加 util schema 定义

<util:list id="cars">
 <ref bean="car"></ref>
 <bean class="com.atguigu.spring.beans.Car">
  <constructor-arg value="Ford"></constructor-arg>
  <constructor-arg value="Changan"></constructor-arg>
  <constructor-arg value="200000" type="double"></constructor-arg>
 </bean>
</util:list>
<bean id="person7" class="com.atguigu.spring.beans.collection.Person">
 <property name="name" value="Mike"></property>
 <property name="age" value="27"></property>
 <property name="cars" ref="cars"></property>
</bean>

使用 p 命名空间

1.为了简化 XML 文件的配置,越来越多的 XML 文件采用属性而非子元素配置信息。
2.Spring 从 2.5 版本开始引入了一个新的 p 命名空间,可以通过 元素属性的方式配置 Bean 的属性。
3.使用 p 命名空间后,基于 XML 的配置方式将进一步简化

<bean id="person8" class="com.atguigu.spring.beans.collection.Person" p:age="30" p:name="Queen"
  p:cars-ref="cars"></bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值