栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring框架之ioc

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring框架之ioc

一、spring的基本了解

 

1.spring包含的模块

 2.spring全家桶

spring struts Hibernate

spring springmvc mybatis

springboot

SpringCloud

3.技术层面

安全技术方面:Shiro、springSecurity

数据库层面:hibernate/mybatis、SpringDataJpa

消息中间件:activityMQ、RabbitMQ、kaffka...

4.spring的含义及其目的、功能、范围

spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,spring的用途不仅限于服务器的开发。从简单性、可测试性和松耦合的角度而言,任何java应用都可以从spring中受益。

目的:解决企业应用开发的复杂性

功能:使用基本的JavaBean代替EJB,并提供了更多的企业应功能

范围:任何java应用

5.spring的使用

a.中间层框架、万能胶

struts2

spring

hibernte

b.容器框架

JavaBean        项目中的一个个类

IOC、AOP

二、IOC

含义

控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由控制器程序之间的(依赖)关系,而非传统现实中,由程序代码直接操控。这就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。

IoC还有一个另外的名字:“依赖注入(DI=Dependency Injection)”,即由容器动态的将某种依赖关系注入到组件之中

导入依赖 pom.xml


  4.0.0
  com.zw
  zw_Spring
  war
  0.0.1-SNAPSHOT
  zw_Spring Maven Webapp
  http://maven.apache.org
  
  
        5.0.1.RELEASE
        4.0.0
        4.12
    
    
  
        
            junit
            junit
            3.8.1
            test
        
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
        
            junit
            junit
            ${junit.version}
            test
        
        
        
            javax.servlet
            javax.servlet-api
            ${javax.servlet.version}
            provided
        
    
  
    zw_Spring
    
        
                org.apache.maven.plugins
                maven-compiler-plugin
                3.7.0
                
                    1.8
                    1.8
                    UTF-8
                
            
    
  

模拟上传功能

        按照以前的操做去实行,如果突然需要整改或升级

 如:1、限定上传文件大小    2、限定上传文件类别

 所遇到的麻烦:1、更新版本需要改动原有代码
                           2、相关调用此方法的模块伴随着巨大的风险

 使用Ioc操作:将以前由程序员实例化对象/赋值的工作交给了spring处理

导入spring的核心配置文件spring-context.xml:一般就在该文件中进行修改,解决了以前方法操作的麻烦





    
    
    
        
    
    
    



public class PersonAction {
    private UserBiz userBiz;
    
    public UserBiz getUserBiz() {
        return userBiz;
    }

    public void setUserBiz(UserBiz userBiz) {
        this.userBiz = userBiz;
    }

    public void upload() {
        userBiz.upload();
    }
    
    public static void main(String[] args) {
        PersonAction pa=new PersonAction();
        pa.upload();
    }
}
三、spring传参

给JavaBean赋初始化值,传参

方法:

1.set传参

2.构造传参

3.自动配置(基本不用)

1.set传参

za定义一个实体类ParamAction:

public class ParamAction {
    private int age;
    private String name;
    private List hobby;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List getHobby() {
        return hobby;
    }
    public void setHobby(List hobby) {
        this.hobby = hobby;
    }
    
    public void execute() {
        System.out.println(this.name);
        System.out.println(this.age);
        System.out.println(this.hobby);
    }

}

在spring-context.xml中进行配置


    
    
    
        
            游戏
            手机
            电脑
        
    
    

测试:

public class IocTest {
    
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("/spring-context.xml");
        ParamAction paramAction = (ParamAction) ac.getBean("paramAction");
        paramAction.execute();
                            }

}

2.构造传参

 给实体类ParamAction 添加有参无参的构造方法:

public class ParamAction {
    private int age;
    private String name;
    private List hobby;
    
    
    public ParamAction(int age, String name, List hobby) {
        super();
        this.age = age;
        this.name = name;
        this.hobby = hobby;
    }

    public ParamAction() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void execute() {
        System.out.println(this.name);
        System.out.println(this.age);
        System.out.println(this.hobby);
    }

}

 在spring-context.xml中进行配置


        
        
        
            
            游戏
            手机
            电脑
        
        
    

 四、spring与tomcat整合

功能:减少建模, 性能优化,减少服务器的压力

建立一个监听器:建模的过程直接放到监听器里面完成

public class SpringLoaderListener implements ServletContextListener{
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("监听器方法执行……");
        ApplicationContext ac=new ClassPathXmlApplicationContext("/spring-context.xml");
        sce.getServletContext().setAttribute("SpringContext", ac);
            }
     }

在web.xml中进行配置:


  Archetype Created Web Application
  
  
   com.lv.ioc.listener.SpringLoaderListener
 

在servlet中可以直接得到spring上下文,不需再次建模:

@WebServlet("/user")
public class UserServlet extends HttpServlet{
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext springContext = (ApplicationContext) req.getServletContext().getAttribute("SpringContext");
        ParamAction paramAction = (ParamAction) springContext.getBean("paramAction");
        paramAction.execute();
    }
}

结果:监听器只执行一次

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/353547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号