1.Spring简介
Spring是一个非常活跃的开源框架;基于IOC和AOP来构架多层JavaEE系的框架,它的主要目地是简化企业开发。
Spring提供了对开源社区中很多框架及JavaEE中很多技术的支持,让程序员很轻松能整合及使用这些框架技术。
Spring以一种非侵入式的方式来管理你的代码可以适当的时候安装或卸载Spring。
2.IOC/DI
控制反转(Inversion of Control,IoC)与依赖注入(Dependency Injection)
IOC控制反转:不需要实例化对象
DI依赖注入:提前准备要使用的对象
3.实例化Spring容器
Spring最底层的容器是由BeanFactory定义,使用方法:
(1)定义一个Spring Resource
(2)创建一个XmlBeanFactory
在实际应用中,容器一般使用ApplicationContext,它是高级别的BeanFactory。
使用ApplicationContext,实例化Spring容器常用的两种方式:
方法一:
在类路径下寻找配置文件来实例化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});//数组的方式表示可以是多个文件
可以在整个类路径中寻找xml文件
* 通过这种方式加载。需要将spring的配置文件放到当前项目的classpath路径下
* classpath路径指的是当前项目的src目录,该目录是java源文件的存放位置。
方法二:
在文件系统路径下寻找配置文件来实例化容器
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});
4.Spring其他知识点及用法
(1)为什么使用spring
降低组件之间的耦合度,实现软件各层之间的解耦。
可以使用容器提供的众多服务,如:事务管理服务、消息服务等等。当我们使用容器管理事务时,开发人员就不再需要手 工控制事务.也不需处理复杂的事务传播。
容器提供单例模式支持,开发人员不再需要自己编写实现代码。
容器提供了AOP技术,利用它很容易实现如权限拦截、运行期监控等功能。
容器提供的众多辅作类,使用这些类能够加快应用的开发,如: JdbcTemplate、 HibernateTemplate。
Spring对于主流的应用框架提供了集成支持,如:集成Hibernate、JPA、Struts等,这样更便于应用的开发。
(2)三种实例化bean的方式
第一种、使用类构造器实例化(默认无参数)
<bean id=“personService" class="com.bean.impl.PersonServiceImpl"/>
第二种、使用静态工厂方法实例化(简单工厂模式)
<bean id="personService" class="com.factory.PersonServiceFactory"
factory-method="createPersonService" />
public class PersonServiceFactory {
public static PersonService createPersonService(){
return new PersonServiceImpl();
}
}
第三种、使用实例工厂方法实例化(工厂方法模式):
<bean id=“personServiceFactory" class="com.factory.PersonServiceFactory"/>
<bean id="personService" factory-bean=“personerviceFactory"
factory-method="createPersonService" />
public class PersonServiceFactory {
public PersonService createPersonService(){
return new PersonServiceImpl();
}
}
(3)Bean的作用域
.singleton(默认值)
在每个Spring IoC容器中一个bean定义只有一个对象实例(共享)。
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
<bean id="xxx" class="com.sysmaster.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
.prototype
允许bean可以被多次实例化(使用一次就创建一个实例)
(4).指定Bean的初始化方法和销毁方法
<bean id=“test” class=“...Test”
init-method=“setup”
destory-method=“teardown”/>
//Bean被调用自动执行 自动销毁
(5)通过setter方法注入依赖
<bean>元素的< property >子元素指明了使用它们的set方法来注入。
<bean id="personService" class="com.sysmaster.bean.impl.PersonServiceImpl">
<!-- 基本类型,string类型 -->
<property name="age" value="20"></property>
<property name="name" value="张无忌"></property>
</bean>
<!-- 引用其它bean-->
<bean id="person" class="com.sysmaster.bean.Person" />
<bean id="personService" class="com.sysmaster.bean.impl.PersonServiceImpl">
<!-- 引用类型 -->
<property name="person" ref="person" />
</bean>
<!-- 内部bean-->
<bean id="personService" class="com.sysmaster.bean.impl.PersonServiceImpl">
<!-- 内部bean注入 -->
<property name="personClass">
<bean class="com.sysmaster.bean.PersonClass" />
</propert>
</bean>
(6)通过构造函数注入依赖
<!-- 通过参数的顺序 -->
<constructor-arg index="0">
<value>张三</value>
</constructor-arg>
<constructor-arg index="1">
<value>56</value>
</constructor-arg>
<!-- 通过参数的类型 -->
<constructor-arg type="java.lang.Integer">
<value>56</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>张三</value>
</constructor-arg>
5.Spring的环境搭建
(1)导入类库(注意由于junit jar版本问题,可能出现异常)
commons-logging-1.2.jar
hamcrest-core-1.3.jar
junit-4.12.jar
spring-aop-4.3.13.RELEASE.jar
spring-beans-4.3.13.RELEASE.jar
spring-context-4.3.13.RELEASE.jar
spring-core-4.3.13.RELEASE.jar
spring-expression-4.3.13.RELEASE.jar
spring-test-4.3.13.RELEASE.jar
(2)编写applicationContext.xml配置文件(名字可更改)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="office" class="net.service.impl.Excel"></bean>
<!--单例默认singleton --> <!--配置文件的方式 -->
<!-- <bean id="office" class="net.service.impl.Excel" scope="prototype">
</bean> 多例模式-->
</beans>
(3)初始化Spring容器
public class TestOffice {
/*@Test
public void testOffice(){
//启动spring环境
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从环境中获取对象
Office office = (Office) context.getBean("office");
Office office1 = (Office) context.getBean("office");
System.out.println("是否是统一对象"+(office==office1));
//调用对象的方法
office.print();
}
}
(4)其他相关类
package net.service;
public interface Office {
public void print();
}
package net.service.impl;
import net.service.Office;
public class Excel implements Office {
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("我是excel");
}
}
6.Spring的使用(注解的方式)
(1)修改配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="net.service">
</context:component-scan>
<!-- 扫描包 -->
</beans>
(2)测试
package net.service;
public class TestOffice extends MyTest{
//DI
@Resource
private Office word;
@Test
public void testWord() {
word.print();
}
}
(3)其他类
//IOC
@RunWith(SpringJUnit4ClassRunner.class)//spring与junit整合
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
public class MyTest {
}