spring注入、切面和测试


how2j这个网站对spring的解释非常好,摘录一些内容


1. 注入

首先applicationContext.xml是spring的核心配置文件,它写明了项目里面有哪些类是需要注入的。在这里,是把包com下面的所有类都扫描了,发现有注解的就注入。

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 扫描该包下面所有的类 -->
    <context:component-scan base-package="com.*"/>

</beans>

当然,也可以分别把每一个需要注入的类写进来:

<bean name="c" class="com.how2java.pojo.Category">
   <property name="name" value="category 1" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
   <property name="name" value="product1" />
   <property name="category" ref="c" />
</bean>

其实这个文件的命名怎么样都行,最主要的是需要一个东西带获取它:

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
        "applicationContext.xml"
});

ApplicationContext 有以下五点作用:
- Bean工厂方法,用于访问应用程序组件
- 有加载文件资源的功能
- 有发布监听器的功能
- 有处理消息和国际化的功能
- 有继承的功能,子上下文可以覆盖父上下文的配置。这意味着一个简单的父上下文可以引申出来多个子上下文,适用于项目中的各个servlet里面。

注:applicationContext.xml这个文件还有很多不明白的地方,比如开头的beans里面分别代表了什么。

在教程里面,在注解旁边写了这个bean的名字,引用的时候就写这个:


@Component("p")
public class Product {

Product p = (Product) context.getBean("p");

但是如果不自定义bean的名字,那么这个bean的默认名字是该类的首字母小写,比如在这里,就是product


2. AOP

AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等

周边功能在Spring的面向切面编程AOP思想里,即被定义为切面

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 然后把切面功能和核心业务功能 “编织” 在一起,这就叫AOP

<aop:config>
    <aop:pointcut id="loggerCutpoint"
        expression=
        "execution(* com.how2java.service.ProductService.*(..)) "/>

    <aop:aspect id="logAspect" ref="loggerAspect">
        <aop:around pointcut-ref="loggerCutpoint" method="log"/>
    </aop:aspect>
</aop:config>

execution(* com.how2java.service.ProductService.*(..))

这表示对满足如下条件的方法调用,进行切面操作:
*返回任意类型 com.how2java.service.ProductService.* 包名以 com.how2java.service.ProductService 开头的类的任意方法 (..) 参数是任意数量和类型

注解的方式:

<aop:aspectj-autoproxy/> 
package com.how2java.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggerAspect {

    @Around(value = "execution(* com.how2java.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

3. 测试

package com.how2java.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.how2java.pojo.Category;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
    @Autowired
    Category c;

    @Test
    public void test(){
        System.out.println(c.getName());
    }
}

运行这个代码需要右键运行里面选择JUnitt test,快捷键 shift+alt+x, t。以前一直知道这个单元测试,今天才是第一次使用,觉得挺有意思的。这样是不是意思就是不依赖别的方法也能运行呢?回头要试试。附一张运行成功的图片:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值