Spring:
-
降低解耦,aop编程的支持,声明式事务的支持
-
ioc (控制反转)和 aop(切面编程)
-
体系结构
-
使用 spring的 IOC解决程序耦合的步骤
a) 在pom.xml中导入依赖的spring包
b) 创建相应的dao和service以及实现
c) 在resource下配置bean.xml
d) 测试配置是否成功 -
相应的代码如下
//在pom中导入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
//实现类
public class IaccountImp implements Iaccount {
public void saveUser() {
System.out.println("保存数据");
}
}
public class IaccountSerImp implements IaccountSer {
private Iaccount iaccount= new IaccountImp();//此处的依赖有待解决
public void saveAcc() {
iaccount.saveUser();
}
}
//配置的bean.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标签:用于配置让spring创建对象,并且存入ioc容器之中
id属性:对象的唯一标识
class属性:指定要创建对象的全限定类名-->
<!--配置service-->
<bean id="IaccountSer" class="service.imp.IaccountSerImp"></bean>
<!--配置dao-->
<bean id="Iaccount" class="dao.imp.IaccountImp"></bean>
</beans>
//测试
@Test
public void test()
{
//使用ApplicationContext接口,就是再获取spring容器
ClassPathXmlApplicationContext cp = new ClassPathXmlApplicationContext("bean.xml");
//根据bean的id获取对象
IaccountSer aService= (IaccountSer) cp.getBean("IaccountSer");
System.out.println(aService);
Iaccount ac= (Iaccount) cp.getBean("Iaccount");
System.out.println(ac);
}
- Spring基于 XML 的 IOC 细节
a) BeanFactory和 ApplicationContext 的区别
BeanFactory 才是 Spring 容器中的顶层接口。
ApplicationContext 是它的子接口。
BeanFactory 和 ApplicationContext 的区别:
创建对象的时间点不一样。
ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
BeanFactory:什么使用什么时候创建对象。
b) ApplicationContext 接口的实现类
ClassPathXmlApplicationContext: 它是从类的根路径下加载配置文件 推荐使用这种
FileSystemXmlApplicationContext: 它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
AnnotationConfigApplicationContext: 当我们使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。 - Ioc中bean标签和管理对象
a) Bean标签
作用:用于配置对象让spring来创建的
默认情况下她调用的类中的无参构造函数,如果没有则不能创建成功
属性:
Id:给对象在容器中提供一个唯一的标识,用于获取对象
Class:指定类的全限定类名,哦那个与反射创建对象,默认情况下调用无参构造函数
Init-methon:指定类中的初始化方法名称
Destory-methon:指定类中销毁方法名称
Scope:指定对象的作用范围:
Singleton:单例对象
Prototype:多例的
Request:web项目中,spring 创建一个bean对象,将对象存入到request中
Session:web项目中,存入到session
Global session:web项目中,应用在portlet环境,若没有,则相当于sesion
2 bean 的作用范围和生命周期
单例对象:scope=“singleton” 一个应用只有一个对象的实例。
它的作用范围就是整个引用。
生命周期:
对象出生:当应用加载,创建容器时,对象就被创建了。
对象活着:只要容器在,对象一直活着。
对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
多例对象:scope=“prototype” 每次访问对象时,都会重新创建对象实例。
生命周期:
对象出生:当使用对象时,创建新的对象实例。
对象活着:只要对象在使用中,就一直活着。
对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。 - 实例化bean的三种方式
a. 使用无参构造方法,如果bean中没有,则会失效
b. Spring管理静态工厂。使用静态工厂的方法创建对象
此种方式是: 使用 StaticFactory 类中的静态方法 createAccountService 创建对象,并存入 spring 容器
id 属性:指定 bean 的 id,用于从容器中获取
class 属性:指定静态工厂的全限定类名
factory-method 属性:指定生产对象的静态方法
c. spring管理实例工厂,使用实例工厂的方法创建对象:
此种方式是: 先把工厂的创建交给 spring 来管理。 然后在使用工厂的 bean 来调用里面的方法
factory-bean 属性:用于指定实例工厂 bean 的 id。
factory-method 属性:用于指定实例工厂中创建对象的方法。
代码实现
第二种方法:
/*第二种:spring管理静态工厂,-使用静态工厂的方法创建对象
* 模拟一个静态工厂,创建业务层实现类*/
public class fac {
public static IaccountSer creaAccountSer()
{
return new IaccountSerImp();
}
}
<!--此二种方法是:
使用fac类中的静态方法creaAccountSer创建对象,并存入spring容器中
id:指定bean的id,用于从容器中获取
class:指定静态工厂的全限定类名
factory-method:指定生产对象的静态方法
-->
<bean id="accountServive" class="ui.fac" factory-method="creaAccountSer"></bean>
/**
* 第二种:spring管理静态工厂,-使用静态工厂的方法创建对象
* * 模拟一个静态工厂,创建业务层实现类*
*/
@Test
public void test1()
{
//使用ApplicationContext接口,就是再获取spring容器
ClassPathXmlApplicationContext cp = new ClassPathXmlApplicationContext("bean.xml");
//根据bean的id获取对象
IaccountSer aService= (IaccountSer) cp.getBean("accountServive");
System.out.println(aService);
/*service.imp.IaccountSerImp@6d4d66d2
dao.imp.IaccountImp@2a265ea9*/
Iaccount ac= (Iaccount) cp.getBean("Iaccount");
System.out.println(ac);
}
第三种方法:
/*第三种方式:spring管理实例工厂-使用实例工厂的方法创建对象 /**
* 模拟一个实例工厂,创建业务层实现类 * 此工厂创建对象,必须现有工厂实例对象,再调用方法 */
public class fac3 {
public IaccountSer creaAccountSer()
{
return new IaccountSerImp();
}
}
<!-- 此3种方式是: 先把工厂的创建交给 spring 来管理。
然后在使用工厂的 bean 来调用里面的方法
factory-bean 属性:用于指定实例工厂 bean 的 id。
factory-method 属性:用于指定实例工厂中创建对象的方法。 -->
<bean id="instancFactory" class="ui.fac3"></bean>
<bean id="accountService"
factory-bean="instancFactory"
factory-method="creaAccountSer"></bean>
Spring-依赖注入
- 构造函数注入
public class IaccountSerImp implements IaccountSer {
/*构造函数依赖注入
* 顾名思义,就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置
的方式,让 spring 框架来为我们注入。具体代码如下: */
private Integer age;
private String name;
private Date birthday;
public IaccountSerImp(Integer age, String name, Date birthday) {
this.age = age;
this.name = name;
this.birthday = birthday;
}
@Override
public void saveAcc() {
System.out.println(age+","+name+","+birthday);
}
}
3. <?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">
<!-- 使用构造函数的方式,给 service 中的属性传值
要求: 类中需要提供一个对应参数列表的构造函数。
涉及的标签: constructor-arg
属性: index:指定参数在构造函数参数列表的索引位置
type:指定参数在构造函数中的数据类型
name:指定参数在构造函数中的名称 用这个找给谁赋值
=======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
value:它能赋的值是基本数据类型和 String 类型
ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean -->
<bean id="accountService" class="service.imp.IaccountSerImp">
<constructor-arg name="name" value=" 张三 "></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
</beans>
2 . Set方法注入、
<!-- 通过配置文件给 bean 中的属性传值:使用 set 方法的方式
涉及的标签:
property
name:找的是类中 set 方法后面的部分
ref:给属性赋值是其他 bean 类型的
value:给属性赋值是基本数据类型和 string 类型的 实际开发中,此种方式用的较多。 -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="name" value="test"></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>
- 使用p名称空间注入数据,本质还是调用set方法
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p=http://www.springframework.org/schema/p
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="accountService" class="service.imp.IaccountSerImp"
p:name="one" p:age="18" p:birthday-ref="now"></bean>
- 注入集合
public class IaccountSerImp implements IaccountSer {
//注入集合属性
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
public void saveAcc() {
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(myMap);
System.out.println(myProps);
System.out.println(mySet);
}
}
bean.xml
<property name="mySet">
<list>
<value>set</value>
<value>set</value>
<value>set</value>
</list>
</property>
<property name="myProps">
<map>
<entry key="pp" value="00"></entry>
<entry key="pp" value="00"></entry>
<entry key="pp" value="00"></entry>
</map>
</property>
</bean>