一、spring的基本认识

Spring的认识

Spring是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用

Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架

  • 一站式框架,指的是spring为JavaEE开发的每一层都提供了支持
    • web层:SpringMVC
    • service层:Spring的bean管理、声明式事务管理
    • dao层:jdbc模板、ORM模块

Spring的优点

  • 方便解耦,简化开发 
  • 支持AOP编程
  • 支持声明式事务
  • 便于程序测试,支持junit单元测试
  • 方便集成各种框架
  • 降低JavaEE API的使用难度

Spring的组成结构

Spring的环境搭建

  1. 下载开发包(http://repo.spring.io/release/org/springframework/spring/
  2. 解压Spring的开发包
    1. docs:Spring的开发规范和API

      libs:Spring开发需要的jar和源码

      schema:Spring配置文件的约束

  3. 创建web项目,引入jar
    1. Spring的Core Container中包含beans、core、context、SpEL四个模块,所以Spring项目必须使用的四个jar
      1. spring-beans-4.2.4.RELEASE.jar
      2. spring-context-4.2.4.RELEASE.jar
      3. spring-core-4.2.4.RELEASE.jar
      4. spring-expression-4.2.4.RELEASE.jar  
    2. 基本的项目也需要日志记录,spring日志相关的jar
      1. com.springsource.org.apache.commons.logging-1.1.1.jar
      2. com.springsource.org.apache.log4j-1.2.15.jar  
  4. 创建一个简单的实体类、一个测试类
    1. 实体类
       1 package com.qf.demo;
       2 
       3 public class User {
       4 
       5     private Long id;
       6     private String name;
       7     private Integer age;
       8     
       9     public void setId(Long id) {
      10         this.id = id;
      11     }
      12     public void setName(String name) {
      13         this.name = name;
      14     }
      15     public void setAge(Integer age) {
      16         this.age = age;
      17     }
      18     
      19     @Override
      20     public String toString() {
      21         return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
      22     }
      23     
      24     public User(Long id, String name, Integer age) {
      25         super();
      26         this.id = id;
      27         this.name = name;
      28         this.age = age;
      29     }
      30     public User() {
      31         super();
      32     }
      33     
      34 }
      View Code
    2. 测试类
      public class TestDemo {
      
      	@Test
      	public void test() {
      		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      		User user = (User) context.getBean("user");
      		System.out.println(user);
      	}
      }
  5. 编写配置文件
    1. applicationContext.xml
      <?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 id="user" class="com.qf.demo.User">
      		<property name="id" value="1"/>
      		<property name="name" value="qf"/>
      		<property name="age" value="18"/>
      	</bean>
      
      </beans>
    2. log4j.properties
      ### direct log messages to stdout ###
      log4j.appender.stdout=org.apache.log4j.ConsoleAppender
      log4j.appender.stdout.Target=System.err
      log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
      log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
      
      ### direct messages to file mylog.log ###
      log4j.appender.file=org.apache.log4j.FileAppender
      log4j.appender.file.File=c:mylog.log
      log4j.appender.file.layout=org.apache.log4j.PatternLayout
      log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
      
      ### set log levels - for more verbose logging change 'info' to 'debug' ###
      # error warn info debug trace
      log4j.rootLogger= info, stdout
  6. 测试类中运行测试
    14:53:19,995  INFO ClassPathXmlApplicationContext:578 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5dbe6544: startup date [Tue Jan 08 14:53:19 GMT+08:00 2019]; root of context hierarchy
    14:53:20,039  INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [applicationContext.xml]
    User [id=1, name=qf, age=18]
    

      

原文地址:https://www.cnblogs.com/qf123/p/10239033.html