初见spring

框架

3.png

spring

IOC AOP

配置文件


<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="iUserDao" class="com.wxb.dao.UserDaoImp">

</bean>
<bean id="iUservice" class="com.wxb.service.UserService">

</bean>

</beans>

IOC/DI

依赖注入(Dependecy Injection)

控制反转(Inversion of Control)

是同一个概念,具体的讲:
当某个角色需要另外一个角色协助的时候,在传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在spring中创建被调用者的工作不再由调用者来完成,因此称为控制反转。创建被调用者的工作由spring来完成,然后注入调用者因此也称为依赖注入。 spring以动态灵活的方式来管理对象 , 注入的两种方式,设置注入和构造注入。

实现方式

propety 方式实现 属性set方法

private  IUserDao dao;
public void setDao(IUserDao dao) {
        this.dao = dao;
    }
<property name="dao" ref="iUserDao"></property>

构造方式实现 加constructor-arg 有参构造方式

private  IUserDao dao;
    public UserService(IUserDao dao) {
        this.dao = dao;
    }
<constructor-arg>
<ref bean="iUserDao"/>
</constructor-arg>

p命名空间实现:p:dao-ref="iUserDao" set 方法


<bean id="iUservice" class="com.wxb.service.UserService" p:dao-ref="iUserDao" >

自动装配 *autowire="byName" autowire="byType" 建议使用byName

  • 局部
   <bean id="iUservice" class="com.wxb.service.UserService"   autowire="byName">
  • 全局
2.png

注意 前三种优先级高于自动装配

特殊字段 符号及集合 的处理方式

bean.png
bean.png
  • user
<bean id="user" class="com.wxb.bean.User">
    <property name="name" value="张三">
    </property>
    <property name="password" value="123">
    </property>
</bean>
  • strs
<property name="strs">
        <array>
            <value>金星</value>
            <value>火星</value>
            <value>木星</value>
            <value></value>
        </array>
    </property>
  • list
<property name="strs">
        <array>
            <value>金星</value>
            <value>火星</value>
            <value>木星</value>
            <value></value>
        </array>
    </property>
  • set
<property name="set">
        <set>
            <value>中国</value>
            <value>日本</value>
            <value>俄罗斯</value>
        </set>
    </property>
  • str
 <property name="str">
        <value><![CDATA[#$$%784582]]></value>
    </property>
  • pro
<property name="pro">
        <props>
            <prop key="北京">通州</prop>
            <prop key="杭州">西湖</prop>
            <prop key="上海">浦东</prop>
        </props>
    </property>
  • map
<property name="map">
        <map>
            <entry>
                <key>
                    <value>京</value></key>
                <value>北京</value>
            </entry>
            <entry>
                <key>
                    <value>浙</value></key>
                <value>浙江</value>
            </entry>
            <entry>
                <key>
                    <value>豫</value></key>
                <value>河南</value>
            </entry>
        </map>
    </property>

作用域

  • prototype 原型 每次创建新的实例对象 多用于control层
<bean id="user" class="com.wxb.bean.User"  scope="prototype">
  • singleton 单例 只创建一次 多用于service dao
 <bean id="user" class="com.wxb.bean.User"  scope="singleton">

注解

注解.png
注解.png
11.png
22.png
22.png

<!-- 自动扫描-->
    <context:component-scan base-package="com.wxb.dao,com.wxb.service">
    </context:component-scan>
33.png
33.png

此时自动装配的时候使用的是bytype 所以注解内可以不写

44.png

AOP 面向切面编程

核心思想:面向切面的编程思想

场景及作用:

  • <1> 统计某个方法的运行时间
  • <2> 增强操作日志
  • <3> 数据更新事物

AOP 编程思想理解

不在业务中写附加代码,而是单独写切面类;后期切面类和业务组装配置

xml配置文件设置

111.png
111.png
112.png
112.png
<!-- aop配置 -->
<aop:config>
    <aop:pointcut id="pointcut" expression="execution(public * com.wxb.service..*(..))" />
</aop:config>   

<!-- 切面功能配置,和业务点组装 -->
<aop:config>
<aop:aspect ref="demoUtil">
    <aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>

统计方法运行时间


@Component("demoUtil")
public class DemoUtil {
    
      private DemoUtil demoUtil;
      
        public void setDemoUtil(DemoUtil demoUtil) {
        this.demoUtil = demoUtil;
    }

        //环绕增强处理
        public Object around(ProceedingJoinPoint pjp){
            Object object=null;
        //开始时间
        Date  before=new Date();
        
        //调用方法
        try {
             object = pjp.proceed();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        //结束时间
        Date after=new Date();  
        //统计时间
        
        System.out.println("运行时间为:"+(after.getTime()-before.getTime()));
        return object;
        }
    
}


### 环绕增强处理 <aop:around>   pjp.proceed();//调用原方法

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

前置增强处理

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

最终增强处理 结论:无论异常与否,都能执行

<aop:after method="after" pointcut-ref="pointcut"/>
    

后置增强处理 条件:不出现异常 <aop:after-returning>

<aop:after-returning method="afterReturn" pointcut-ref="pointcut" returning="obj"/>

异常增强处理 用途:报错信息日志;<aop:after-throwing>


<aop:after-throwing method="afterThrow" pointcut-ref="pointcut"  throwing="exc"/>

Advisor增强处理

<aop:advisor advice-ref="logUtil" pointcut-ref="pointcut"/>

实现接口 MethodBeforeAdvice 实现接口方法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • 什么是Spring Spring是一个开源的Java EE开发框架。Spring框架的核心功能可以应用在任何Jav...
    jemmm阅读 16,568评论 1 133
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • 如下是整篇文章的结构,所需阅读时间大约20min Spring简介 Spring框架由Rod Johnson开发,...
    李序锴阅读 914评论 0 15
  • 现在有很多幼儿园的老师都被曝光虐待孩子,导致很多家长很是担心孩子在学校的状况。 我上小学的时候班主任是一个脾气极其...
    jckc阅读 300评论 0 0