Spring入门

Spring是一个轻量级开源的框架,它的核心是 Ioc(控制反转)DI(依赖注入)AOP(面向切面编程) 。它的优点是方便解耦,简化开发、AOP编程的支持、声明式事务的支持、方便程序的测试。方便继承各种优秀的框架、降低JavaEE API的使用难度。核心容器:Beans、Core、Context、Expression

Spring分层

Web层:Struts,Spring-MVC
Service:Spring
Dao层:Hibernate,Mybaits,jdbcTemplate

Spring之Ioc

Ioc(Inversion of Control) 就是控制反转,将我们的控制权交给了Spring

  • 1、需要以下jar包添加到我们的Web工程的lib中。也就是4个核心的jar

    • com.springsource.org.apache.commons.logging-1.1.1.jar
    • spring-beans-3.2.0.RELEASE
    • spring-context-3.2.0.RELEASE
    • spring-core-3.2.0.RELEASE
    • spring-expression-3.2.0.RELEASE
  • 2、工程src下新建一个app-spring.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">
    <!-- 配置Service
        <bean>:配置需要创建的对象
            id:用于之后从Spring容器中获取容器实例时使用
            class:需要创建实例的全限定类名 -->
    <bean id="userServiceId" class="com.wj.ioc.UserServiceImpl"></bean>
    </beans>
    
  • 3、写一个UserService接口和一个实现类

    UserService

    package com.wj.ioc;
    
    public interface UserService {
    
    public void addUser();
    }

    UserServiceImpl

    package com.wj.ioc;
    
    public class UserServiceImpl implements UserService{
    
    public void addUser() {
        System.out.println("addUser执行");
    }
    
    }
  • 4、测试

    package com.wj.ioc;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestIoc {
    
    @Test
    public void test1(){
        //不使用Spring的模式
        UserService service=new UserServiceImpl();
        service.addUser();
    }
    
    @Test
    public void test2(){
        //Spring的方式
        String xmlPath="com/wj/ioc/app-spring.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
        UserService service=(UserService) applicationContext.getBean("userServiceId");
        service.addUser();
    }
    }
    

    总结:通过xml配置Service的实现类,并且定义全路径以及id,然后在使用的时候无需使用new关键字创建Service,而是通过Spring的方式查找id拿到对象

Spring之DI

DI(Dependency Injection) 依赖注入

  • 1、同样需要以上的jar包,然后新建一个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">
    
    <!-- 创建Service -->
    <bean id="bookServiceId" class="com.wj.di.BookServiceImpl">
        <!--property属性注入  
            name:bean的属性名,通过setter方法获得
            ref:另一个bean的id值得引用
        -->
        <property name="bookDao" ref="bookDaoId"></property>
    </bean>
    
    <!-- 创建Dao实例-->
    <bean id="bookDaoId" class="com.wj.di.BookDaoImpl"></bean>
    </beans>
  • 2、写一个BookService接口和一个实现类、以及BookDao和实现类

    package com.wj.di;
    
    public interface BookDao {
    
    public void addBook();
    }
    package com.wj.di;
    
    public class BookDaoImpl implements BookDao{
    
    public void addBook() {
        System.out.println("DI addBook");
    
    }
    
    }
    package com.wj.di;
    
    public interface BookService {
    
    public void addBook();
    }
    
    package com.wj.di;
    
    public class BookServiceImpl implements BookService{
    
    //方式1:接口=实现类
    //    private BookDao bookDao=new BookDaoImpl();
    //方式2:接口+setter
    private BookDao bookDao;
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }
    
    public void addBook() {
        bookDao.addBook();
    }
    }
  • 测试

    package com.wj.di;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestDI {
    
    @Test
    public void test2(){
        //Spring的方式
        String xmlPath="com/wj/di/app-spring.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
        BookService bookService=(BookService) applicationContext.getBean("bookServiceId");
        bookService.addBook();
    }
    }
    

    总结:在Service的实现类中操作时,以前都是需要new一个Dao的。而使用Spring,就只需要在xml中配置Service中需要的Dao(使用property标签配置,然后就会创建),通过setterDao方法设置了值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值