初识Spring:

Spring作者:Rod Johnson

Spring框架由20个模块组成,这些模块分成六个部分,分别是Core Container,Data Access/Integration,Web,AOP,Instrumentation和Test.

Spring Core是框架的最基础的部分,提供了IoC特性。Spring Context为企业级开发提供了遍历和集成的工具。

Spring Aop是基于Spring Core的符合规范的切面编程的实现

Spring JDBC提供了提供了JDBC的抽象层,简化了JDBC编码

Spring ORM对市面上流行的ORM框架提供了支持

Spring Web为Spring在Web应用程序中的使用提供了支持

Spring 体系结构图:

最基础部分========Spring IoC

控制反转   (依赖注入)  面向对象编程的一种设计理念,降低程序代码之间的耦合度

先定义持久化方法:


public interface IUserBiz {
//隔离的作用
public void save(User user);
}

实现对User类的持久化操作

public class UserBiz implements IUserBiz {
private IDao dao;
public void save(User user) {
dao.save(user);
}
public void setDao(IDao dao) {
this.dao = dao;
}
}

用户业务类,实现对User功能的业务管理

public class UserServiceImpl implements UserService
{ private UserDao dao=new UserDaoImpl();
public void addNewUser(User user){
dao.save(user);
} }

bean文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--IOC-->
<bean id="happyService" class="cn.zixin.service.HappyService">
<!--DI 依赖注入-->
<property name="info" value="Spring"></property>
</bean> <!--准备一个彩色墨盒-->
<bean id="colorInk" class="cn.zixin.printer.ink.GrayInk"></bean>
<!--准备一个B5纸-->
<bean id="B5paper" class="cn.zixin.printer.paper.A5Paper"></bean>
<!--准备打印机-->
<bean id="printer" class="cn.zixin.printer.print.Printer">
<property name="ink" ref="colorInk"></property>
<property name="paper" ref="B5paper"></property> </bean> <!--dao-->
<bean id="UserDao" class="cn.zixin.aop.UserDao"></bean>
<!--service-->
<bean id="UserBiz" class="cn.zixin.aop.service.UserBiz">
<property name="dao" ref="UserDao"></property>
</bean>
<!--增强配置-->
<!--前置配置-->
<bean id="LoggerAfter" class="cn.zixin.aop.LoggerAfter"></bean>
<!--后置配置-->
<bean id="LoggerBefore" class="cn.zixin.aop.LoggerBefore"></bean> <!--Aop配置-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="LoggerAfter" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="LoggerBefore" pointcut-ref="pointcut"/>
</aop:config>
</beans>

使用<BEAN>的一个组件时   ----------    id用来访问的唯一名称  name属性指定

测试类:

package cn.zixin.test;

import cn.zixin.aop.User;
import cn.zixin.aop.service.IUserBiz;
import cn.zixin.printer.print.Printer;
import cn.zixin.service.HappyService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by benxin on 2017/7/22.
*/
public class FirstSpringTest { @Test
public void firstTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HappyService service=(HappyService) context.getBean("happyService");
service.work();
}
@Test
public void firstTests(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Printer printer = (Printer) context.getBean("printer");
printer.print();
}
@Test
public void aop() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserBiz biz=(IUserBiz)ctx.getBean("UserBiz");
User user=new User();
biz.save(user);
System.out.println("success!");
}
}

最新文章

  1. 在网站开发中很有用的8个 jQuery 效果【附源码】
  2. [HDOJ1811]Rank of Tetris(并查集、拓扑排序)
  3. python读取文本、配对、插入数据脚本
  4. uva 1597 Searching the Web
  5. 使用 Flask-Cache 缓存给Flask提速
  6. SpringBoot入门:Hello World
  7. bootstrap treeview实现菜单树
  8. Java内存泄漏分析
  9. urls.py路由系统分发的本质
  10. VBA RemoveDuplicates方法去重复项
  11. 160道Java技术面试题
  12. Casual Note of Computer Network
  13. 变分推断(Variational Inference)
  14. SpringBoot------集成PageHelper分页功能
  15. CentOS7.6 Install TensorFlow
  16. B树和TreeSet与TreeMap
  17. sqldeveloper和plsqldebeloper
  18. 51Nod 1003 1004 1009
  19. POJ 1656 Counting Black
  20. android笔记5——同一个Activity中Fragment的切换

热门文章

  1. Xcode升级到9.3之后pod问题
  2. 架构师养成记--17.disrunptor 多生产者多消费者
  3. FlowPortal-BPM——创建新模块
  4. 半年的iOS代码生活
  5. SuperMap(无对应字段)空间属性挂接
  6. python全栈开发_day17_时间,系统模板和序列化
  7. Calibre 3.4版中,为epub书籍嵌入中文字体
  8. WCF系列教程之WCF服务配置
  9. Json&XML比较
  10. 每天一道leetcode141-环形链表