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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zw</groupId>
  <artifactId>zw_Spring</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>zw_Spring Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
        <spring.version>5.0.1.RELEASE</spring.version>
        <javax.servlet.version>4.0.0</javax.servlet.version>
        <junit.version>4.12</junit.version>
    </properties>
    
  <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- 2、导入spring依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 5.1、junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- 5.2、servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
  <build>
    <finalName>zw_Spring</finalName>
    <plugins>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
    </plugins>
  </build>
</project>

模拟上传功能

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

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

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

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

导入spring的核心配置文件spring-context.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!--     本文件中配置整个项目中包含的所有的JavaBean,目的在于spring统一管理 -->
    <bean name="userBiz1" class="com.lv.ioc.biz.impl.UserBizImpl1"></bean>
    <bean name="userBiz2" class="com.lv.ioc.biz.impl.UserBizImpl2"></bean>
    <bean name="personAction" class="com.lv.ioc.web.PersonAction">
        <property name="userBiz" ref="userBiz2"></property>
    </bean>
    <bean name="userAction" class="com.lv.ioc.web.UserAction">
    <property name="userBiz" ref="userBiz1"></property>

</bean>
</beans>

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<String> 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<String> getHobby() {
        return hobby;
    }
    public void setHobby(List<String> 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中进行配置

<bean name="paramAction" class="com.lv.ioc.web.ParamAction">
    <property name="name" value="小小"></property>
    <property name="age" value="22"></property>
    <property name="hobby" >
        <list>
            <value>游戏</value>
            <value>手机</value>
            <value>电脑</value>
        </list>
    </property>
    </bean>

测试:

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<String> hobby;
    
    
    public ParamAction(int age, String name, List<String> 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中进行配置

<bean name="paramAction" class="com.lv.ioc.web.ParamAction">
        <constructor-arg name="name" value="小民和"></constructor-arg>
        <constructor-arg name="age" value="19"></constructor-arg>
        <constructor-arg name="hobby" >
            <list>
            <value>游戏</value>
            <value>手机</value>
            <value>电脑</value>
        </list>
        </constructor-arg>
    </bean>

 四、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中进行配置:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>
  
  <listener>
  <listener-class> com.lv.ioc.listener.SpringLoaderListener</listener-class>
 </listener>
</web-app>

在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();
    }
}

结果:监听器只执行一次

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值