SSM框架(Spring Spring MVC Mybatis)基础入门学习2——Spring入门实例

上次学习了servlet的基础知识还记得么,先来回顾一下吧:讲了servlet的访问流程,配置方法,以及原理。今天我们来学习spring的入门实例,分为3部分来学习。

1.Spring的基本知识:IoC(控制反转)和DI(依赖注入),AOP(面向切面的编程)

2.Spring常用注入方法的seter

3.Spring的自动扫描注入和自动装配


第一部分:Spring的基本知识

        网上到处都是,我一开始看得也是天花乱坠,还是要自己把第二部分的实例写一遍就都了解了。讲的太抽象!!我也大概讲一下自己作为一个新手的比较片面的认识:IoC和DI其实本质上是一个东西,Ioc是个很大的概念大概意思是对象控制权脱离程序向组件靠,DI是由容器负责装配对象,java方法提供依赖关系,它是IoC的一种形式。其实用大白话说就是一个意思:我要对象,为了省事不自己找了,自己找了对象还要自己维护。现在全交给IoC容器去办,我要什么对象只要清楚的告诉它,它就会给你创造出来。不用自己维护那先对象,极大的解决了各层之间对象耦合的问题。

     AOP面向切面的编程,由于本人知识水平有限觉得它就是一个功能很强大的过滤器。对于刚入行的我们用的不太多,只在编码校验时用一下,实例中并没有用这个功能。等上入门之后,再学这个吧。一口吃不成一个大胖子么。


第二步分:Spring常用注入方法的seter

        注入bean对象需要3步,第一准备好要注入的对象,第二设置注入关联关系,第三就是调用了。基本的注入方法有两种seter法和构造器法,这里我们用seter法,代码更加简单。废话不多说,代码贴出来大家看:


public class HelloWorldServicePre {
	
    private IHelloWorldDao iHelloWorld;//此接口下有2个业务的实现方法

    public void sayHello(){
    	iHelloWorld = new HelloWorldDaoOne();//每次调用时都必须在service层实例化Dao层的类,高耦合关系不利于后期维护
        String HelloWords=iHelloWorld.getHelloWords();
        iHelloWorld = new HelloWorldDaoTwo();
        HelloWords+=iHelloWorld.getHelloWords();
        System.out.println(HelloWords);
    }
}

采用Spring的IoC容器后,耦合关系降低:


package com.xdy.service;

import com.xdy.dao.IHelloWorldDao;

public class HelloWorldServiceIocSeter {
	// Field type HelloWorld
    private IHelloWorldDao helloWorld1;//声明需要用的两个Dao层的类,不用实例化,然后写出他们的get与set方法,供容器调用
    private IHelloWorldDao helloWorld2;
  
    
    public void sayHello(){
        String HelloWords=helloWorld1.getHelloWords();//容器根据helloWorld的set方法实例化该对象,不用我们去操作,是不是很省心
        if(null!=helloWorld2){
        	
        HelloWords+=helloWorld2.getHelloWords();
        }
        System.out.println(HelloWords);
    }


	public IHelloWorldDao getHelloWorld2() {
		return helloWorld2;
	}


	public void setHelloWorld2(IHelloWorldDao helloWorld2) {
		this.helloWorld2 = helloWorld2;
	}
    
	public IHelloWorldDao getHelloWorld1() {
		return helloWorld1;
	}

	public void setHelloWorld1(IHelloWorldDao helloWorld1) {
		this.helloWorld1 = helloWorld1;
	}

}


配置文件这样写:


<?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">
  <!--每个id对应一个bean类,在代码中直接取id就可以拿到对应的类的实体  -->
    <bean id= "helloWorldDaoOne"
        class="com.xdy.dao.HelloWorldDaoOne"></bean>
        
    <bean id="helloWorldDaoTwo"
        class="com.xdy.dao.HelloWorldDaoTwo"></bean> 
  
    <bean id="helloWorldServiceSeter1"
        class="com.xdy.service.HelloWorldServiceIocSeter">
<!--  property是一个属性,那么对应bean类中的一个成员,ref代表注入一个bean对象,下边这句话的意思是将helloWorldDaoOne对象注入到helloWorldServiceSeter1对象的helloWorld成员  ,property还有其他用法感兴趣的自己去学,这里我们只用到bean注入,就可以将Dao层和Service层的对象关联起来,是不是很方便,就不用在server层写太多代码了 -->
        <property name="helloWorld1" ref="helloWorldDaoOne"/>
    </bean>
    
    <bean id="helloWorldServiceSeter2"
        class="com.xdy.service.HelloWorldServiceIocSeter">
        <property name="helloWorld1" ref="helloWorldDaoOne"/>
        <property name="helloWorld2" ref="helloWorldDaoTwo"/>
    </bean>
  
</beans>

将server层的业务方法和Dao层的数据模型方法关联注入后,在控制层就可以直接调用:


package com.xdy.controler;

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

import com.xdy.service.HelloWorldServiceIocSeter;

 

public class HelloControlerSeter {

	public static void main(String [] args){
                 //获得容器上下文对象
		 ApplicationContext context =
                 new ClassPathXmlApplicationContext("config.xml");
          
		 System.out.println("......iocSeter1........");
                  //获得所需的bean对象
		 HelloWorldServiceIocSeter iocSeter =
              (HelloWorldServiceIocSeter) context.getBean("helloWorldServiceSeter1");
                 //调用对象方法
		 iocSeter.sayHello();
		 
		 System.out.println("..........iocSeter1.........");
		 iocSeter =
              (HelloWorldServiceIocSeter) context.getBean("helloWorldServiceSeter2");
		 iocSeter.sayHello();
		 
		
		 
	}
	
	
}

手动配置就讲完了,具体实例在下边会添出来,供大家免费下载!!!


第三部分:Spring的自动扫描注入和自动装配

         一个项目业务类可能成百上千个,一个一个配置bean,然后配置关联注入太繁琐。一般常用的就是这个了,扫描特定的包有注解的注入,扫描所有方法有注解装配!是不是很方便,先来看配置文档:


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 
     
<context:component-scan base-package = "com.xdy" >  
    <context:exclude-filter type="regex"  expression= "com/.xdy/.controler/..*"  />  
</context:component-scan>  
</beans>  


你没有看错,就这么多,自动扫描注册bean,自动扫描注入!以上代码将扫描com.xdy包下所有类(除controler包之外)的注解,注解的使用方式如下:


package com.xdy.dao;

import org.springframework.stereotype.Repository;

@Repository
public class HelloWorldDaoAuto  implements IHelloWorldDao  {
	   public String getHelloWords()  {
           return "One say Hello!";
   }
}

@Repository 注解说明它是资源型,为了代码可读性最好将Spring注解都使用比较详细的注解

package com.xdy.service;


import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xdy.dao.IHelloWorldDao;
@Service
public class HelloWorldServiceAuto {
	
	@Resource
    private IHelloWorldDao helloWorldAuto;
  
    
    public void sayHello(){
        String HelloWords=helloWorldAuto.getHelloWords();
        
        System.out.println(HelloWords);
    }


	
}

@service注解说明这个bean是service层的bean,注意成员有@Resource 注解,它的意思是此bean加载时要依赖注入helloWorldAuto的bean,我们上边刚好把它注册为资源型bean,所以IoC容器会找到他们的id是匹配的然后注入。这里注意,id是唯一标识,意味着所有注解的名称需要唯一,不同包下也不允许有同名bean的name,否则会报错!!!!入门实例用这么多就可以了,至于其他的注解,等大家入门之后再做研究吧。


主调用方法这样写:

package com.xdy.controler;

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

import com.xdy.service.HelloWorldServiceAuto;


public class HelloControlerAuto {

	public static void main(String [] args){
		 ApplicationContext context =
                 new ClassPathXmlApplicationContext("auto.xml");
          
		 System.out.println("......auto........");
		 HelloWorldServiceAuto auto =
              (HelloWorldServiceAuto) context.getBean("helloWorldServiceAuto");
		 auto.sayHello();
		
		 
	}
	
	
}

输出:......auto........
One say Hello!

这就是Spring在SSM中用的最多的功能了,它将帮助我们维护各层间bean类的关系,帮我们创建bean!AOP等入门以后,再一起学习吧!!!

下篇文章,我们一起学习Spring和Mybits结合应用吧,同样我也会写一个入门demo,与大家分享!

最后,免费下载地址:

http://download.csdn.net/detail/nwpumaster/9586022



  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值