spring基础代码实现(注解方式)

本文介绍了Spring框架的基础,重点讲解了IOC(反转控制)和DI(依赖注入)的概念,以及AOP(面向切面编程)的思想。在IOC中,对象的创建由Spring管理。DI允许对象的属性在使用时已经注入了值。AOP则将核心业务功能与周边功能如日志、事务管理等分离,通过‘编织’实现结合。文章还包含关键代码示例。
摘要由CSDN通过智能技术生成

Spring是一个基于IOC和AOP的结构J2EE系统的框架,IOC 反转控制 是Spring的基础;

一、Inversion Of Control简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象;
在这里插入图片描述
二、DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。
三、AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等

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

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能 “编织” 在一起,这就叫AOP
在这里插入图片描述
主要代码:
在这里插入图片描述

LoggerAspect类:
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;
	    }
}
Category类:
import org.springframework.stereotype.Component;

@Component("c")
public class Category {
	private int id;
	private String name="category 1";
	
	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;
	}
	

}

Product类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("p")
public class Product {
	private int id;
    private String name = "product 1";
    
    @Autowired
    private 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;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
    
}

ProductService类:
import org.springframework.stereotype.Component;

@Component("s")
public class ProductService {
	public void doSomeService(){
        
        System.out.println("doSomeService");
         
    }

}
TestSpring类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.service.ProductService;
 
public class TestSpring {
  
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值