Spring快速入门 古城微笑少年丶 2022-07-13 05:07 113阅读 0赞 什么是Spring Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架 分层 SUN提供的EE的三层结构:web层、业务层、数据访问层(持久层/集成层) Struts2是web层基于MVC设计模式框架 Hibernate是持久层的一个ORM的框架 一站式 Spring框架有对三层的每层解决方案 web层:Spring MVC 持久层:JDBC Template 业务层:Spring的Bean管理 Spring的核心 IOC(Inverse of Control 控制反转):将对象的创建权,交由Spring完成 AOP(Aspect Oriented Programming): 是面向对象的功能延伸.不是替换面向对象,是用来解决OO中一些问题. Spring优点 方便解耦,简化开发 Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理 AOP编程的支持 Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能 声明式事务的支持 只需要通过配置就可以完成对事务的管理,而无需手动编程 方便程序的测试 Spring对Junit4支持,可以通过注解方便的测试Spring程序 方便集成各种优秀框架 Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持 降低JavaEE API的使用难度 Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低 Spring的入门的程序 下载Spring的开发包 spring-framework-3.2.0.RELEASE-dist.zip ---Spring开发包 docs :spring框架api和规范 libs :spring开发的jar包 schema :XML的约束文档. spring-framework-3.0.2.RELEASE-dependencies.zip ---Spring开发中的依赖包 创建web工程引入相应jar包 spring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-core-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE.jar 开发的日志记录的包: com.springsource.org.apache.commons.logging-1.1.1.jar --- 用于整合其他的日志的包(类似Hibernate中slf4j) com.springsource.org.apache.log4j-1.2.15.jar 创建Spring的配置文件 在src下创建一个applicationContext.xml 引入XML的约束:找到xsd-config.html.引入beans约束 <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"> 编写实现类 ![复制代码][copycode.gif] public class HelloServiceImpl implements HelloService { private String info; public void setInfo(String info) { this.info = info; } public void sayHello() { System.out.println("Hello Spring..."+info); } } ![复制代码][copycode.gif] 在配置中配置类 <!-- 通过一个<bean>标签设置类的信息,通过id属性为类起个标识. --> <bean id="userService" class="cn.yzu.spring3.demo1.HelloServiceImpl"> <!-- 使用<property>标签注入属性 --> <property name="info" value="你好"/> </bean> 创建测试类 ![复制代码][copycode.gif] @Test // Spring开发 public void demo2() { // 创建一个工厂类. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("userService"); helloService.sayHello(); } ![复制代码][copycode.gif] IOC和DI(\*\*\*\*\*)区别? IOC:控制反转:将对象的创建权,由Spring管理. DI:依赖注入:在Spring创建对象的过程中,把对象依赖的属性注入到类中. Spring框架加载配置文件 ApplicationContext 应用上下文,加载Spring 框架配置文件 加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml"); :加载classpath下面配置文件 加载磁盘路径:new FileSystemXmlApplicationContext("applicationContext.xml"); :加载磁盘下配置文件 BeanFactory与ApplicationContext区别? ApplicationContext类继承了BeanFactory BeanFactory在使用到这个类的时候,getBean()方法的时候才会加载这个类,ApplicationContext类加载配置文件的时候,创建所有的类 ApplicationContext对BeanFactory提供了扩展:国际化处理,事件传递,Bean自动装配,各种不同应用层的Context实现 早期开发使用BeanFactory,用例: ![复制代码][copycode.gif] @Test public void demo4(){ // ClassPathResource FileSystemResource BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("applicationContext.xml")); HelloService helloService = (HelloService) beanFactory.getBean("userService"); helloService.sayHello(); } ![复制代码][copycode.gif] MyEclipse配置XML提示 Window--->xml catalog--->add 找到schema的位置 ,将复制的路径 copy指定位置,选择schema location. [copycode.gif]: /images/20220713/be4d5fe40b8947738bf098ffeb2bbed1.png
还没有评论,来说两句吧...