一、Spring框架了解
Spring框架是一个开源的框架,为JavaEE应用提供多方面的解决方案,用于简化企业级应用的开发,相当于是一种容器,可以集成其他框架(结构图如下)。
上图反映了框架引包的依赖关系(例如:DAO的jar包依赖AOP的jar包,AOP的jar包依赖Core的jar包)
上图也反映了Spring功能有:
IOC:控制反转,Spring的核心功能
AOP:面向切面编程
Web:MVC结构实现、与其他Web技术融合
DAO:与JDBC整合和事务管理
ORM:与ORM对象映射实现的框架整合
JEE:与JavaEE服务整合
二、SpringIOC(Inversion Of Control)
SpringIOC控制反转,是指程序中对象的获取方式发生反转,由最初的new方式创建,转为由框架创建注入,这样可以降低对象之间的耦合度。
IOC的主要作用是管理程序的组件,创建组件对象和维护对象之间的关系。
Spring容器:在Spring中,任何的Java类都被当成Bean组件,通过容器管理和使用,Spring容器实现了IOC和AOP机制,
Spring容器有ApplicationContext和BeanFactory两种类型。
ApplicationContext继承自BeanFactory,提供了更多的方法,建议使用ApplicationContext。
ApplicationContext实例化途径:
1.从classpath下加载配置文件实例化:ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
2.从文件系统中加载配置文件实例化:ApplicationContext ac = new FileSystemXmlApplicationContext("D:\applicationContext.xml");
Bean对象的创建:
Spring容器创建Bean对象有三种方法:
1.使用构造器来实例化
2.使用静态工厂方法来实例化
3.使用动态(实例)工厂方法来实例化
** Spring容器创建Bean对象的步骤:**
第一步:导入SpringIOC相关包到WebRoot/WBE-INF/lib下
commons-logging-1.2.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
第二步: 编写实体类和实体工厂类
实体类:
package com.springioc.entity;
public class User {
private Integer id;
private String username;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + "]";
}
}
实体工厂类:
package com.springioc.entity;
public class UserFactory {
public static User CreateUser(){
return new User();
}
public User DynamicCreateUser(){
return new User();
}
}
第三步: 编写实例化配置文件applicationContext.xml
<!-- 在容器配置文件applicationContext中添加bean的定义,等价于将Bean组件放入Spring容器,后续由Spring负责创建对象 -->
<!-- 在容器中最基本的单位就是bean标签,一个bean标签代表一个对象 -->
<!-- 默认情况下,每一个bean都是单例的,在单例(singleton)情况下,随容器的创建而创建,随着容器的销毁而销毁 -->
<!-- Bean创建对象的三种方式 -->
<!-- 1.构造器
语法格式:
<bean id="对象标识符" class="对象权限定名"></bean>
举例:
<bean id="user" class="com.springioc.entity.User"></bean>
-->
<!-- 2.静态工厂
语法格式:
<bean id="静态工厂标识符" class="工厂权限定名" factory-method="静态方法名"></bean>
举例:
<bean id="staticFactory" class="com.springioc.entity.UserFactory" factory-method="CreateUser"></bean>
-->
<!-- 3.动态(实例)工厂
语法格式:
<bean id="工厂标识符" class="工厂权限定名"></bean>
<bean id="动态工厂标识符" factory-bean="工厂标识符" factory-method="动态方法名"></bean>
举例:
<bean id="userFactory" class="com.springioc.entity.UserFactory"></bean>
<bean id="dynamicCreateUser" factory-bean="userFactory" factory-method="DynamicCreateUser"></bean>
-->
<bean id="userFactory" class="com.springioc.entity.UserFactory"></bean>
<bean id="dynamicCreateUser" factory-bean="userFactory" factory-method="DynamicCreateUser"></bean>
<!-- Bean便签常用属性:
id:bean对象标识符
factory-bean:bean对象创建工厂标识符
class:权限定名
factory-method:对象创建工厂函数
init-method:bean对象初始化方法名
destory-method:bean对象销毁方法名
lazy-init:延迟bean对象实例化,当设置为true的时候,不管是不是单例(singleton),当调用getBean()的时候才会创建对象
Beans标签常用属性:(作用于beans标签中的所有bean标签)
default-init-method:默认bean对象初始化方法名
default-destory-method:默认bean对象销毁方法名
default-lazy-init:默认延迟bean对象实例化
-->
<!-- Bean对象的参数注入
注入类型可以为字符串、集合、Bean对象
1.setter注入
name:实体类属性名
value:注入参数的值
ref:注入bean对象的值
举例:
<bean id = "user" class = "com.springioc.entity.User">
<property name="id" value="1"></property>
<property name="username" value="admin"></property>
<property name="password" value="123"></property>
</bean>
相当于之前 User user = new User();
user.setId(1);
user.setUsername("admin");
user.setPassword("123");
2.构造器注入
index:实体类构造函数的形参顺序,0表示第一个参数,1表示第二参数,2表示第三个参数...
value:注入参数的值
ref:注入bean对象的值
举例:
<bean id = "user" class = "com.springioc.entity.User">
<constructor-arg index="0" value="2"></constructor-arg>
<constructor-arg index="1" value="username"></constructor-arg>
<constructor-arg index="2" value="password"></constructor-arg>
</bean>
注意:(以上两种注入方式都是以字符串的注入类型来举例的,下面是Bean对象注入和集合注入)
Bean对象的注入:如果一个实体类中的属性是另一个实体类时,注入时需将property标签的value属性改为ref属性
集合注入:(集合有set,list,map,props)
概述:假设一个实体类的属性中有集合,那么参数注入集合应该采用集合注入
举例:
<bean id = "girl" class = "com.springioc.entity.Girl">
<property name="glist">
<list>
<value>范冰冰</value>
<value>杨幂</value>
<value>王祖贤</value>
</list>
</property>
<property name="gset">
<set>
<ref bean = "user1"/>
<ref bean = "user2"/>
</set>
</property>
<property name="gmap">
<map>
<entry key="美女" value="杨幂"></entry>
<entry key="美人">
<value>范冰冰</value>
</entry>
</map>
</property>
<property name="gprops">
<props>
<prop key="美女">杨幂</prop>
<prop key="美人">范冰冰</prop>
</props>
</property>
</bean>
-->
<!-- 一个Bean对象参数注入的实例:加载JDBC驱动-->
<!-- 加载资源文件 -->
<!-- 第一步:先建一个文件,文件名为jdbc.properties,放在src/下,文件中写 admin=root
password=root
url=jdbc:mysql://localhost:3306/test
JDBCdriver=com.mysql.jdbc.Driver
第二步:在applicationContext.xml中写
<util:properties id="jdbc" location="classpath:jdbc.properties"></util:properties>
-->
<!-- 配置数据源 -->
<!-- Spring引入一种表达式语言,它和EL的语法相似,可以读取一个Bean对象/集合中的数据 -->
<!-- 第一步:编写一个和jdbc.properties文件相对应的实体类JDBCTest,属性名需要相等
第二步:在applicationContext.xml中写
<bean id="jdbc" class="com.springioc.test.JDBCTest">
<property name="admin" value="#{db.admin}"></property>
<property name="root" value="#{db.password}"></property>
<property name="url" value="#{db.url}"></property>
<property name="JDBCdriver" value="#{db.JDBCdriver}"></property>
</bean>
-->
**第四步**
编写实例化测试类
import org.springframework.context.ApplicationContext;//spring-context.jar包中的
import org.springframework.context.support.ClassPathXmlApplicationContext;//spring-context-support.jar包中的
import com.springioc.entity.User;
public class ApplicationContextBuildUserTest {
public static void main(String[] args) {
//以前的方式创建对象
// User user = new User();
// System.out.println(user);
//现在使用Spring框架的容器来创建对象
//第一步:创建Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/*第二步:从ApplicationContext容器获取Bean对象
* 1.构造器获取对象
* 语法格式:
* 类型名 变量名 = 容器对象.getBean("对象标识符",组件类型);
* 举例:
* User user = ac.getBean("user", User.class);
* 2.静态工厂获取对象
* 语法格式:
* 类型名 变量名 = 容器对象.getBean("静态工厂标识符",组件类型);
* 举例:
* User user = ac.getBean("staticFactory", User.class);
* */
User user = ac.getBean("dynamicCreateUser", User.class);
System.out.println(user);
}
第五步: 运行结果