1. Spring介绍
Spring是一个企业级应用框架,其中包含了大量的各种应用组件,Spring框架为现代基于java的企业级应用程序提供了一个全面的编程和配置模型,能够在任何类型的部署平台进行部署.其核心是IoC及AOP。
Spring中的两个核心概念
1.1 IoC
控制反转(Inversion of Control),将原来我们来完成的实例化过程,交给 容器来完成,对组件对象的控制权从代码本身转移到容器. 组件的思想:分离专注点,使用接口,不再关注实现. 依赖的注入:将组建的构建和实现分离
- DI
依赖注入,依赖于某一个媒介完成对对象的初始化或者是赋值。
1.2 AOP
面向切面编程(Aspect Oriented Programing)
1.3 Spring 优点
- 低浸入式编程
- 独立于各种应用服务器
- 依赖注入特性将组件关系透明化,降低了耦合度
- 面向切面编程特性允许将通用任务进行集中是处理
- 与第三方框架的良好整合
1.4 Spring工程构建
1.4.1 Maven Spring依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
1.4.2 Spring核心配置文件编写
<?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">
</beans>
1.4.2.1 完成控制反转以及依赖注入
<bean id="userInfo" class="com.domain.UserInfo">
</bean>
<property name="username" value="张三" />
<property name="content" value="刘家豪大笨蛋!" />
<bean id="userInfo" class="com.domain.UserInfo">
<property name="username" value="张三" />
<property name="content" value="刘家豪大笨蛋!" />
</bean>
bean的id名称,在同一个配置文件下,是只能拥有一个,不能够出现重复的id名称!
<bean id="userInfo" class="com.domain.UserInfo">
<property name="username" value="张三" />
<property name="content" value="刘家豪大笨蛋!" />
</bean>
<bean id="userInfo" class="com.domain.UserInfo">
<property name="username" value="李四" />
<property name="content" value="翟雪松大帅哥!" />
</bean>
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name ‘userInfo’ is already used in this element Offending resource: class path resource [applicationContext.xml]
1.4.2.2 测试
@Test
public void testInit() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml") ;
UserInfo userInfo = (UserInfo) ctx.getBean("userInfo") ;
userInfo.say();
}
1.4.2.3 Spring中的IoC产生的对象是否是单例模式
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml") ;
UserInfo userInfo = (UserInfo) ctx.getBean("userInfo1") ;
userInfo.say();
System.out.println(userInfo);
userInfo = (UserInfo) ctx.getBean("userInfo1") ;
userInfo.say();
System.out.println(userInfo);
以上代码,对同一个bean进行两次调用,得到对象是同一个对象,说明在Spring的设计当中,所使用的 是单例设计模式!每一个调用的bean,都会生成一个新的对象。
<bean id="userInfo1" class="com.domain.UserInfo">
<property name="username" value="张三" />
<property name="content" value="刘家豪大笨蛋!" />
</bean>
<bean id="userInfo2" class="com.domain.UserInfo">
<property name="username" value="李四" />
<property name="content" value="翟雪松大帅哥!" />
</bean>
1.5 bean的三种实例化方式
1.5.1 构造方法方式【重点】
-
BookDaoImpl实现类 public class BookDaoImpl implements BookDao {
public BookDaoImpl() {
System.out.println("book dao constructor is running ....");
}
public void save() {
System.out.println("book dao save ...");
}
}
-
applicationContext.xml配置
<bean id="bookDao" class="com.csi.dao.impl.BookDaoImpl"/>
-
AppForInstanceBook测试类 public class AppForInstanceBook {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
}
}
1.5.2 静态工厂方式
-
OrderDao接口和OrderDaoImpl实现类 public interface OrderDao {
public void save();
}
public class OrderDaoImpl implements OrderDao {
public void save() {
System.out.println("order dao save ...");
}
}
-
OrderFactory工厂类
public class OrderDaoFactory {
public static OrderDao getOrderDao(){
System.out.println("factory setup....");
return new OrderDaoImpl();
}
}
-
applicationContext.xml配置
<bean id="orderDao" class="com.csi.factory.OrderDaoFactory" factorymethod="getOrderDao"/>
-
AppForInstaceOrder测试类 public class AppForInstanceOrder {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
OrderDao orderDao = (OrderDao) ctx.getBean("orderDao");
orderDao.save();
}
}
1.5.3 实例工厂方式
-
UserDao接口和UserDaoImpl实现类 public interface UserDao {
public void save();
}
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("user dao save ...");
}
}
-
UserDaoFactory工厂类
public class UserDaoFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
-
applicationContext.xml配置 <!--方式三:使用实例工厂实例化bean-->
<bean id="userFactory" class="com.csi.factory.UserDaoFactory"/>
<bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>
-
AppForInstanceUser测试类 public class AppForInstanceUser {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) ctx.getBean("userDao");
userDao.save();
}
}
1.6 bean的生命周期
-
使用init-method及destory-mothod public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("book dao save ...");
}
public void init(){
System.out.println("init...");
}
public void destory(){
System.out.println("destory...");
}
}
<bean id="bookDao" class="com.csi.dao.impl.BookDaoImpl" init-method="init" destroy-method="destory"/>
public class AppForLifeCycle {
public static void main( String[] args ) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
ctx.close();
}
}
-
实现initializingBean,DisposableBean接口 public class BookServiceImpl implements BookService,InitializingBean,DisposableBean {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
System.out.println("set .....");
this.bookDao = bookDao;
}
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
public void destroy() throws Exception {
System.out.println("service destroy");
}
public void afterPropertiesSet() throws Exception {
System.out.println("service init");
}
}
1.61 Bean的销毁时机
|