一,概念:
Spring是一个基于IOC和AOP的结构J2EE系统的框架
IOC 反转控制 是Spring的基础,Inversion Of Control
简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象
DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。
二,配置
2.注解
applicationContext.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" 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="cn.sleepq123.pojo"></context:component-scan>
<context:component-scan base-package="cn.sleepq123.service"/>
<context:component-scan base-package="cn.sleepq123.aspect"/>
<aop:aspectj-autoproxy/> <!--开启aop支持-->
</beans>
cn.sleepq123.pojo (放javaBean的包)
Product.java
@Component("p")
public class Product {
private int id = 1;
private String name ="小米";
@Autowired
Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Component 对bean类的注解配置
@Autowired 对象注入
注意:必须在applicationContext.xml下添加下列代码
context:component-scan base-package="cn.sleepq123.pojo"></context:component-scan>
个人理解:这个就像扫描器
三,使用
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
Product product = (ProductService) context.getBean("p");
System.out.println(product.getName());
--------------------------------------------------分界线-----------------------------------------------------
一,AOP
AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等
周边功能在Spring的面向切面编程AOP思想里,即被定义为 切面在面向切面编程AOP的思想里面,核心业务功能和切面功能分别 独立进行开发
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP
1.applicationContext.xml配置
<context:component-scan base-package="cn.sleepq123.pojo"></context:component-scan>
<context:component-scan base-package="cn.sleepq123.service"/>
<context:component-scan base-package="cn.sleepq123.aspect"/>
<aop:aspectj-autoproxy/>
2.ProductService.java 服务业务(核心业务)
@Component("s")
public class ProductService {
public void doService() {
System.out.println("dododo");
}
}
3.LoggerAspect.java 日志业务(周边功能)
@Aspect
@Component
public class LoggerAspect {
@Around(value = "execution(* cn.sleepq123.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;
}
}
@Aspect 这是切面
@Component 表示这是一个bean,由Spring进行管理
@Around(value = "execution(* cm.sleepq123.service.ProductService.*(..))") 表示对cn.sleepq123.service.ProductService 这个类中的所有方法进行切面操作
4.test.java 测试
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
ProductService service = (ProductService) context.getBean("s");
service.doService();
}
}
打印结果
start log:doService
dododo
end log:doService
------------------------------------------------分界线-------------------------------------------------------------
使用Junit
1.导包
需要Junit 和 hamcrest-all 俩个jar包
2.SpringTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest {
@Autowired
Category category;
@org.junit.Test
public void test() {
System.out.println(category.getName());
}
}
@RunWith 指定是spring的测试类
@ContextConfiguration 指定spring的配置文件
@Test 测试逻辑
--END--