spring详解

2 篇文章 0 订阅
2 篇文章 0 订阅

Spring(详解)

什么是spring?

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。

Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
它是一个容器框架,用来装javabean(java对象),中间层框架(万能胶)可以起一个连接作用,比如说把Struts和hibernate粘合在一起运用。简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

简单来说就是:spring 是地球母亲他可以融汇万物

1.什么是IOC

IOC是Inversion of Control的缩写,多数书籍翻译成“控制反转”。

1996年,Michael Mattson在一篇有关探讨面向对象框架的文章中,首先提出了IOC 这个概念。对于面向对象设计及编程的基本思想,前面我们已经讲了很多了,不再赘述,简单来说就是把复杂系统分解成相互合作的对象,这些对象类通过封装以后,内部实现对外部是透明的,从而降低了解决问题的复杂度,而且可以灵活地被重用和扩展。

控制反转(依赖注入)

        //通过工厂的指定流水线来生产
//        Dog d = (Dog) ax.getBean("dog");//IOC
//        Cat c = (Cat) ax.getBean("cat");//IOC
//        System.out.println(d.getDid());
//        System.out.println(d.getDname());
//        System.out.println(c.getCid());
//        System.out.println(c.getCname());
        Master m=(Master) ax.getBean("master");
        System.out.println(m.getC().getCid()+"~"+m.getD().getDid());

Set注入

  <bean id="dog" class="entity.Dog">
        <property name="did" value="1"></property> <!--依赖注入-->
        <property name="dname" value="陈狗1"></property>
    </bean>
    <bean id="cat" class="entity.Cat">
        <property name="cid" value="2"></property> <!--依赖注入-->
        <property name="cname" value="陈狗2"></property>
    </bean>

    <bean id="master" class="entity.Master">
        <property name="d" ref="dog">
        </property>
        <property name="c" ref="cat">
        </property>
    </bean>

构造器注入

需要生成构造方法

    <bean id="dog" class="entity.Dog">
        <constructor-arg name="did" value="1"></constructor-arg>
        <constructor-arg name="dname" value="傻逼"></constructor-arg>
    </bean>
    <bean id="cat" class="entity.Cat">
        <constructor-arg name="cid" value="1"></constructor-arg>
        <constructor-arg name="cname" value="陈狗"></constructor-arg>
    </bean>

p标签注入

    <!--使用 p标签需要在头部添加一个   xmlns:p="http://www.springframework.org/schema/p"标签-->
    <bean id="dog" class="entity.Dog" p:did="1" p:dname="小黑">
    </bean>

p标签注入对象

<bean id="master" class="entity.Master" p:c-ref="cat" p:d-ref="dog">

</bean>

所有注入方式

beans.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="Address">
        <property name="address" value="我的"/>


    </bean>

    <bean id="student" class="Student">
        <!--第一张 普通值注入,value -->

        <property name="name" value="申剑"/>
        <!--第二种,bean注入,ref -->

        <property name="address" ref="address"/>
        <!--数组 -->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>

            </array>
        </property>
        <!--list -->

        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
                <value>杀人</value>
            </list>
        </property>
        <!--map -->

        <property name="card">
            <map>
                <entry key="身份证" value="1313213211321232"></entry>
                <entry key="银行卡" value="132132132132"></entry>
            </map>
        </property>
        <!--set -->

        <property name="games">
            <set>
                <value>lol</value>
                <value>cf</value>
                <value>coc</value>
            </set>
        </property>
        <!--null -->

        <property name="wife">
            <null/>

        </property>
        <!--propertyies -->

        <property name="info">
            <props>
                <prop key="driver">20136</prop>
                <prop key="url">打完</prop>
                <prop key="username">root</prop>
                <prop key="password">12345</prop>
            </props>

        </property>
    </bean>


</beans>

2.AOP

AOP全称(Aspect Oriented Programming)面向切片编程的简称

AOP的定义:

AOP通过预编译方式和运行期动态代理实现,在不修改源代码的情况下,给程序动态统一添加功能的一种技术,简称AOP。是spring框架的一个重要内容,是OOP(面向对象编程)的衍生模范。

AOP的作用:

利用AOP对业务逻辑的各个部分进行隔离,降低业务逻辑的耦合性,提高程序的可重用型和开发效率

主要用于对同一对象层次的公用行为建模
简单来说就是:当你要卖腰子买苹果13 (13香) 首先你得把腰子割下来 要先找到地方然后将他割下来然后卖了几天 终于

如愿买了一个13 用了几天发现并不好 发现最近身体不行了,你又想买回来腰子 那你得又去医院将腰子买回来

又从新植入进去,总而言之可以前植入后植入提高程序的可重用型和开发效率

面向切面

代码实现

applicationContext.xml

    <bean id="student" class="entity.Student" >
    </bean>

    <bean id="aop" class="aop.MyAop">


    </bean>
    <aop:config>
        <!-- 找到切点  -->
        <aop:pointcut id="pc" expression="execution(public int study())"/>
            <!--通过切点 打开切面 -->

        <aop:aspect ref="aop">
            <aop:before method="washFace" pointcut-ref="pc"/>
                <!-- 如果要返回值就要加这个after-returning -->
            <aop:after-returning method="eat" pointcut-ref="pc" returning="result"/>
        </aop:aspect>
    </aop:config>

前增强

 <aop:before method="washFace" pointcut-ref="pc"/>

后增强

<aop:after-returning method="eat" pointcut-ref="pc" returning="result"/>

环绕增强

<aop:around method="around" pointcut-ref="pc"/>

最终增强

<aop:before method="myFinal" pointcut-ref="pc"/>

异常增强

报错执行

<aop:after-throwing method="error" pointcut-ref="pc" throwing="e"/>

    public void error(Exception e){
        System.out.println("我是异常增强!报错就有我");
    }

实体类

package entity;

import org.aspectj.lang.JoinPoint;
/***
原本有的
*/
public class Student {

    public  int  study(){
        System.out.println("学生在学习");
        return 1;
    }

}

aop

package aop;

import org.aspectj.lang.JoinPoint;

/***
 * 后续要植入的方法
 */
public class MyAop {

    public void washFace(JoinPoint joinPoint) {
        System.out.println("学生先洗脸");
    }

    public void eat(JoinPoint joinPoint,Object result) {
        System.out.println(result);
        System.out.println("学生学习完要吃饭");
    }
}

Java反射

什么是反射?

 反射就是利用java的一种机制 运行时所有的类都会加载到jvm虚拟机 然后我们可以通过直接获取加载好的类 得到对象
        Class<Car> c = Car.class;
        Car s = null;
        try {
//            所有java的类 都会被加载到java的虚拟机
//            得到类有三种方式

            Class<Student> c1 = (Class<Student>) Class.forName("reflect.Student");

            Class<Student> c2 = (Class<Student>) new Student().getClass();
            Field[] fields = c.getDeclaredFields();
//            通过反射得到加载好的类 再通过类得到对象
            s = c.newInstance();

//直接获取类中所有可见的属性 Field[] fields=c.getFields();
//直接获取类中所有的属性 Field[] fields=c.getDeclaredFields()
            for (Field f : fields
            ) {
                if (f.getName().equals("cname")) {
                    f.setAccessible(true); 
                  
                    f.set(s, "123");//为cname赋值
                }else {
                    f.setAccessible(true);
                    f.set(s, 1);

                }
            }


            //______________获取方法______
            Method[] methods = c.getMethods();
            for (Method ms :     methods
            ) {
                System.out.println(ms.getName());
            }
            c.getDeclaredMethod("eat");
            //--------------获取构造方法------
//            Constructor[] constructors = c.getConstructors();
//            for (Constructor cc : constructors
//            ) {
//                cc.setAccessible(true);
//                cc.getName();
//
//            }

            System.out.println(c.getName());


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Bean的作用域

六大作用域:singleton、prototype、request、session、application、websocket

1.单例模式(spring默认机制)

<bean id="s" class="引用对象" 	c:age="11" c:name="申" scope="singleton" />

2.原型模式:每次从容器中get的时候,都会产生一个新对象

<bean id="s" class="引用对象" 	c:age="11" c:name="申" scope="prototype" />

3.其余的request、session、application、这些只能在web中开发使用到!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值