zoukankan      html  css  js  c++  java
  • spring 总结

    控制反转(Inversion of Control)

    依赖注入(Dependency Injection)

    一、控制反转

    1.控制:传统的方式是由程序创建的,写死的, spring 是由spring管理创建对象,实现了解耦。

    2.反转:由主动创建变为被动接受,正转:程序创建对象。

    优点:比如两个实现类   mysql的实现,oracle的实现,如果不用控制反转,就需要程序写死,日后维护不方便,如果用控制反转,只需要修改配置文件,动态传入是mysql,oracle

    二、依赖注入:有spring容器来装配对象

    set注入,构造方法注入,数组注入,集合注入,配置文件properties注入,命名空间,P,C注入

    控制反转和依赖注入主要是为了实现多态,实现程序解耦,可扩展

    IOC  配置文件方式

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
            
            <bean id="user1" class="com.model.User">
               <property name="name" value="张三"></property>
               <!-- 引用外部对象注入 -->
               <property name="car" ref="car"></property>
            </bean>
            
            <bean id="car" class="com.model.Car"></bean>
            <!-- 注入特殊字符 -->
            <bean id="user2" class="com.model.User">
              <constructor-arg index="0">
                 <value type="java.lang.String">
                   <![CDATA[<SAFASFD&&&^^^>]]>
                 </value>
              </constructor-arg>
              
              <!-- 在当前属性中注入  外部不能引用 -->
              <property name="car">
                <bean class="com.model.Car"></bean>
              </property>
            </bean>
            
            <bean class="com.process.MyBeanPostProcess"></bean>
    <!-- 引用资源文件用SEPL获取${jdbc_url} --> <!-- <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> --> </beans>

    2注解方式

    <!-- 不用默认的扫描  用自定义的扫描,扫描多个包用,base-package="com.controller,com.service"  可以放多个context:include-filter或context:include-filter-->
        <!-- <context:component-scan base-package="com.controller" resource-pattern="" use-default-filters="false">
          <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
          <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        </context:component-scan> -->
        
        <!-- 对应的整合spring的配置配置文件不扫描springmvc的注解,为了避免重复扫描创建两个对象 -->
        <!-- <context:component-scan base-package="com.controller">
          <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
          <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        </context:component-scan> -->
        
        <!-- resource-pattern="/service/*.class   只扫描service下的加注解的类 -->
        <!-- <context:component-scan base-package="com.user" resource-pattern="/service/*.class"></context:component-scan> -->
        <context:component-scan base-package="com.base" ></context:component-scan>
    • @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
    • @Service 通常作用在业务层,但是目前该功能与 @Component 相同。
    • @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。
    • @Repository 通常作用于持久层
    @Service("userService")//不写值  默认注入的名称是首字母小写userAddServiceImpl
    public class UserAddServiceImpl implements UserService{
    
        public void add() {
            System.out.println("UserAddServiceImpl--------添加");
        }
    
    }

        @Autowired//自动装配       @Resource一样的效果,@Inject
        @Qualifier(value="userUpdateServiceImpl")   //如果有多个实现可具体指定注入哪个

        private UserService userService;

    @Autowired
    private User user;
    private String school;

    public UserAccountServiceImpl(){
        this.school = user.getSchool();
    }

    会报错

    private User user;
    private String school;

    @Autowired
    public UserAccountServiceImpl(User user){
        this.user = user;
        this.school = user.getSchool();
    }

    Java变量的初始化顺序为:静态变量或静态语句块–>实例变量或初始化语句块–>构造方法–>@Autowired

    //加到方法上会自动执行

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");


    // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
    ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
    
    // 如果加载spring-context.xml文件:
    // ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
     

     ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());

    userService = context.getBean(UserService.class);

    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource resources[] = resolver.getResources("classpath*://com/***/*.xml");
     
     

    切面可以切注解

    AOP:注解

        <!-- 基础注解方式的aop -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    package com.aop.annotation;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    /**
     * 日志切面
     * @author Administrator
     *
     */
    
    
    @Component
    @Aspect
    @Order(1)//值越小优先级越高,越先执行
    public class LogAspects {
        
        /**
         * 声明切入点表达式
         * 在其他切面中可@Before("com.aop.annotation.declarePointExecution()")引用
         */
        @Pointcut("execution(* com.aop.annotation.*.*(..))")
        public void declarePointExecution(){
            
        }
        
    
        @Before("declarePointExecution()")
        public void before(JoinPoint joinPoint){
            System.out.println(joinPoint.getSignature().getName()+"-----方法调用之前");
        }
        
        /**
         * 后置通知方法抛异常仍然会执行,不能得到方法的返回结果
         * @param joinPoint
         */
        @After("execution(* com.aop.annotation.*.*(..))")
        public void after(JoinPoint joinPoint){
            System.out.println(joinPoint.getSignature().getName()+"-----方法调用之后");
        }
        
        /**
         * 返回通知,出异常不会被调用
         * @param joinPoint
         */
        @AfterReturning(value="execution(* com.aop.annotation.*.*(..))",returning="result")
        public void AfterReturning(JoinPoint joinPoint,Object result){
            System.out.println(joinPoint.getSignature().getName()+"-----方法调用的返回结果-------"+result);
        }
        
        
        /**
         * 异常通知,返回异常,可指定出现特定异常如NullPointerException
         * @param joinPoint
         */
        @AfterThrowing(value="execution(* com.aop.annotation.*.*(..))",throwing="ex")
        public void AfterReturning(JoinPoint joinPoint,Exception ex){
            System.out.println(joinPoint.getSignature().getName()+"-----方法调用出现异常-------"+ex);
        }
        
        
        /**
         * 环绕通知,和动态代理相似
         *  必须有返回值
         * @param joinPoint
         */
        @Around(value="execution(* com.aop.annotation.*.*(..))")
        public Object Around(ProceedingJoinPoint pjd){
            //System.out.println(joinPoint.getSignature().getName()+"-----环绕通知-------");
            try {
                return pjd.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return null;
        }
        
    }

    AOP:XML

    <aop:config>
            <aop:aspect id="jptSmsAopAspect" ref="jptSmsAop" order="5">
                <aop:after-returning pointcut="execution(* com.jit.gwcs.services.gw..*.*(..))" method="afterService" />
            </aop:aspect>
        </aop:config>
        
      
        <bean id="jptSmsAop" class="com.jit.gwcs.services.gw.sms.aop.SmsAop">
            <property name="jptSmsService" ref="jptSmsService"></property>
        </bean>
        <!-- 基础xml方式的aop -->
        <bean id="userService" class="com.aop.xml.UserService"></bean>
        <bean id="logAspects" class="com.aop.xml.LogAspects"></bean>
        
        <aop:config>
           <!-- 定义一个切点 -->
           <aop:pointcut expression="execution(* com.aop.xml.*.*(..))" id="pointcut"/>
           <!-- 配置切面通知 -->
           <aop:aspect ref="logAspects" order="2">
               <aop:before method="before" pointcut-ref="pointcut"/>
           </aop:aspect>
        
        </aop:config>

                                              1.before:前置通知(应用:各种校验)

                       在方法执行前执行,如果通知抛出异常,阻止方法运行

                2.afterReturning:后置通知(应用:常规数据处理)

                       方法正常返回后执行,如果方法中抛出异常,通知无法执行

                       必须在方法执行后才执行,所以可以获得方法的返回值。

                3.around:环绕通知(应用:十分强大,可以做任何事情) 【掌握】

                       方法执行前后分别执行,可以阻止方法的执行。要求必须手动的执行目标方法。

                4.afterThrowing:抛出异常通知(应用:包装异常信息)

                       方法抛出异常后执行,如果方法没有抛出异常,无法执行

                5.after:最终通知(应用:清理现场)

                       方法执行完毕后执行,无论方法中是否出现异常

    事务:基于注解的事务

    <!-- 配置事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <!-- 注解方式配置事物 在DAO中方法上添加@Transactional-->
        <!-- 事务的传播行为 propagation      对哪些异常不回滚noRollbackFor-->
        <!-- 事务的隔离级别@Transactional(isolation=Isolation.READ_COMMITTED) -->
        <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

    在项目中,@Transactional(rollbackFor=Exception.class),如果类加了这个注解,那么这个类里面的方法抛出异常,就会回滚,数据库里面的数据也会回滚。

    在@Transactional注解中如果不配置rollbackFor属性,那么事物只会在遇到RuntimeException的时候才会回滚,加上rollbackFor=Exception.class,可以让事物在遇到非运行时异常时也回滚

     Exception分为运行时异常RuntimeException和非运行时异常

     error是一定会回滚的

    非运行时异常是RuntimeException以外的异常,类型上都属于Exception类及其子类。如IOException、SQLException等以及用户自定义的Exception异常。对于这种异常,JAVA编译器强制要求我们必需对出现的这些异常进行catch并处理,否则程序就不能编译通过。所以,面对这种异常不管我们是否愿意,只能自己去写一大堆catch块去处理可能的异常。

    事务:基于XML

    <!-- 配置事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        
    
        <!-- 拦截器方式配置事物 -->
        <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="append*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="modify*" propagation="REQUIRED" />
                <tx:method name="edit*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="remove*" propagation="REQUIRED" />
                <tx:method name="repair" propagation="REQUIRED" />
                <tx:method name="delAndRepair" propagation="REQUIRED" />
    
                <tx:method name="get*" propagation="SUPPORTS" />
                <tx:method name="find*" propagation="SUPPORTS" />
                <tx:method name="load*" propagation="SUPPORTS" />
                <tx:method name="search*" propagation="SUPPORTS" />
                <tx:method name="datagrid*" propagation="SUPPORTS" />
    
                <tx:method name="*" propagation="SUPPORTS" />
            </tx:attributes>
        </tx:advice>
        <aop:config>
            <aop:pointcut id="transactionPointcut" expression="execution(* com.dogoframework.service.*.*(..))" />
            <aop:advisor pointcut-ref="transactionPointcut"
                advice-ref="transactionAdvice" />
        </aop:config>

    WEB初始化spring IOC原理自定义

    web.xml

             <context-param>
                <param-name>context</param-name>
                <param-value>spring.xml</param-value>
            </context-param>
            
            <listener>
                <listener-class>com.listener.my.SpringServletContextListener</listener-class>
            </listener>
    package com.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    /**
     * Application Lifecycle Listener implementation class SpringServletContextListener
     *
     */
    public class SpringServletContextListener implements ServletContextListener {
    
        /**
         * Default constructor. 
         */
        public SpringServletContextListener() {
            // TODO Auto-generated constructor stub
        }
    
        public void contextInitialized(ServletContextEvent sce) {
            //在应用初始化后初始化IOC容器
            ServletContext context = sce.getServletContext();
            /*<context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring.xml,classpath:spring-mybatis.xml,classpath:spring-druid.xml,classpath:spring-shiro.xml</param-value>
            </context-param>
            
            *<listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            </listener>
            *
            */
            String config = context.getInitParameter("contextConfigLocation");
            
            ApplicationContext context2 = new ClassPathXmlApplicationContext(config);
            
            //WebApplicationContextUtils.getRequiredWebApplicationContext(context);
            
            context.setAttribute("applicationContext", context2);
            // TODO Auto-generated method stub
            
        }
    
        public void contextDestroyed(ServletContextEvent sce) {
            // TODO Auto-generated method stub
            
        }
        
    }

    spring提供初始化

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml,classpath:spring-mybatis.xml,classpath:spring-druid.xml,classpath:spring-shiro.xml</param-value>
        </context-param>
    <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    获取spring   IOC容器     spring提供的

    WebApplicationContext context3 = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    package com.process;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    /**
     * Bean后置处理器
     * @author Administrator
     *
     */
    public class MyBeanPostProcess implements BeanPostProcessor{
    
        public Object postProcessBeforeInitialization(Object bean, String beanName)
                throws BeansException {
    
            System.out.println(beanName+"------------所有BEAN init前被调用");
            
            return bean;
        }
    
        public Object postProcessAfterInitialization(Object bean, String beanName)
                throws BeansException {
            System.out.println(beanName+"------------所有BEAN init后被调用");
            return bean;
            
        }
    
    }

     servlet获取spring 应用上下文

    WebApplicationContext ctx =  WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());

    spring获取servlet上下文

    ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();

    任意公共方法的执行:
        execution(public * *(..))
      任何一个以“set”开始的方法的执行:
        execution(* set*(..))
      AccountService 接口的任意方法的执行:
        execution(* com.xyz.service.AccountService.*(..))
      定义在service包里的任意方法的执行:
        execution(* com.xyz.service.*.*(..))
      定义在service包和所有子包里的任意类的任意方法的执行:
        execution(* com.xyz.service..*.*(..))
      定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
        execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")

      在多个表达式之间使用 ||,or表示 或,使用 &&,and表示 与,!表示 非.例如:

         <aop:config>
          <aop:pointcut id="pointcut" expression="(execution(* com.ccboy.dao..*.find*(..))) or (execution(* com.ccboy.dao..*.query*(..)))"/>
          <aop:advisor advice-ref="jdbcInterceptor" pointcut-ref="pointcut" />
      </aop:config>

    二、spring作用域

    scope

    1、单例

    @Lazy  延迟加载,初始化时不创建,获取的时候创建

    2、原型

    单例和原型创建bean的时期不同,单例是ioc容器初始化是创建对象,原型是获取时创建

    3、request

    4、session

    5、globe session

    三、bean创建条件

    1、实现Condition接口,重写matches

    2、@Import({ImportSelector.class})

    3、实现FactoryBean,把FactoryBean注册到容器中

    四、bean生命周期

    1、创建

    2、初始化

    3、属性赋值

    4、销毁

    五、@Component,@Service,@Controller,@Repository 注解bean

    @PostConstruct   相当于init

    @PreDestroy 相当于destroy

    或者实现接口

    bean

    InitializingBean 初始化

    DisposableBean 销毁

    除了bean ,给所有bean

    实现接口 BeanPostProcessor

    @Bean

    自动装配放到方法参数前

    public void aaa(@Autowired XX xx)

  • 相关阅读:
    csharp:Validate email address using C#
    Sql:SQL Server CREATE SEQUENCE statement
    机器学习实战---SVD简化数据
    机器学习实战---PCA降维
    机器学习实战---使用FP-growth算法来高效发现频繁项集
    机器学习实战---使用Apriori算法进行关联分析
    机器学习实战---集成学习AdaBoost算法
    支持向量机核函数的实现
    支持向量机SMO算法实现(注释详细)
    帧缓冲
  • 原文地址:https://www.cnblogs.com/jentary/p/6440440.html
Copyright © 2011-2022 走看看