1.spring的简介
2.spring优点
3.spring的架构体系
4.Core Container(核心容器)
ApplicationContext.xml中配置Bean
<bean id="userInterfaceImpl" class="com.youlin.spring.demo1.UserInterfaceImpl">
<property name="username" value="张三"></property>
</bean>
测试类中进行获取 @Test
public void getUser() throws Exception {
//获取容器
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserInterface bean = (UserInterface) context.getBean("userInterfaceImpl");
bean.sayHello();
}
5.spring的第一大特性IOC
1.容器创建的三种方式
/**
* 容器创建的第一种方式,也是最常用的一种方式
* 使用配置文件的相对路径
* spring 入门案例
* @throws Exception
*/
@Test
public void getUser() throws Exception {
//获取容器
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserInterface bean = (UserInterface) context.getBean("userInterfaceImpl");
bean.sayHello();
}
容器创建的第二种方式 /**
* 容器创建的第二种方式
* 使用配置文件的路径是绝对路径
*/
@Test
public void getFileSystemContainer() throws Exception {
ApplicationContext context = new FileSystemXmlApplicationContext("D:\\workspace01\\springDay01\\src\\main\\resources\\ApplicationContext.xml");
UserInterface bean = (UserInterface) context.getBean("userInterfaceImpl");
bean.sayHello();
}
第三种方式: /**
* 容器创建的第三种方式,已经过时了,不在使用
* 它是一种懒加载形式
* bean已经定义了,不用到的时候是不加载的,使用的时候才会加载
* 上面两种只管创建,不管你用不用
* @throws Exception
*/
@Test
public void getXmlContainer() throws Exception {
Resource resource = new ClassPathResource("ApplicationContext.xml");
XmlBeanFactory beanFactory = new XmlBeanFactory(resource );
UserInterface bean = (UserInterface) beanFactory.getBean("userInterfaceImpl");
bean.sayHello();
}
2.加载对象的三种方式
package com.youlin.spring.demo2;
public class UserBean2Factory {
public static UserBean2 getUserBean2() {
return new UserBean2();
}
}
<!-- 通过静态工厂来创建对象 -->
<bean id="userBean2Factory" class="com.youlin.spring.demo2.UserBean2Factory" factory-method="getUserBean2"></bean>
/**
* 第二种创建对象的方式:通过静态工厂来创建对象
*/
@Test
public void getUserBean2() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserBean2 bean = (UserBean2) context.getBean("userBean2Factory");
bean.userBean2SayHello();
}
package com.youlin.spring.demo2;
public class UserBean3Factory {
public UserBean3 getUserBean3() {
return new UserBean3();
}
}
配置 <!-- 通过实例工厂来创建对象 -->
<bean id="userBean3Factory" class="com.youlin.spring.demo2.UserBean3Factory"></bean>
<bean id="userBean3" class="com.youlin.spring.demo2.UserBean3" factory-bean="userBean3Factory" factory-method="getUserBean3"></bean>
获取 /**
* 第三种创建对象的方式,通过实例工厂来创建对象
* 可以在创建对象之前,加塞一些自己其他的逻辑,在实例工厂内加塞
* @throws Exception
*/
@Test
public void getUserBean3() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserBean3 bean = (UserBean3) context.getBean("userBean3");
bean.sayHello();
}
3.bean的作用域及作用范围以及生命周期
<!-- bean的作用域与作用范围 -->
<bean id="orders" class="com.youlin.spring.demo3.Orders" scope="singleton" init-method="initMethod" destroy-method="destroyMethod"></bean>
/**
* 作用域与作用范围
* @throws Exception
*/
@Test
public void getOrders() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Orders bean = (Orders) context.getBean("orders");
Orders bean2 = (Orders) context.getBean("orders");
System.out.println(bean.toString());
System.out.println(bean2.toString());
context.close();
}
6.Spring的第二大特性 DI Dependency Injection 依赖注入
<!-- 通过构造器来为属性赋值 -->
<bean id="jerry" class="com.youlin.spring.demo4.Jerry">
<!-- 通过constructor-arg来为我们的属性赋值
name 指定哪个属性的名称
-->
<constructor-arg name="name" value="jerry"></constructor-arg>
<constructor-arg name="sex" value="1"></constructor-arg>
<constructor-arg name="color" value="brown"></constructor-arg>
</bean>
<!-- 通过索引来为属性赋值 -->
<bean id="jerry" class="com.youlin.spring.demo4.Jerry">
<constructor-arg index="0" value="jerry2"></constructor-arg>
<constructor-arg index="1" value="0"></constructor-arg>
<constructor-arg index="2" value="黑白色"></constructor-arg>
</bean>
通过P名称空间和C名称空间赋值 <!-- 通过P名称空间来为属性赋值 -->
<bean id="tom" class="com.youlin.spring.demo4.Tom" p:age="10" p:tomName="tom" ></bean>
<!-- 通过C名称空间来为构造器的属性注入 -->
<bean id="jerry4" class="com.youlin.spring.demo4.Jerry" c:color="白色" c:sex="0" c:name="jerry4" ></bean>
<!-- spel表达式 -->
<bean id="user2" class="com.youlin.spring.demo4.User">
<property name="age" value="10"></property>
<property name="username" value="tom"></property>
</bean>
<bean id="tom2" class="com.youlin.spring.demo4.Tom">
<property name="tomAge" value="#{user2.getAge()+20}"></property>
<property name="tomName" value="#{user2.getUsername()+'tomtom'}"></property>
<property name="jerry" value="#{jerry4}"></property>
</bean>