[自主学习一:Spring]day01_MySpring_CoreSpring

1、根据jar包需要,导入spring核心jar包。

2、编写spring的context上下文环境

<?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 definitions here -->

</beans>

3、编写java类

3.1、编写POJO类

package com.corespring.core;

public class UserInfo {
    private String userName;
    private String userPass;
    private String empno;

    public UserInfo(){
        System.out.println("调用空参构造函数");
    }

    public UserInfo(String userName) {
        super();
        this.userName = userName;
    }

    public UserInfo(String userName, String userPass) {
        super();
        this.userName = userName;
        this.userPass = userPass;
        System.out.println("调用-2-个参数的构造函数");
    }

    public UserInfo(String userName, String userPass, String empno) {
        super();
        this.userName = userName;
        this.userPass = userPass;
        this.empno = empno;
        System.out.println("调用--3--个参数的构造函数");
    }

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPass() {
        return userPass;
    }
    public void setUserPass(String userPass) {
        this.userPass = userPass;
    }

    public String getEmpno() {
        return empno;
    }

    public void setEmpno(String empno) {
        this.empno = empno;
    }
}

3.2、编写service层:

接口:

package com.corespring.core.biz;

import java.util.List;

import com.corespring.core.UserInfo;

public interface IUserBiz {
    public abstract List<UserInfo> findList();//接口里面的方法都是抽象方法,但修饰符public abstract是默认的,所以也可以省略不写
}

该接口的实现类:

package com.corespring.core.biz.impl;

import java.util.List;

import com.corespring.core.UserInfo;
import com.corespring.core.biz.IUserBiz;
import com.corespring.core.dao.IUserDao;

public class UserBizImpl implements IUserBiz{
    public UserBizImpl(){
        System.out.println("调用UserBizImpl---空参--构造函数..........");
    }

    private IUserDao userDao;
    public UserBizImpl(IUserDao userDao) {
        super();
        this.userDao = userDao;
        System.out.println("调用UserBizImpl--一个参数--构造函数");
    }

    @Override
    public List<UserInfo> findList() {
        return userDao.findAll();
    }

}

3.3、编写dao层:

接口:

package com.corespring.core.dao;

import java.util.List;

import com.corespring.core.UserInfo;

public interface IUserDao {
     public abstract List<UserInfo> findAll();//接口方法前面的修饰符默认为:public abstract,所以不写这两个单词也行
}

该接口的实现类:

package com.corespring.core.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.corespring.core.UserInfo;
import com.corespring.core.dao.IUserDao;

public class UserDaoImpl implements IUserDao{

    public UserDaoImpl(){
        System.out.println("UserDaoImpl构造函数");
    }

    @Override
    public List<UserInfo> findAll() {
        List<UserInfo> list=new ArrayList<UserInfo>();
        UserInfo  userInfo=null;
        for(int i=0;i<10;i++)
        {
            userInfo=new UserInfo("user"+i,"pass"+i);
            list.add(userInfo);
        }
        return list;
    }

}

4、配置spring上下文环境:

<?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 definitions here -->

<!--biz层(服务层)  -->
<bean id="UserBizImpl" class="com.corespring.core.biz.impl.UserBizImpl">
 <!--进行依赖注入时,可以使用value或ref进行注入;ref配置的是其他bean的id值 (即其他由spring管理的类),可以理解为一个引用,就是引用其他bean的id -->
 <!--当使用ref进行依赖注入时,会先实例化引用对象后,才实例化本类  -->
 <!--因为我们在biz层添加了一个构造函数,用于传递dao层的接口对象,所以,我们要在这里使用构造函数依赖注入的方式,将dao层接口对象传过去  -->
  <constructor-arg name="userDao" ref="UserDaoImpl"></constructor-arg>
</bean>
<!--dao层  -->
<bean id="UserDaoImpl" class="com.corespring.core.dao.impl.UserDaoImpl">
</bean>

<!--配置一个 bean  用于初始化javabean的值,初始化的过程,称为依赖注入(采用的是,使用构造函数初始化值),如果没有进行值注入,则没有进行初始化,注入值的方法一:<constructor-arg value="nihao"></constructor-arg>    -->
<!--id值 可以任意命名,但不能重名;这个   class 是  想要初始化那个类的引用-->
<bean id="UserInfo" class="com.corespring.core.UserInfo">
    <!--constructor-arg value="abc" 为构造函数初始化默认值 ,它的属性  name 表示指定为某个属性初始化值 -->
    <!--使用构造函数进行依注入:
      1、能够跟根据构造参数的个数自动匹配类的构造函数。
      2、可以不指定参数的名称,此时,会根据类的参数列表顺序进行注入
      3、可以全部或部分指定参数名称(使用name属性),此时会跟根据参数名称进行注入
      4、如果使用了参数名称 ( name属性),则参数的配置顺序可以任意颠倒,也就是说,如果没有指定参数名称,则这里的配置顺序,将会对应构造函数那边的书写顺序
      5、配置的参数个数一定要与构造函数匹配
      6、如果在这里配置了初始值,而该初始值没有相应的构造函数,那么是不能实例化成功的,运行时,会出错
      7、这个  bean 可以多次配置,只要id不重复,就可以了
    -->
    <constructor-arg name="empno" value="abc"></constructor-arg>
    <constructor-arg  value="123456"></constructor-arg>
    <constructor-arg value="nihao"></constructor-arg>
</bean>

<!--依赖注入方法二 ,因为这个并没有构造函数的值,所以在实例化这个类的时候,它会调用空参的构造函数,(一个类,实例化的时候,构造函数一定会被执行) -->
<bean id="UserInfo1" class="com.corespring.core.UserInfo">
<!--在使用spring管理对象时,还可以使用set方法进行依赖注入  -->
<!--使用property配置依赖关系时,name属性是必须要配置的  -->
<property name="UserName" value="敏"></property>
<!--在配置property的name时,配置的是set方法后面的名称,而不是类的属性名称  -->
<property name="empno" value="1001"></property>
</bean>
</beans>

5、编写测试类

package test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.corespring.core.UserInfo;
import com.corespring.core.biz.impl.UserBizImpl;

public class TestUserBiz {

    public static void main(String[] args) {
        //1.初始化spring上下文环境
        String config="applicationcontext.xml";
        ApplicationContext ctx=new ClassPathXmlApplicationContext(config);
        //2、通过spring上下文环境,调用spring的bean factory实例化对象
        /*其实这里可以直接转成父接口,如:IUserBiz userBiz = (IUserBiz) ctx.getBean("UserBizImpl");
             它能直接转成父接口,这就是接口隔离原则(接口的调用类不关心接口的实现类而只关心接口方法)
        */
        UserBizImpl userBizimpl=(UserBizImpl) ctx.getBean("UserBizImpl");
        System.out.println("调用findlist方法前..........");
        //3.调用实例对象,实现业务功能
        List<UserInfo> list=userBizimpl.findList();
        for (UserInfo userInfo : list) {
            System.out.println(userInfo.getUserName()+"----"+userInfo.getUserPass());
        }
    }
}

注意看看下面这个类:

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.corespring.core.UserInfo;

public class TestUserInfo {
    //ApplicationContext  ctx;      /*如果测试方法 testSpring2() 时,把这个环境定义为全局*/
    //传统的方法
    private static void test(){
        UserInfo userInfo=new UserInfo();
        System.out.println(userInfo.getUserPass());
    }

    public static void main(String[] args) {
         // test();
        testSpring();
//        testSpring2();//如果把这个方法放到方法testSpring(); 的前面 ,是不行的,运行出错,因为没有初始化上下文环境
    }

    //使用Spring方法
    private static void testSpring(){
        UserInfo userInfo=null;

        System.out.println("-----1-----");//打印这些数字,只是为了查看spring的生命周期

        //1。初始化spring上下文环境;因为它要预先取读取有多少个需要实例化的变量。在初始化上下文环境的时候,
        //applicationcontext.xml 里面的内容都会被执行
          String config="applicationcontext.xml";
          ApplicationContext  ctx=new ClassPathXmlApplicationContext(config);

          System.out.println("------2------");

        //2.通过spring上下文环境,调用spring的bean factory 实例化对象
          userInfo=(UserInfo)ctx.getBean("UserInfo1");//UserInfo 这个是applicationcontext.xml bean 的id.
        //3.调用实例化后的对象,实现业务功能
          System.out.println("-----3----");
          System.out.println(userInfo.getUserName());
          System.out.println(userInfo.getUserPass());
          System.out.println(userInfo.getEmpno());

    }

//    private static void testSpring2(){
//        UserInfo userInfo=null;
//          userInfo=(UserInfo)ctx.getBean("UserInfo1");//UserInfo 这个是applicationcontext.xml bean 的id.
//          System.out.println("-----3----");
//          System.out.println(userInfo.getUserName());
//          System.out.println(userInfo.getUserPass());
//          System.out.println(userInfo.getEmpno());
//
//    }

}

好了,到此简单的spring学习完毕,下面附上开发步骤:

Spring开发步骤:

1、新建Java项目
2、把spring的lib库的核心jar包加入到项目的libs中:
  spring-beans-4.1.7.RELEASE.jar
  spring-context-4.1.7.RELEASE.jar
  spring-core-4.1.7.RELEASE.jar
  spring-expression-4.1.7.RELEASE.jar

     而且还要添加日志文件:commons-logging-1.1.3.jar;
     这个日志文件在:文件夹ssh\spring-framework-4.0.0-dependencies\files-2.1\commons-logging\commons-logging\1.1.3\f6f66e966c70a83ffbdb6f17a0919eaf7c8aca7f

3、添加spring的context上下文环境
       即是写: applicationcontext.xml文件;这个文件的内容在第七章,标题为:34.2 XML Schema-based configuration 

  <?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 definitions here -->

  </beans>

4、编写java类,并交给spring进行管理
<?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 definitions here -->
<bean id="UserInfo" class="com.corespring.core.UserInfo">
</beans>

5、编写测试类,通过spring bean factory 进行实例化
   private static void testSpring(){
        UserInfo userInfo=null;
        //1。初始化spring上下文环境;因为它要预先取读取有多少个需要实例化的变量
          String config="applicationcontext.xml";
          ApplicationContext  ctx=new ClassPathXmlApplicationContext(config);

        //2.通过spring上下文环境,调用spring的bean factory 实例化对象
          userInfo=(UserInfo)ctx.getBean("UserInfo");//UserInfo 这个是applicationcontext.xml bean 的id.
        //3.调用实例化后的对象,实现业务功能
          System.out.println(userInfo.getUserPass());
   }

------------------------------
理解spring的生命周期:
1、spring的使用过程有3个步骤
   (1)。初始化spring上下文环境
   (2)。通过spring上下文环境,调用spring的bean factory实例化对象;
   (3).调用实例化后的对象,实现业务功能

2、当初始化spring环境时,会对配置到spring配置文件中的类进行实例化;
3、当通过bean factory取得实例化对象时,其实是返回实例对象的引用
4、当ApplicationContext对象引用失效时,spring的环境同时失效

------------------------------------------------------------------------------------------------------------------
1、配置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 definitions here -->

  </beans>
 

Spring开发使用三层架构:

1、采用面向接口编程思想

2、编写各层次代码:三层结构(视图层、业务层、数据访问层)
      这里的项目使用两个接口、两个接口的实现类

3、把接口实现类交给spring进行管理
   <!--biz层  -->
   <bean id="UserBizImpl" class="com.corespring.core.biz.impl.UserBizImpl">
   <!--进行依赖注入时,可以使用value或ref进行注入;ref配置的是其他bean的id值 (即其他由spring管理的类),可以理解为一个引用,就是引用其他bean的id -->
   <!--当使用ref进行依赖注入时,会先实例化引用对象后,才实例化本类  -->
    <!--因为我们在biz层添加了一个构造函数,用于传递dao层的接口对象,所以,我们要在这里使用构造函数依赖注入的方式,将dao层接口对象传过去  -->
   <constructor-arg name="userDao" ref="UserDaoImpl"></constructor-arg>
   </bean>
   <!--dao层  -->
   <bean id="UserDaoImpl" class="com.corespring.core.dao.impl.UserDaoImpl">
   </bean> 

4、编写测试类(视图层),使用spring框架功能
      public static void main(String[] args) {
        //1.初始化spring上下文环境
        String config="applicationcontext.xml";
        ApplicationContext ctx=new ClassPathXmlApplicationContext(config);
        //2、通过spring上下文环境,调用spring的bean factory实例化对象
        /*其实这里可以直接转成父接口,如:IUserBiz userBiz = (IUserBiz) ctx.getBean("UserBizImpl");
             它能直接转成父接口,这就是接口隔离原则(接口的调用类不关心接口的实现类而只关心接口方法)
        */
        UserBizImpl userBizimpl=(UserBizImpl) ctx.getBean("UserBizImpl");
        System.out.println("调用findlist方法前..........");
        //3.调用实例对象,实现业务功能
        List<UserInfo> list=userBizimpl.findList();
        for (UserInfo userInfo : list) {
            System.out.println(userInfo.getUserName()+"----"+userInfo.getUserPass());
        }
    }   

附上项目结构图:

时间: 2025-01-04 14:29:13

[自主学习一:Spring]day01_MySpring_CoreSpring的相关文章

spring学习之——Spring推荐文章

        spring 2.5学习过程中,看的是传智播客视频,跟着练习一些小实验,实验是做出来了,可是总觉得不对劲,不是很理解spring的林林总总,就看去Google查看资料.硬着头皮看了很多篇文章博客,看懂一些也有些看不懂.在这里就推荐几篇对我有帮助的.关于spring的文章,希望帮助那些和我一样在自主学习spring过程中遇上困难的同学. 这一篇是IBM网站的,详细的介绍了spring的种种.Spring框架介绍.个人觉着这篇还是比较容易看懂的,就没什么好突出介绍了.        

Spring事务管理(详解+实例)

写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: Spring事务机制详解 Spring事务配置的五种方式 Spring中的事务管理实例详解 1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必须是要么都执行要么都

SSM整合(spring,spirngmvc,mybatis)

整合思路   准备环境:导入jar包(spring mybatis  dbcp连接池  mysql驱动包 log4j) 工程结构: --------------------------- 1.  整合dao mybatis和spring进行整合   applicationContext-dao.xml 配置: 1.数据源 2.SqlSessionFactory 3.mapper扫描器 创建po以及mapper(通过逆向工程,这里不再演示) 针对综合查询mapper,一般情况会有关联查询,建议自定

Spring Boot 热部署

需要在pom.xml文件中加如下代码: 1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-devtools</artifactId> 5 <optional>true</optional> 6 </dependency> 7 </depe

Spring多线程

Spring是通过TaskExecutor任务执行器来实现多线程和并发编程的.使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.而实际开发中任务一般是非阻碍的,即异步的,所以我们要在配置类中通过@EnableAsync开启对异步的支持,并通过在实际执行的Bean的方法中使用@Async注解来声明其是一个异步任务. 实例代码: (1)配置类 package com.lwh.highlight_spring4.ch3.taskexecutor; /**

Spring与JavaMail

JavaMail与Spring集成开发 spring框架集成JavaMail的主要包 2.mail.properties mail.smtp.host=smtp.163.com mail.smtp.auth=true mail.username=15511111111 mail.password=123 [email protected] 3.使用spring配置(applicationContext-mail.xml) <?xml version="1.0" encoding=

Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]56528192: startup date [Tue Sep 19 15:05:24 CST 2017]; root of context hierarchy 2017-09-19 15:05:24.858 INFO 9986 --

Spring rabbitMq 中 correlationId或CorrelationIdString 消费者获取为null的问题

问题 在用Spring boot 的 spring-boot-starter-amqp   快速启动 rabbitMq 是遇到了个坑 消费者端获取不到:correlationId或CorrelationIdString 问题产生的原因 correlationId 的在 spring rabbitmq 2.0 以后 byte方式会被放弃,所以 目前 代码中有些地方没有改过来,应该算一个BUG @SuppressWarnings("deprecation") public class De

Spring框架之Spring AOP

一.基于注解管理的AOP 1.Spring配置文件 <!-- 配置自动扫描包,自动扫描Bean组件,切面类 --> <context:component-scan base-package="com.zhoujian.spring.anno,com.zhoujian.spring.test"> <!-- <context:include-filter type="annotation" expression="org.a