Spring-day1

Spring框架

是一个轻量级的企业级应用框架

优点

低侵入式设计

独立于各种应用服务器

依赖注入特性将组件关系透明化,降低耦合度

面向切面编程特性允许将通用任务进行集中式处理

与第三方框架的良好整合

Spring体系结构

Spring设计理念

是面向Bean的编程

Spring两大核心技术

(1)控制反转(IoC:Inversion of Control)/依赖注入(DI:Dependency Injection)

(2)面向切面编程(AOP:Aspect Oriented Programming)

控制反转

  • 最基础的调用的对象是通过new一个对象出来,例如:People p=new People
  • 我们Spring框架中的IOC即改变这种方式的调用,将后面“new People”转换为xml文件去调用,即使用第三者调用

使用控制反转的好处:用来降低程序代码之间的耦合度

依赖注入

  • 在Spring中实现控制反转的是IOC容器,其实现方法是DI(依赖注入)。

实现依赖注入的三种方式:setter方式注入,构造注入,使用P命名实现注入

setter方式注入

创建学生类,给予学生类一个学号,一个姓名的属性(此两个属性必须生成set方法,才能完成setter方式的注入)

public class Student {
 
	//学号
	private int num;
	//姓名
	private String name;
 
	public void setNum(int num) {
		this.num = num;
	}
 
	public void setName(String name) {
		this.name = name;
	}
}

 在applicationContext.xml文件中赋值

<!-- setter方法注入 -->
<bean id="stu" class="Student" >
		<property name="num" value="1"></property>
		<property name="name" value="李四"></property>
		
</bean>

构造注入

给学生类写个带参的构造函数(写完带参的构造函数后,必须再定义一个无参的构造函数)

//构造注入
public class Student {
	
	public Student(){
		
		
	}
	public Student(int num,String name){
		
	}
    //学号
	private int num;
	//姓名
	private String name;
}

在applicationContext.xml文件中赋值

	<!-- 构造器注入 -->
	<bean id="st" class="Student">
		<constructor-arg>
			<value type="java.lang.int">101</value>
		</constructor-arg>
		<constructor-arg> 
            <value type="java.lang.String">张三</value>
		</constructor-arg>
	</bean> 

使用P命名实现注入

在Student最基础类的基础上,给ApplicationContext.xml文件中导入一个头的包

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 
 
    xmlns:p="http://www.springframework.org/schema/p"
   
 
</beans>

  之后在xml用bean标签赋值

<bean id="stu" class="pojo.Student" p:name="路飞" p:word="我是海贼王">
    </bean>

基于注解的依赖注入_怎么给applicationcontext添加依赖_Muyuuu.的博客-CSDN博客



Bean的自动装配_醉卧雕龙舫 、的博客-CSDN博客

使用注解实现Spring IoC

注解方式将Bean的定义信息和Bean实现类结合在一起,Spring提供的注解有

@Component:实现Bean组件的定义

@Repository:用于标注DAO类

@Service:用于标注业务类

@Controller:用于标注控制器类

 

使用@Autowired注解实现Bean的自动装配 默认按类型匹配,

可使用@Qualifier指定Bean的名称 

 

 修改配置文件使注解生效,完成Bean的定义和组装

 

使用Java标准注解完成装配

使用@Resource注解实现组件装配 默认按名称匹配

 

 

对比@Autowired注解和@Resource注解

@Autowired是Spring提供的注解,@Resource是Java提供的

在不指定任何参数且不配合其他注解时,@Autowired注解会优先按Bean的类型进行装配,@Resource注解则是优先按Bean的名称进行装配

 

AOP

 面向切面编程,简单地说就是在不改变原程序的基础上为代码段增加新功能,对代码段进行增强处理 

前置增强、后置增强、最终增强、异常增强、环绕增强(定义一个用于增强的MyAspect类)

package Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * 切面类
 */
@Aspect
public class MyAspect {

    /**
     * 前置增强
     */
    @Before("execution(* service..*.*(..))")
    public void before(){
        System.out.println("前置通知。。。。");
    }

    /**
     * 后置增强
     * returnVal 切点方法执行后的返回值
     */
    @AfterReturning(value = "execution(* service..*.*(..))",returning = "returnVal")
    //Object returnValue:与注解中的returnValue名字一致
    public void afterReturning(Object returnVal){
        System.out.println("后置通知。。。"+returnVal);
    }

    /**
     * 环绕通知
     * @param joinPoint  可用于执行切点得类
     * @return
     * @throws Throwable
     */
    @Around("execution(* service..*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知前。。。。");
        Object obj =(Object) joinPoint.proceed();
        System.out.println("环绕通知后。。。。");
        return obj;
    }
    /**
     * 抛出通知
     */
    @AfterThrowing(value = "execution(* service..*.*(..))",throwing = "e")
    public void afterThrowable(Throwable e){
        System.out.println("出现异常:msg="+e.getMessage());
    }

    /**
     * 无论如何都会执行的方法
     */
    @After(value = "execution(* service..*.*(..))")
    public void after(){
        System.out.println("最终通知。。。。");
    }
}

Student类中定义一个study方法用作于切口

package pojo;

import lombok.Data;

@Data
public class Student {
    private String name;
    private String word;
    
    //学习的方法,当做于切口
    public void study(){
        System.out.println("学生"+name+"口号"+word);
    }

}

ApplicationContext.xml文件中配置的代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!--注入切面配置文件-->
    <bean id="aspect" class="Aspect.MyAspect"></bean>

    <!--支持注解开发 代理模式-->
    <aop:aspectj-autoproxy/>

</beans>

 

测试类中的关键代码(包括IOC以及AOP的测试)


public class testStudent {
    public static void main(String[] args) {
//第一步:读取Spring的核心文件
        ApplicationContext user=new ClassPathXmlApplicationContext("ApplicationContext.xml");
//第二步:根据ac对象获得里面所有的bean
        Student stu = (Student) user.getBean("stu");
        //System.out.println(stu.toString());
//调用study的方法
        stu.study();
    }
}

Sping框架的两大核心技术知识_spring最重要的两大技术_程序员xiaoQ的博客-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值