做中介平台网站 需要什么,网站建设投标书免费,济南建网站app,网页设计与制作教程 第2版 张兵义Spring 1.Spring的含义#xff1a;
Spring 可从狭义与广义两个角度看待
狭义的 Spring 是指 Spring 框架(Spring Fremework)
广义的 Spring 是指 Spring 生态体系
2.狭义的 Spring 框架
Spring 框架是企业开发复杂性的一站式解决方案
Spring 框架的核心是 IoC 容器和 AO…Spring 1.Spring的含义
Spring 可从狭义与广义两个角度看待
狭义的 Spring 是指 Spring 框架(Spring Fremework)
广义的 Spring 是指 Spring 生态体系
2.狭义的 Spring 框架
Spring 框架是企业开发复杂性的一站式解决方案
Spring 框架的核心是 IoC 容器和 AOP 面向切面编程
Spring Ioc 负责创建与管理系统对象并在此基础上扩展功能
3.广义的 Spring 生态系统 4.Spring IoC 容器
4.1 IoC 控制反转
1IoC 控制反转全称是 Inverse of Control,是一种设计理念
2由代理人来创建与管理对象消费者通过代理人来获取对象
3IoC 的目的是降低对象间的直接耦合
加入 IoC 容器将对象统一管理让对象关联变为弱耦合 4.2 Ioc 容器是Spring生态的地基用于统一创建和管理对象的依赖 4.3 Spring IoC 容器职责
1对象的控制权交由第三方统一管理(IoC控制反转)
2利用反射技术实现运行时对象创建与关联DI依赖注入
3基于配置提高应用程序的可维护性与扩展性
4.4 DI依赖注入
1IoC 是设计理念是现代程序设计遵循的标准是宏观目标
2DI (Dependency Injection) 是具体技术实现是微观实现
3DI 在java中利用反射技术实现对象注入(Injection)
4.4.1 什么是对象依赖注入
依赖注入是指运行时将容器内对象利用反射赋给其他对象的操作
1基于Setter方法注入对象
bean idsweetApple classcom.mkyuan.ioc.entity.Apple!--IoC 容器自动利用反射机制运行时调用setXXX方法为属性赋值--property nametitle value红富士/propertyproperty namecolor value红色/propertyproperty nameorigin value欧洲/property/beanbean idlily classcom.mkyuan.ioc.entity.Childproperty namename value莉莉/propertyproperty nameapple refsweetApple/property/bean案例
BookDao 类
public interface BookDao {public void insert();
}BookDaoImpl 类
public class BookDaoImpl implements BookDao{Overridepublic void insert() {System.out.println(向 MySQL Book 表插入一条数据);}
}BookDaoOracleImpl 类
public class BookDaoOracleImpl implements BookDao{Overridepublic void insert() {System.out.println(向 Oracle Book 表插入一条数据);}
}BookService 类
public class BookService {private BookDao bookDao;public BookDao getBookDao() {return bookDao;}public void setBookDao(BookDao bookDao) {this.bookDao bookDao;}public void purchase(){System.out.println(正在执行图书采购业务方法);bookDao.insert();}
}applicationContext-dao.xml
bean idbookDao classcom.mkyuan.ioc.bookshop.dao.BookDaoOracleImpl/bean可通过选择Dao接口下不同的实现类注入对象 applicationContext-service.xml bean idbookService classcom.mkyuan.ioc.bookshop.service.BookServiceproperty namebookDao refbookDao/property/beanBookShopApplication 类
public class BookShopApplication {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(classpath:applicationContext-*.xml);BookService bookService context.getBean(bookService, BookService.class);bookService.purchase();}
}2基于构造方法注入对象
bean idsweetApple classcom.mkyuan.ioc.entity.Appleproperty nametitle value红富士/propertyproperty nameorigin value欧洲/propertyproperty namecolor value红色/property/beanbean idlily classcom.mkyuan.ioc.entity.Childproperty namename value莉莉/propertyproperty nameapple refsweetApple/property/bean4.4.2 注入集合对象
Company 类
Data
NoArgsConstructor
AllArgsConstructor
public class Company {private SetString rooms;private MapString, Computer computers;private Properties info;
}Computer 类
Data
NoArgsConstructor
AllArgsConstructor
public class Computer {private String brand;private String type;private String sn;private Float price;
}applicationContext.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdbean idc1 classcom.mkyuan.ioc.entity.Computerconstructor-arg namebrand value联想/constructor-argconstructor-arg nametype value台式机/constructor-argconstructor-arg namesn value8389283012/constructor-argconstructor-arg nameprice value3085/constructor-arg/beanbean idcompany classcom.mkyuan.ioc.entity.Companyproperty nameroomssetvalue2001-总裁办/valuevalue2003-总经理办公室/valuevalue2010-研发部会议室/valuevalue2010-研发部会议室/value/set/propertyproperty namecomputersmapentry keydev-88172 value-refc1/entryentry keydev-88173bean classcom.mkyuan.ioc.entity.Computerconstructor-arg namebrand value联想/constructor-argconstructor-arg nametype value笔记本/constructor-argconstructor-arg namesn value8389283012/constructor-argconstructor-arg nameprice value5060/constructor-arg/bean/entry/map/propertyproperty nameinfopropsprop keyphone010-12345678/propprop keyaddress深圳市xxx路xx大厦/propprop keywebsitehttp://www.xxxx.com/prop/props/property/bean
/beansSpringApplication 类
public class SpringApplication {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);Company company context.getBean(company, Company.class);String website company.getInfo().getProperty(website);System.out.println(website);System.out.println(company);}
}4.4.3 查看容器内对象
SpringApplication
public class SpringApplication {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);Company company context.getBean(company, Company.class);String website company.getInfo().getProperty(website);System.out.println(website);System.out.println(company);System.out.println();//获取容器内所有BeanId数组String[] beanNames context.getBeanDefinitionNames();for (String beanName : beanNames) {System.out.println(beanName);System.out.println(类型context.getBean(beanName).getClass().getName());System.out.println(内容context.getBean(beanName));}}
}5.Spring Ioc 初体验
5.1.案例
1妈妈在早餐后给三个孩子分发餐后水果
2盘子里装有三个水果红富士/青苹果/金帅
3孩子们口味不同莉莉喜欢甜的/安迪喜欢酸的/露娜喜欢软的
如何让孩子得到喜欢的苹果
5.1.1 普通方式
Apple 类
Data
NoArgsConstructor
AllArgsConstructor
public class Apple {private String title;private String color;private String origin;
}Child 类
Data
NoArgsConstructor
AllArgsConstructor
public class Child {private String name;private Apple apple;public void eat() {System.out.println(name 吃到了 apple.getOrigin() 种植的 apple.getTitle());}
}Application 类
public class Application {public static void main(String[] args) {Apple apple1 new Apple(红富士, 红色, 欧洲);Apple apple2 new Apple(青苹果, 绿色, 中亚);Apple apple3 new Apple(金帅, 黄色, 中国);Child lily new Child(莉莉, apple1);Child andy new Child(安迪, apple2);Child luna new Child(露娜, apple3);lily.eat();andy.eat();luna.eat();}
}5.1.2 IoC 方式
引入依赖 dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.9.RELEASE/version/dependency创建 applicationContext.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdbean idsweetApple classcom.mkyuan.ioc.entity.Appleproperty nametitle value红富士/propertyproperty nameorigin value欧洲/propertyproperty namecolor value红色/property/beanbean idsourApple classcom.mkyuan.ioc.entity.Appleproperty nametitle value青苹果/propertyproperty nameorigin value中亚/propertyproperty namecolor value绿色/property/beanbean idsoftApple classcom.mkyuan.ioc.entity.Appleproperty nametitle value金帅/propertyproperty nameorigin value中国/propertyproperty namecolor value黄色/property/beanbean idlily classcom.mkyuan.ioc.entity.Childproperty namename value莉莉/propertyproperty nameapple refsweetApple/property/beanbean idandy classcom.mkyuan.ioc.entity.Childproperty namename value安迪/propertyproperty nameapple refsourApple/property/beanbean idluna classcom.mkyuan.ioc.entity.Childproperty namename value露娜/propertyproperty nameapple refsoftApple/property/bean/beansSpringApplication 类
public class SpringApplication {//创建 spring IoC 容器并且根据配置文件在容器中实例化对象ApplicationContext context new ClassPathXmlApplicationContext(classpath:applicationContext.xml);Apple sweetApple context.getBean(sweetApple, Apple.class);System.out.println(sweetApple);Child lily context.getBean(lily, Child.class);lily.eat();Child andy context.getBean(andy, Child.class);andy.eat();Child luna context.getBean(luna, Child.class);luna.eat();
}6.Bean 的配置方式
6.1 基于 XML 配置 bean
1基于构造方法实例化对象
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd!--bean标签默认通过默认构造方法创建对象--bean idapple1 classcom.mkyuan.ioc.entity.Apple/bean!--bean标签通过带参构造方法创建对象--bean idapple2 classcom.mkyuan.ioc.entity.Appleconstructor-arg nametitle value红富士/constructor-argconstructor-arg namecolor value红色/constructor-argconstructor-arg nameorigin value欧洲/constructor-argconstructor-arg nameprice value19.8/constructor-arg/beanbean idapple3 classcom.mkyuan.ioc.entity.Appleconstructor-arg index0 value红富士/constructor-argconstructor-arg index1 value红色/constructor-argconstructor-arg index2 value欧洲/constructor-argconstructor-arg index3 value19.8/constructor-arg/bean
/beans2基于静态工厂实例化对象
AppleStaticFactory 类
/*** 静态工厂通过静态方法创建对象隐藏创建对象的细节*/
public class AppleStaticFactory {public static Apple createSweetApple() {Apple apple new Apple();apple.setTitle(红富士);apple.setOrigin(欧洲);apple.setColor(红色);return apple;}
}!--利用静态工厂获取对象--bean idapple4 classcom.mkyuan.ioc.factory.AppleStaticFactory factory-methodcreateSweetApple/bean3基于工厂实例方法实例化对象
AppleFactoryInstance 类
/*** 工厂实例方法创建对象是指 Ioc 容器对工厂进行实例化并调用对应的实例方法创建对象的过程*/
public class AppleFactoryInstance {public Apple createSweetApple() {Apple apple new Apple();apple.setTitle(红富士);apple.setOrigin(欧洲);apple.setColor(红色);return apple;}
}!--利用工厂实例方法获取对象--bean idfactoryInstance classcom.mkyuan.ioc.factory.AppleFactoryInstance/beanbean idapple5 factory-beanfactoryInstance factory-methodcreateSweetApple/bean6.1.1 id和name属性相同点
1bean id与 name 都是设置对象在 IoC 容器中唯一标示
2两者在同一个配置文件中都不允许出现重复
3两者允许在多个配置文件中出现重复对象新对象覆盖旧对象
6.1.2 id和name属性区别
1id 更为严格一次只能定义一个对象标示推荐
2name 更为宽松一次允许定义多个对象标示 bean nameapple2,apple7 classcom.mkyuan.ioc.entity.Appleconstructor-arg nametitle value红富士2号/constructor-argconstructor-arg namecolor value红色/constructor-argconstructor-arg nameorigin value欧洲/constructor-argconstructor-arg nameprice value29.8/constructor-arg/bean3id 与 name 的命名要求有意义按照驼峰命名书写
4 没有id和name默认使用类名全称作为bean标示 bean classcom.mkyuan.ioc.entity.Appleconstructor-arg nametitle value红富士3号/constructor-argconstructor-arg namecolor value红色/constructor-argconstructor-arg nameorigin value欧洲/constructor-argconstructor-arg nameprice value29.8/constructor-arg/beanApple apple2 context.getBean(com.mkyuan.ioc.entity.Apple, Apple.class);
System.out.println(apple2);6.2 基于注解配置 bean
6.2.1 基于注解的优势
1摆脱繁琐的XML形式的bean与依赖注入的配置
2基于声明式的原则更适合轻量级的现代企业的应用
3让代码的可读性变的更好研发人员拥有更好的开发体验
6.2.2 三类注解
1组件类型注解-声明当前类的功能与职责
2自动装配注解-根据属性特征自动注入对象
3元数据注解-更细化的辅助 IoC 容器管理对象的注解
6.2.3 四种组件类型注解
6.2.4 两种自动装配注解
6.3 基于 java 代码配置 bean
Config 类
Configuration
ComponentScan(basePackages com.mkyuan)
public class Config {Beanpublic UserDao userDao() {UserDao userDao new UserDao();System.out.println(已创建 userDao);return userDao;}BeanPrimarypublic UserDao userDao1() {UserDao userDao new UserDao();System.out.println(已创建 userDao);return userDao;}Beanpublic UserService userService(UserDao userDao, EmployeeDao employeeDao) {UserService userService new UserService();System.out.println(已创建 userService);userService.setUserDao(userDao);System.out.println(调用setUserDao userDao);userService.setEmployeeDao(employeeDao);System.out.println(调用setEmployeeDao employeeDao);return userService;}BeanScope(prototype)public UserController userController(UserService userService) {UserController userController new UserController();System.out.println(已创建 userController);userController.setUserService(userService);System.out.println(调用setUserService userService);return userController;}
}7.bean scope 属性
7.1 bean scope属性 bean scope属性用于决定对象何时被创建与作用范围
bean scope 配置将影响容器内对象的数量
bean scope 默认值singleton(单例),指全局共享一个对象实例默认情况下bean会在 Ioc 容器创建后自动实例化全局唯一
7.2 singleton与 prototype 对比 8.bean 的生命周期 Order 类
public class Order {private Float price;private Integer quantity;private Float total;public Order() {System.out.println(创建Order对象, this);}public void init() {System.out.println(执行init方法);total price * quantity;}public void destroy() {System.out.println(释放与订单对象相关的资源);}public void pay() {System.out.println(订单金额为 total);}public Float getPrice() {return price;}public void setPrice(Float price) {System.out.println(设置price: price);this.price price;}public Integer getQuantity() {return quantity;}public void setQuantity(Integer quantity) {System.out.println(设置quantity: quantity);this.quantity quantity;}public Float getTotal() {return total;}public void setTotal(Float total) {this.total total;}
}applicationContext.xml
bean idorder1 classcom.mkyuan.ioc.entity.Order init-methodinit destroy-methoddestroyproperty nameprice value19.8/propertyproperty namequantity value20/property
/beanSpringApplication 类
public class SpringApplication {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);System.out.println(IoC容器已初始化);Order order1 context.getBean(order1, Order.class);order1.pay();((ClassPathXmlApplicationContext) context).registerShutdownHook();}
}执行结果