Spring学习笔记(一) IOC/DI 注入对象 注解方式IOC/DI AOP

首先呢 

Spring是一个基于IOC和AOP结构的J2ee系统的框架

IOC 是反转控制,也是Spring的基础Inversion Of Control, 在之前呢,创建对象都是我们自己new的,然后进行各种配置,现在IOC呢就是把这一步交给Spring来做。

DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。

比如我们准备好一个Category的bean:

package com.how2java.pojo;

public class 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;
	}
	private int id;
	private String name;
}

然后在src目录下新建applicationContext.xml文件
applicationContext.xml是Spring的核心配置文件,通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"category 1“到name属性中

<?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">
  
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
  
</beans>

配置好之后,就可以通过spring获取到category对象,和在xml中注入好的name属性了。

package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.pojo.Category;
 
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
 
        Category c = (Category) context.getBean("c");
         
        System.out.println(c.getName());
    }
}

原理图:原理图

站长的说法就是,传统的方式就像我们new了一只鸡,还需要自己进行很多工序之后才能吃,而ioc方式能就相当于我们去饭店,饭店把菜做好,我们直接吃。

注入对象:

创建一个Product 的bean,里面带有属性category 。

然后在xml里面通过ref来给product注入category对象。

<?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">
 
    <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>
 
</beans>

然后从spring那里拿到对象p之后,通过p.getCategory就可以获取到p中注入好的对象category啦

注解方式:

在上面的注入对象都是通过xml中的代码实现的,还是比较繁琐的。

还有一种方式很简单的实现注入:那就是注解哈哈哈

在applicationContext.xml中加上一句:

<context:annotation-config/>

表示告诉spring,我要开始用注解的方式进行配置了。

然后注解掉刚才在product中通过ref注入category的那句。

然后在product bean中,定义category的上面加上@Autowired

然后运行测试可以发现和之前的注入对象达到了同样的效果。

除了@Autowired之外 还可以Resource(name="c")来达到同样的效果,区别在下面代码的注释上写上了。

package com.how2java.pojo;

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 1";
	@Autowired //是按类型找
	 //@Resource(name="c")是先按提供的名称找,找不到再按类型找
	private Category category;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

那么上面就是通过注解来解决注入对象的问题,

出自之外,上面的product、category  这些bean的本身,也可以脱离applicationContext.xml的限制,通过注解来进行。

首先把之前xml中的beans内容全去掉。

只增加一条

<context:component-scan base-package="com.how2java.pojo"/>

这是告诉spring我的bean都在com.how2java.pojo这个包目录下。

然后在之前的bean类上加上@Component

@Component("p")
public class Product {

运行,也能达到同样的结果,而且代码简单了很多。

AOP

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

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

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

准备业务类 ProductService

package com.how2java.service;
 
public class ProductService {
     
    public void doSomeService(){
         
        System.out.println("doSomeService");
         
    }
     
}

准备日志切面 LoggerAspect

ackage com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
 
public class LoggerAspect {
 
    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;
    }
}

applicationContext.xml中加上:

<bean name="s" class="com.how2java.service.ProductService">
    </bean>   
     
    <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
     
    <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> 

aop:pointcut表示核心业务内容,expression中的表示在执行这个地址下的方法之后会调用切面内容。

aop:aspect 中的就是切面内容。

<?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.how2java.pojo"/>

    <bean name="s" class="com.how2java.service.ProductService">
    </bean>
    
    <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect" />
    <bean id="TimingAspect" class="com.how2java.aspect.TimingAspect" />
    <aop:config>
    	<aop:pointcut expression="execution(* com.how2java.service.ProductService.*(..))" id="loggerCutpoint"/>
    	<aop:aspect id="logAspect" ref="loggerAspect">
    		<aop:around pointcut-ref="loggerCutpoint" method="log"/>
    	</aop:aspect>
    	<aop:aspect id="TimeAspect" ref="TimingAspect">
    		<aop:around pointcut-ref="loggerCutpoint" method="show_time"/>
    	</aop:aspect>
    </aop:config>
    
    

     
</beans>

学习网站https://how2j.cn/p/4922

发布了82 篇原创文章 · 获赞 21 · 访问量 7938

猜你喜欢

转载自blog.csdn.net/qq_41658124/article/details/104841258