spring框架

1.Spring的两大核心
IoC(Inverse Of Control:反转控制)
  作用:削减计算机程序的耦合(解除我们代码中的依赖关系)
AOP(Aspect Oriented Programming:面向切面编程)
  作用:在程序运行期间,不修改源码对已有方法进行增强  

2.Spring的优势
方便解耦,简化开发
通过 Spring 提供的 IoC 容器,可以将对象间的依赖关系交由 Spring 进行控制,避免硬编码所造成的过度程序耦合。用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用。

AOP 编程的支持
通过 Spring 的 AOP 功能,方便进行面向切面的编程,许多不容易用传统 OOP 实现的功能可以通过 AOP 轻松应付。

声明式事务的支持
可以将我们从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活的进行事务的管理,提高开发效率和质量。

方便程序的测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可做的事情。

方便集成各种优秀框架
Spring 可以降低各种框架的使用难度,提供了对各种优秀框架(Struts、Hibernate、Hessian、 Quartz等)的直接支持。

降低 JavaEE API 的使用难度
Spring 对 JavaEE API(如 JDBC、 JavaMail、远程调用等)进行了薄薄的封装层,使这些 API 的使用难度大为降低。

Java 源码是经典学习范例
Spring 的源代码设计精妙、结构清晰、匠心独用,处处体现着大师对 Java 设计模式灵活运用以及对 Java 技术的高深造诣。它的源代码无意是 Java 技术的最佳实践的范例。


3
spring中工厂的类结构图





3.1 BeanFactory和ApplicationContext的区别

BeanFactory才是Spring容器中的顶层接口。
ApplicationContext是它的子接口。
BeanFactory和ApplicationContext的区别:
	创建对象的时间点不一样。
		ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
		BeanFactory:什么使用什么时候创建对象。



3.2 基于XML的配置(入门案例)

3.2.1 第一步:创建maven工程并导入坐标

此时我们不需要通过浏览器访问,所以打包方式选择jar即可。

pom.xml的坐标信息:


<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
 </dependencies>

为了方便测试,可以加入单元测试
<!--单元测试-->
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
</dependency>

 

3.2.2 第二步:创建业务层和接口实现类

业务层接口


/***
*业务层接口
****/
public interface AccountService {
   /***
    * 模拟保存账号
    */
   void saveAccount();
}

业务层接口实现类


/***
* 业务层接口实现类
***/
public class AccountServiceImpl implements AccountService {
   //依赖问题待解决
   private AccountDao accountDao = new AccountDaoImpl();

   /***
    * 模拟保存账号
    */
   public void saveAccount() {
       accountDao.saveAccount();
  }
}

 

 

3.2.3 第三步:创建持久层接口和实现类

持久层接口


/********
* 持久层接口
* version:1.0
******/
public interface AccountDao {
   /***
    * 模拟保存账户
    */
   void saveAccount();
}

持久层实现类


/********
* author:shenkunlin
* date:2018/7/9 15:26
* description:持久层实现类
* version:1.0
******/
public class AccountDaoImpl implements AccountDao {
   /**
    * 模拟保存账户
    */
   public void saveAccount() {
       System.out.println("保存了账户!");
  }
}

 

3.2.4 第四步:创建测试类


/********
* 模拟表现层用于调用Service
******/
public class Client {
   public static void main(String[] args) {
//依赖问题待解决
       AccountService accountService = new AccountServiceImpl();
       accountService.saveAccount();
  }
}

3.2.5 第五步:在类的根路径下创建一个任意名称的xml文件(不能是中文),我们创建bean.xml

给配置文件导入约束:/spring-framework-5.0.2.RELEASE/docs/spring-framework-reference/html5/core.html


<?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>

 

3.2.6 第六步:让spring管理资源,在配置文件中配置service和dao


<?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="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
   
   <!-- 配置dao -->
   <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>

</beans>

 

3.2.7 测试配置是否成功


public class Client {
/**
    * 使用main方法获取容器测试执行
*/
   public static void main(String[] args) {
       //1.使用ApplicationContext接口,就是在获取spring容器
       ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
       //2.根据bean的id获取对象
       AccountService accountService = (AccountService) ac.getBean("accountService");
       System.out.println(accountService);

       AccountDao accountDao = (AccountDao) ac.getBean("accountDao");
       System.out.println(accountDao);
  }
}

日志信息


信息: Loading XML bean definitions from class path resource [bean.xml]
com.itheima.service.impl.AccountServiceImpl@61832929
com.itheima.dao.impl.AccountDaoImpl@29774679

 

 

 

posted @ 2019-06-04 20:13  饭后芙蓉王  阅读(157)  评论(0)    收藏  举报