spring用法

spring创建对象的几种方式

1:使用默认构造函数来创建对象(必须要有一个无参构造函数才可以创建)

 <bean id="car" class="bean.Car"></bean>

2.使用工厂类的方法

工厂类

public class ServiceFactory {


    public MyService get(){
        return new MyService();
    }
}

配置文件

 <bean id="factory" class="Service.ServiceFactory"></bean>
 <bean id="service" factory-bean="factory" factory-method="get"></bean>

3.使用工厂类的静态方法

工厂类

public class StaticService {

    public static MyService get(){
        return  new MyService();
    }
}

配置文件

    <bean id="staticFactory" class="Service.StaticService" factory-method="get"></bean>

 

spring的作用范围

spring有singleton和prototype两种属性

singletong表示单例,只创建一个单例对象,在容器创建的时候创建。

prototype表示可以创建多个不同的对象,在要使用该对象的时候才会创建.

spring的依赖注入

1.使用构造方法的方式 

  <bean id="car" class="bean.Car">
        <constructor-arg name="name" value="baoma"></constructor-arg>
        <constructor-arg name="speed" value="120"></constructor-arg>
        <constructor-arg name="money" value="30000.2"></constructor-arg>
    </bean>

2.使用set方法的方式

    <bean id="car1" class="bean.Car">
        <property name="name" value="baoma"></property>
        <property name="money" value="2000.32"></property>
    </bean>

Spring的注解

1.用于创建对象的注解,和xml配置文件中的<bean>标签功能相同

@Controller @Service @Repository @Component

对象默认的id是类名首字母小写.也可以自己命名.注解有一个value属性可以用来定义id.当只有一个value属性时,可以省略不写.

2.用于注入数据的注解

@Autowired 自动按照成员变量类型注入

  1.当spring容器中有且只有一个对象的类型与要注入的类型相同时,注入该对象.

  2.当spring容器中有多个对象类型与要注入的类型相同时,使用要注入的变量名作为bean的id,在spring 容器查找,找到则注入该对象.找不到则报错.

@Qualifier::在自动按照类型注入的基础之上,再按照bean的id注入(不能单独使用,必须和@Autowired一起使用)

@Resource:直接按照bean的id注入,它可以独立使用.独立使用时相当于同时使用@Autowired@Qualifier两个注解.

3.用于改变作用范围的注解

1.@Scope  “singleton”或者“prototype”

4.和生命周期相关的注解

@PostConstruct:初始化方法

@PreDestroy:销毁方法

spring的纯注解配置

  1. @Configuration: 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解.获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class).
  2. @ComponentScan: 指定spring在初始化容器时要扫描的包,作用和bean.xml 文件中<context:component-scan base-package="要扫描的包名"/>是一样的. 其属性如下:
    • basePackages: 用于指定要扫描的包,是value属性的别名
  3. @Bean: 该注解只能写在方法上,表明使用此方法创建一个对象,并放入spring容器,其属性如下:
    • name: 指定此方法创建出的bean对象的id
    • 细节: 使用注解配置方法时,如果方法有参数,Spring框架会到容器中查找有没有可用的bean对象,查找的方式与@Autowired注解时一样的.
  4. @PropertySource: 用于加载properties配置文件中的配置.例如配置数据源时,可以把连接数据库的信息写到properties配置文件中,就可以使用此注解指定properties配置文件的位置,其属性如下:
    • value: 用于指定properties文件位置.如果是在类路径下,需要写上"classpath:"
  5. @Import: 用于导入其他配置类.当我们使用@Import注解之后,有@Import注解的类就是父配置类,而导入的都是子配置类. 其属性如下:
    • value: 用于指定其他配置类的字节码

 

Spring的AOP

AOP的相关术语

  • Joinpoint(连接点): 被拦截到的方法.

  • Pointcut(切入点): 我们对其进行增强的方法.

  • Advice(通知/增强): 对切入点进行的增强操作

    包括前置通知,后置通知,异常通知,最终通知,环绕通知

  • Weaving(织入): 是指把增强应用到目标对象来创建新的代理对象的过程。

  • Aspect(切面): 是切入点和通知的结合

1.使用<aop:config>标签声明AOP配置,所有关于AOP配置的代码都写在<aop:config>标签内

2.使用<aop:aspect>标签配置切面,其属性如下

  id:指定切面的id

  ref:引用通知类的id

3.使用<aop:pointcut>标签配置切入点表达式,指定对哪些方法进行增强,其属性如下

  1.切入点表达式的id

  2.expression:指定切入点表达式

4.使用<aop:xxx>标签配置对应类型的通知方法

  其属性如下:

    1.method: 指定通知类中的增强方法名.

    2.ponitcut-ref: 指定切入点的表达式的id

    3.oinitcut: 指定切入点表达式

    4.其中pointcut-refpointref属性只能有其中一个

  具体的通知类型

    1.<aop:before>: 配置前置通知,指定的增强方法在切入点方法之前执行.

    2.<aop:after-returning>: 配置后置通知,指定的增强方法在切入点方法正常执行之后执行.

    3.<aop:after-throwing>: 配置异常通知,指定的增强方法在切入点方法产生异常后执行.

    4.<aop:after>: 配置最终通知,无论切入点方法执行时是否发生异常,指定的增强方法都会最后执行.

    5.<aop:around>: 配置环绕通知,可以在代码中手动控制增强代码的执行时机.

 

 <aop:config>
        <aop:aspect id="logService" ref="logger">
            <aop:before method="beforePrint" pointcut="execution(* com.Service.*.*(..))"></aop:before>
            <aop:after-returning method="afterPrint" pointcut="execution(* com.Service.*.*(..))"></aop:after-returning>
        </aop:aspect>
    </aop:config>

 

使用注解配置AOP

半注解配置AOP,需要在bean,xml中加入下面语句开启对注解AOP的支持

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

用于声明切面的注解

@Aspect: 声明当前类为通知类,该类定义了一个切面.相当于xml配置中的<aop:aspect>标签

用于声明通知的注解

  • @Before: 声明该方法为前置通知.相当于xml配置中的<aop:before>标签
  • @AfterReturning: 声明该方法为后置通知.相当于xml配置中的<aop:after-returning>标签
  • @AfterThrowing: 声明该方法为异常通知.相当于xml配置中的<aop:after-throwing>标签
  • @After: 声明该方法为最终通知.相当于xml配置中的<aop:after>标签
  • @Around: 声明该方法为环绕通知.相当于xml配置中的<aop:around>标签
@Aspect
@Component
public class Logger {

    @Before("execution(* com.Service.MyService.*(..))")
    public void beforePrint(){
        System.out.println("that is beforePrint method");
    }

    public void afterPrint(){
        System.out.println("that is afterPrint method");
    }
}

 

posted @ 2020-08-03 13:28  jesscia5  阅读(149)  评论(0编辑  收藏  举报