影视网站建设平台,黄江网站仿做,西安seo优化工作室,win7系统下动网站建设Spring介绍
Spring是一个轻量级的Java 开发框架#xff0c;核心是IOC#xff08;控制反转#xff09;和AOP#xff08;面向切面编程#xff09;
Spring解决了业务层#xff08;Service包#xff09;与其他各层#xff08;表现层#xff0c;包括Model#xff0c;Vie…Spring介绍
Spring是一个轻量级的Java 开发框架核心是IOC控制反转和AOP面向切面编程
Spring解决了业务层Service包与其他各层表现层包括ModelViewController三部分持久层jdbc和mybatis……之间耦合度高的问题
耦合是什么Spring如何解决高耦合的
耦合是什么
耦合是衡量程序模块之间互相依赖程度的指标
耦合也可以用来衡量程序拓展性和维护性
这里的依赖不是继承关系的那种依赖
本质上指的是模块之间关联关系强弱属性直接获取修改直接调用方法通过方法参数调用使用同一全局变量使用同一其他模块参数为一部分成员变量……
松耦合代表业务层和其他层之间的耦合度较低互相依赖的程度较低彼此之间影响小 松耦合代表业务层和其他层之间的耦合度较高互相依赖的程度较高修改A的代码B也要修改
我们开发程序一般都是以“高内聚低耦合”为目标的
Spring如何解决高耦合
没有Spring之前我们使用ServletJSP和JDBC作为Java开发框架开发JavaWeb程序
JSP将业务层和视图层耦合在了一起JDBC又将业务层和持久层耦合在了一起
Spring解决了这个问题其中的关键在于IOC将创建对象使用对象销毁对象的权力交给SpringBean容器而不是代码
IOC的关键在于DI(依赖注入)
SpringMVC简介
Modelentity包-Viewthymeleaf等视图引擎-ControllerController包
Model(模型)用来处理程序中数据逻辑的部分
View(视图)在应用程序中专门和浏览器进行交互展示数据的资源
Contreller(控制器)可以理解成是一个分发器来决定对于视图发来的请求需要用哪一个模型来处理以及处理完后需要跳回到哪一个视图也就是用来连接视图和模型的
Spring框架的特点
方便解耦简化开发Spring就是一个大工厂可以将所有对象创建和依赖关系维护交给Spring管理。IOC的作用。AOP编程的支持Spring提供面向切面编程可以方便的实现对程序进行权限拦截、运行监控等功能。可扩展性声明式事务的支持只需要通过配置就可以完成对事务的管理而无需手动编程。方便程序的测试Spring对Junit4支持可以通过注解方便的测试Spring程序。方便集成各种优秀框架Spring不排斥各种优秀的开源框架其内部提供了对各种优秀框架如Struts2、Hibernate、MyBatis、Quartz等的直接支持。降低JavaEE API的使用难度Spring 对JavaEE开发中非常难用的一些APIJDBC、JavaMail、远程调用等都提供了封装使这些API应用难度大大降低。
IOC简介
IOC – Inverse of Control控制反转将对象的创建权力反转给Spring框架 控制反转Inversion of Control缩写为IoC是面向对象编程中的一种设计原则可以用来减低计算机代码之间的耦合度。 解决问题使用IOC可以解决的程序耦合性高的问题。Spring的工厂读取配置文件。
IOC创建的bean默认情况下整个内存只有一份也就是单例模式后续的测试中可以看到这一点
IOC是思想DI是IOC的实现方法两者绑定
bean的实例化
准备工作
maven依赖 dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.0.2.RELEASE/version/dependencydependencygroupIdcommons-logging/groupIdartifactIdcommons-logging/artifactIdversion1.2/version/dependencydependencygroupIdlog4j/groupIdartifactIdlog4j/artifactIdversion1.2.12/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependency/dependencies
service接口
public interface UserService {public void hello();}
service接口实现类
public class UserServiceImpl implements UserService {Overridepublic void hello() {System.out.println(Hello IOC!!);}
}
resources下创建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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd/beans
通过applicationContext.xml的方式实例化
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!--IOC管理bean--!--1. 直接实例化--bean iduserService classcn.tx.service.UserServiceImpl //beans缺点当需要加载到bean容器中的bean数量太多的时候这样一个一个导入非常繁琐
2. 静态bean工程实例化
需要一个静态工厂类
public class StaticBeanFactory {// 静态工厂方式public static UserService staticBeanCreate() {System.out.println(通过静态工厂的方式创建UserServiceImpl对象...);return new UserServiceImpl();}}随后在xml文件中导入静态工厂bean对象即可
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!--静态工厂方式--bean idstaticUs classcom.qcby.mySpring01.beanFactory.StaticBeanFactory factory-methodstaticBeanCreate/
/beans3. 动态工厂实例化
public class DynamicBeanFactory {//对象方法public UserService dynamicBeanCreate(){System.out.println(动态工厂的方式创建bean对象。。。);return new UserServiceImpl();}
}?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
!--动态工厂方式--bean iddynamicFactoryBean classcom.qcby.mySpring01.beanFactory.DynamicBeanFactory/bean iddynamicUs factory-beandynamicFactoryBean factory-methoddynamicBeanCreate/
/beans通过注解实例化
Component Service Repository Controller Mapper
bean实例化测试
public class IOCTest {Testpublic void run() {ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);
/* ApplicationContext applicationContext new FileSystemXmlApplicationContext(D:\\6_WorkSpace\\shiXun\\spring01\\src\\main\\resources\\applicationContext.xml);*/UserService userService (UserService) applicationContext.getBean(userService);UserService userService1 (UserService) applicationContext.getBean(userService);System.out.println(userService);System.out.println(userService1);userService.hello(IOC!);}Testpublic void factoryBeanTest() {ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);UserService staticBean (UserService) applicationContext.getBean(staticUs);UserService dynamicBean (UserService) applicationContext.getBean(dynamicUs);staticBean.hello(staticFactory);dynamicBean.hello(dynamicFactory);}
}依赖注入
DIDependency Injection依赖注入在Spring框架负责创建Bean对象时(bean实例化) 动态的将依赖对象对象的属性注入到Bean组件中
通过applicationContext.xml的方式注入
set方法注入
必须保证setter方法存在否则会报错
public class CarServiceImpl implements CarService {private CarDao carDao;Overridepublic String toString() {return CarServiceImpl{ msg msg \ , id id };}private String msg;private Integer id;public void setMsg(String msg) {this.msg msg;}public void setId(Integer id) {this.id id;}public void setCarDao(CarDao carDao) {this.carDao carDao;}public ListCar findAll() {ListCar carList carDao.findAll();return carList;}
}
!--set方法DI注入--bean idcarDao classcom.qcby.mySpring01.mapper.impl.CarDaoImplproperty namedataSource refdataSource//beanbean idcarService classcom.qcby.mySpring01.service.impl.CarServiceImplproperty namecarDao refcarDao/property namemsg value你好/property nameid value100//bean构造方法注入
public class Car {private int id;private String carName;private int size;private String color;public Car() {}public Car(String carName, int size, String color) {this.carName carName;this.size size;this.color color;}Overridepublic String toString() {return Car{ id id , carName carName \ , size size , color color \ };}public int getId() {return id;}public void setId(int id) {this.id id;}public String getCarName() {return carName;}public void setCarName(String carName) {this.carName carName;}public int getSize() {return size;}public void setSize(int size) {this.size size;}public String getColor() {return color;}public void setColor(String color) {this.color color;}
}
!--构造器DI注入--bean idcar1 classcom.qcby.mySpring01.pojo.Car!-- constructor-arg index0 value小米Su7 Pro Max/constructor-arg index1 value10/constructor-arg index2 valueblue/--constructor-arg namecarName value小米Su7 Pro Max/constructor-arg namesize value10/constructor-arg namecolor valueblue//beanbean idcar2 classcom.qcby.mySpring01.pojo.Car!-- constructor-arg index0 value小米Su7 Pro Max/constructor-arg index1 value10/constructor-arg index2 valueblue/--constructor-arg namecarName valueBYD秦PLUS DMI/constructor-arg namesize value198/constructor-arg namecolor valueblack//bean通过接口注入
暂时略后续补充
数组集合(List,Set,Map)Properties等的注入
public class CollectionBean {// 数组private Student[] studentArr;public void setStudentArr(Student[] studentArr) {this.studentArr studentArr;}private ListString list;public void setList(ListString list) {this.list list;}private MapString, String map;public void setMap(MapString, String map) {this.map map;}private MapStudent, Car studentCarMap;public void setStudentCarMap(MapStudent, Car studentCarMap) {this.studentCarMap studentCarMap;}private Properties properties;public void setProperties(Properties properties) {this.properties properties;}Overridepublic String toString() {return CollectionBean{ studentArr Arrays.toString(studentArr) , list list , map map , studentCarMap studentCarMap , properties properties };}
}!--引用类型/集合注入--bean idstudent1 classcom.qcby.mySpring01.pojo.Studentproperty namename value张三/property nameage value15/property namegrade value7//beanbean idstudent2 classcom.qcby.mySpring01.pojo.Studentproperty namename value李四/property nameage value14/property namegrade value6//beanbean idstudent3 classcom.qcby.mySpring01.pojo.Studentproperty namename value王五/property nameage value18/property namegrade value13//beanbean idcollectionBean classcom.qcby.mySpring01.pojo.CollectionBeanproperty namestudentArrarrayref beanstudent1/ref beanstudent2/ref beanstudent3//array/propertyproperty namelistlistvalue熊大/valuevalue熊二/valuevalue吉吉国王/value/list/propertyproperty namemapmapentry key111 valueaaa/entry key222 valuebbb//map/propertyproperty namestudentCarMapmapentry key-refstudent1 value-refcar1/entry key-refstudent2 value-refcar2//map/propertyproperty namepropertiespropsprop keyusernameroot/propprop keypassword123456/prop/props/property/bean这些其实都是固定写法
测试
Testpublic void DITest() {ApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);CarService carService (CarService) applicationContext.getBean(carService);// set方法注入System.out.println(carService);ListCar carList carService.findAll();for (Car car : carList) {System.out.print(car.getCarName() \t);}System.out.println();// 构造方法注入Car car1 (Car) applicationContext.getBean(car1);System.out.println(car1);// 数组集合ListSetMapProperties注入CollectionBean collectionBean (CollectionBean) applicationContext.getBean(collectionBean);System.out.println(collectionBean);}通过注解注入
依赖注入常用的注解 Value 用于注入普通类型Stringintdouble等类型 Autowired 默认按类型进行自动装配引用类型 Qualifier 和Autowired一起使用强制使用名称注入 Resource Java提供的注解也被支持。使用name属性按名称注入 对象生命周期作用范围注解 Scope 生命周期注解取值singleton默认值单实例和prototype多例 初始化方法和销毁方法注解了解 PostConstruct 相当于init-method PreDestroy 相当于destroy-method
Service
Qualifier(studentServiceImpl)
public class StudentServiceImpl implements StudentService {public void listAll() {System.out.println(studentService2);}
}
Service
Qualifier(studentServiceImpl2)
public class StudentServiceImpl2 implements StudentService {public void listAll() {System.out.println(studentService2);}
}
Configuration
ComponentScan(com.qcby)
Import({SpringConfig2.class})// 多配置文件
public class SpringConfig {}Testpublic void qualifierTest() {ApplicationContext applicationContext new AnnotationConfigApplicationContext(SpringConfig.class);
// ApplicationContext applicationContext
// new ClassPathXmlApplicationContext(applicationContext_anno.xml);
/* ApplicationContext applicationContext new FileSystemXmlApplicationContext(D:\\6_WorkSpace\\shiXun\\spring01\\src\\main\\resources\\applicationContext_anno.xml);*/StudentService studentService (StudentService) applicationContext.getBean(studentServiceImpl);StudentService studentService2 (StudentService) applicationContext.getBean(studentServiceImpl2);System.out.println(studentService);System.out.println(studentService2);studentService.listAll();studentService2.listAll();}纯注解不需要写applicationContext.xml文件 文章转载自: http://www.morning.tpmnq.cn.gov.cn.tpmnq.cn http://www.morning.hrzky.cn.gov.cn.hrzky.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.cbqqz.cn.gov.cn.cbqqz.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.xbxks.cn.gov.cn.xbxks.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.cwpny.cn.gov.cn.cwpny.cn http://www.morning.kbyp.cn.gov.cn.kbyp.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.npmx.cn.gov.cn.npmx.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.nxzsd.cn.gov.cn.nxzsd.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.tgmfg.cn.gov.cn.tgmfg.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.lndongguan.com.gov.cn.lndongguan.com http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn