一、技术分析之Spring框架中<bean>标签的配置
1.1. id属性和name属性的区别* id -- Bean起个名字,在约束中采用ID的约束,唯一
* 取值要求:必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号。 id不能出现特殊字符
* name -- Bean起个名字,没有采用ID的约束(了解)
* 取值要求:name-出现特殊字符。如果<bean>没有id的话 , name可以当做id使用
* Spring框架在整合Struts1的框架的时候,Struts1的框架的访问路径是以/开头的,例如:/bookAction
1.2. class属性 -- Bean对象的全路径
1.3. scope属性 -- scope属性代表Bean的作用范围
* singleton-- 单例(默认值)
* prototype -- 多例,在Spring框架整合Struts2框架的时候,Action类也需要交给Spring做管理,配置把Action类配置成多例
* request -- 应用在Web项目中,每次HTTP请求都会创建一个新的Bean
* session -- 应用在Web项目中,同一个HTTP Session 共享一个Bean
* globalsession -- 应用在Web项目中,多服务器间的session
1.4. Bean对象的创建和销毁的两个属性配置(了解)
* 说明:Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法
* init-method -- 当bean被载入到容器的时候调用init-method属性指定的方法
* destroy-method -- 当bean从容器中删除的时候调用destroy-method属性指定的方法
* 想查看destroy-method的效果,有如下条件
* scope= singleton有效
* web容器中会自动调用,但是main函数或测试用例需要手动调用(需要使用ClassPathXmlApplicationContext的close()方法)
案例:
UserService:
public interface UserService {
void initalize();
void sayHello();
void destory();
}
UserServiceImpl:
public class UserServiceImpl implements UserService {
@Override
public void initalize() {
System.out.println("initalize...");
}
@Override
public void sayHello() {
System.out.println("sayHello running...");
}
@Override
public void destory() {
System.out.println("destory...");
}
}
applicationContext.xml:
<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="userService" class="com.ken.demo2.UserServiceImpl"
init-method="initalize" destroy-method="destory" />
</beans>
Demo2:
public class Demo2 {
@Test
public void run2() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
((ClassPathXmlApplicationContext)ac).close();
userService.sayHello();
}
}
二、技术分析之依赖注入(DI)
2.1. IOC和DI的概念IOC -- Inverse of Control,控制反转,将对象的创建权反转给Spring
DI -- Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中
2.2. DI(依赖注入)
例如:如果UserServiceImpl的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注入把该属性的值传入进来
具体的配置如下
<bean id="us" class="com.itheima.demo1.UserServiceImpl">
<property name="uname" value="小风"/>
</bean>
三、技术分析之Spring框架的属性注入
1. 对于类成员变量,常用的注入方式有两种- 构造函数注入
- 属性setter方法注入
2.1 构造方法的注入方式,两步
编写Java的类,提供构造方法
public class Car {
private String name;
private double money;
public Car(String name, double money) {
this.name = name;
this.money = money;
}
@Override
public String toString() {
return "Car [name=" + name + ", money=" + money + "]";
}
}
编写配置文件
<bean id="car" class="com.itheima.demo4.Car">
<constructor-arg name="name" value="大奔"/>
<constructor-arg name="money" value="100"/>
</bean>
2. 属性的setter方法的注入方式
编写Java的类,提供属性和对应的set方法即可
编写配置文件
3. 如果Java类的属性是另一个Java的类,那么需要怎么来注入值呢?
<property name="name" rel="具体的Bean的ID或者name的值"/>
例如:
<bean id="person" class="com.itheima.demo4.Person">
<property name="pname" value="美美"/>
<property name="car2" ref="car2"/>
</bean>
四、技术分析之Spring的2.5版本中提供了一种:p名称空间的注入(了解)
4.1. 步骤一:需要先引入 p 名称空间在schema的名称空间中加入该行:xmlns:p="http://www.springframework.org/schema/p"
4.2. 步骤二:使用p名称空间的语法
p:属性名 = ""
p:属性名-ref = ""
4.3. 步骤三:测试
<bean id="person" class="com.itheima.demo4.Person" p:pname="老王" p:car2-ref="car2"/>
五、技术分析之Spring的3.0提供了一种:SpEL注入方式(了解)
5.1. SpEL:Spring Expression Language是Spring的表达式语言,有一些自己的语法5.2. 语法
* #{SpEL}
5.3. 例如如下的代码
<!-- SpEL的方式 -->
<bean id="person" class="com.itheima.demo4.Person">
<property name="pname" value="#{'小风'}"/>
<property name="car2" value="#{car2}"/>
</bean>
5.4. 还支持调用类中的属性或者方法
定义类和方法,例如
public class CarInfo {
public String getCarname(){
return "奇瑞QQ";
}
}
六、技术分析之数组,集合(List,Set,Map),Properties等的注入
6.1. 如果是数组或者List集合,注入配置文件的方式是一样的
<bean id="collectionBean" class="com.itheima.demo5.CollectionBean">
<property name="arrs">
<list>
<value>美美</value>
<value>小风</value>
</list>
</property>
</bean>
6.2. 如果是Set集合,注入的配置文件方式如下:
<property name="sets">
<set>
<value>哈哈</value>
<value>呵呵</value>
</set>
</property>
6.3. 如果是Map集合,注入的配置方式如下:
<property name="map">
<map>
<entry key="老王2" value="38"/>
<entry key="凤姐" value="38"/>
<entry key="如花" value="29"/>
</map>
</property>
6.4. 如果是properties属性文件的方式,注入的配置如下:
<property name="pro">
<props>
<prop key="uname">root</prop>
<prop key="pass">123</prop>
</props>
</property>
七、技术分析之Spring框架的配置文件分开管理(了解)
例如:在src的目录下又多创建了一个配置文件,现在是两个核心的配置文件,那么加载这两个配置文件的方式有两种!7.1 主配置文件中包含其他的配置文件:
<import resource="applicationContext2.xml"/>
7.2 工厂创建的时候直接加载多个配置文件:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
八、Spring框架整合WEB(不是最终的方案)
8.1. 创建JavaWEB项目,引入Spring的开发包。编写具体的类和方法。
最简单的IOC使用时在Controller层中,通过读取applicationContext.xml来创建spring容器,从而获得bean。例如:
public class CustomerAction extends ActionSupport{
/**
* 保存客户
* @return
*/
public String save(){
System.out.println("WEB层:保存客户...");
// 使用工厂
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService cs = (CustomerService) ac.getBean("customerService");
cs.save();
return NONE;
}
}
这会带来一个问题,
每访问一次save()方法都会加载一次配置文件,这样效率会非常非常慢!!8.2. 解决上面的问题
将工厂创建好了以后放入到ServletContext域中。使用工厂的时候,从ServletContext中获得。
ServletContextListener:用来监听ServletContext对象的创建和销毁的监听器。
当ServletContext对象创建的时候:创建工厂 , 将工厂存入到ServletContext。
8.3. Spring整合Web项目
思想:启动服务器,ServletContext对象就会被创建。我们监听ServletContext对象的创建,然后,在这个监听器的方法中加载applicationContext.xml。我们需要的监听器就在整合包spring-web-4.2.4.RELEASE.jar包中。
* 引入spring-web-4.2.4.RELEASE.jar包
有了jar包,就说明有一些类已经准备好了
配置监听器
<!-- 配置web整合的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- ContextLoaderListener默认只能加载WEB-INF目录下的配置文件,提供配置方式,加载src目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
ContextLoaderListner默认是去加载WEB-INF下的配置文件。那我们就配置一个全局的变量,用来存放spring的配置文件。
8.4. 修改servlet的代码
从ServletContext中获得工厂具体代码如下
/**
* 客户的Action
* @author Administrator
*/
public class CustomerAction extends ActionSupport{
private static final long serialVersionUID = 113695314694166436L;
/**
* 保存客户
* @return
*/
public String save(){
System.out.println("WEB层:保存客户...");
// 使用工厂
/*ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService cs = (CustomerService) ac.getBean("customerService");
cs.save();*/
ServletContext servletContext = ServletActionContext.getServletContext();
// 需要使用WEB的工厂的方式
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
CustomerService cs = (CustomerService) context.getBean("customerService");
cs.save();
return NONE;
}
}
service注入dao,就在xml中配置好了。
源码下载