Spring框架(《面向切面》——对象值的注入——输出)

目录

简介

spring体系结构

Spring两大核心技术

一、Spring框架搭建

1.导入Spring依赖

2.编写Spring配置文件

二、开始代码编写

1.随便创建个实体类

2.开始往Spring里面注入

3.测试

三、不同的方法注入

1.使用p命名空间注入

2.使用构造器方法注入

 赋值是有特殊字符该怎么办?

3.使用注解方式把控制器给Spring

三、AOP切面

1.创建aop切面类

2.注入到Spring

3.结果

 aop注解

(完)


简介

按我的理解Spring框架是一个强大的粘合剂,可以和其他框架联动使用,是当前非常主流的框架。

优点:

  • 低侵入式设计
  • 独立于各种应用服务器
  • 依赖注入特性将组件关系透明化,降低耦合度
  • 面向切面编程特性允许将通用任务进行集中式处理
  • 与第三方框架的良好整合

spring体系结构

Spring两大核心技术

  1. 控制反转(IoCInversion of Control)/依赖注入(DIDependency Injection)
  2. 面向切面编程(AOPAspect Oriented Programming)

一、Spring框架搭建

1.导入Spring依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xinxi2</groupId><!--这是我的项目地址-->
    <artifactId>xinxi2_spring_test02</artifactId><!--这三个要根据自己项目的地址来填写-->
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.24</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.24</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.24</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>
2.编写Spring配置文件

在resources包中放spring配置文件有的人命名:spring.xml有的人命名: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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
">
    
</beans>

beans里面的xsi,aop,p,context后面都要使用先加上去后面编码时都要使用。

二、开始代码编写

1.随便创建个实体类

不做数据库交互,随便创建个实体类就行

package com.xinxi2.bean;

public class Students {
    private Integer id ;

    private String name ;

    private String bigname;

    public Integer getId(){
        return this.id;
    }

    public void setId(Integer id){
        this.id=id;
    }

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name=name;
    }

    public String getBigname() {
        return bigname;
    }

    public void setBigname(String bigname) {
        this.bigname = bigname;
    }
}

我就创建了一个只有三个属性的实体类,你们也可以直接用。

2.开始往Spring里面注入
<bean id="niuniu" class="com.xinxi2.bean.Students">
        <property name="name" value="牛牛"></property>
        <property name="bigname" value="牛圈"></property>
</bean>

这个是最基本的注入        id属性一定要唯一,声明出来后面还可以引用,class对应刚刚创建的实体类地址。

property标签中的        name属性对应上面class对应的实体类里面的属性,value填入的是值,不用value还可以用ref引用其他bean的id

3.测试

@Test注解是省略了main方法也是测试

@Test
    public void Test02(){
        <!--spring.xml对应你的spring地址-->
        ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
        <!--niuniu对应你spring配置文件中的niuniu标签的id属性-->
        Students students =(Students) ac.getBean("niuniu");
        System.out.println(students.getName()+students.getBigname());
    }

输出结果

三、不同的方法注入

1.使用p命名空间注入
<bean id="shishi" class="com.xinxi2.bean.Students" p:name="龙龙" p:bigname="非洲"></bean>
2.使用构造器方法注入

构造器注入要有对应的构造方法

public Students(){

}
public Students(String name,String bigname){
        this.name=name;
        this.bigname=bigname;
}

切记添加有参的构造方法时一定要把无参的给带上真的非常非常非常非常重要,

自我感觉构造器方法是相当的不方便,狗都不用

<bean id="niuzi" class="com.xinxi2.bean.Students">
        <constructor-arg name="name" value="牛子"></constructor-arg>
        <constructor-arg name="bigname" value="牛圈"></constructor-arg>
</bean>

p命名空间和构造器的区别

  1. p命名空间,先调用无参构造方法再调用对应的set方法  灵活,慢,不好控制
  2. 构造器注入:要求必须有对应的构造方法,创建对象的同时,直接赋值  好控制,快
 赋值是有特殊字符该怎么办?

比如说你想把牛牛用html的h1标签赋值<>和/都是特殊字符会出现错误

这时候就出现了value标签的<![CDATA[]]>标签给括起来

<bean id="guangtou" class="com.xinxi2.bean.Students">
        <property name="name">
            <value><![CDATA[<h1>牛牛</h1>]]></value>
        </property>
    </bean>
3.使用注解方式把控制器给Spring

声明bean的注解

  • @Component 组件,没有明确的角色
  • @Service 在业务逻辑层使用(service层)
  • @Repository 在数据访问层使用(dao层)
  • @Controller 在展现层使用,控制器的声明(servlet层)

 声明后可以给注解后面加个括号给命名,到时候可以直接通过命名使用

声明完注解一定要在spring配置文件中扫描后才能使用

<context:component-scan base-package="com.xinxi2.bean,com.xinxi2.computer"></context:component-scan>

扫描的是包的地址,如果有多个包需要扫描,在base-package里面用“,”隔开。

三、AOP切面

aop切面按我的理解就是:把一些重复型的代码写在里面让多个代码都使用到,一些有变动性的代码不能使用aop切面,需要自己编写。

1.创建aop切面类

一般都先创建一个切面包大部分都命名为aspect。

方法名根据自己的业务来命名

package com.xinxi2.aspect;

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

import java.util.Arrays;

public class SysLogger {

    //前置增强,切入的方法前执行
    public void before(JoinPoint jp){
        System.out.println("类名:"+jp.getTarget().toString());
        System.out.println("方法名:"+jp.getSignature().getName());
        System.out.println("参数:"+ Arrays.toString(jp.getArgs()));
    }
    //后置增强,切入的方法后执行,不一定被执行,出现异常的情况下
    public void afterReturning(JoinPoint jp,Object obj){
        System.out.println("后置增强");
    }
    //异常增强,出现异常执行
    public void afterException(JoinPoint jp,Exception e){
        System.out.println("异常增强");
    }
    //环绕增强,最强大的一个增强,前后织入代码,能拿到返回值,嫩捕获异常,能决定方法是否执行。
    public Object around(ProceedingJoinPoint pjp){
        Object result=null;
        try {
            System.out.println("*************");
            result=pjp.proceed();
            System.out.println("-------------");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }
    //最终增强,一定会被执行的增强
    public void after(JoinPoint jp){
        System.out.println("最终增强");
    }
}
2.注入到Spring
<bean id="sysLogger" class="com.xinxi2.aspect.SysLogger"></bean>
<aop:config>
        <aop:pointcut id="loggerTestClass"
                      expression="execution(public void print(..))"/>
        <aop:aspect ref="sysLogger">
            <aop:before  method="before" pointcut-ref="loggerTestClass"></aop:before>
            <aop:after-returning method="afterReturning" pointcut-ref="loggerTestClass" returning="obj"></aop:after-returning>
            <aop:after-throwing method="afterException" pointcut-ref="loggerTestClass" throwing="e"></aop:after-throwing>
            <aop:around method="around" pointcut-ref="loggerTestClass"></aop:around>
            <aop:after method="after" pointcut-ref="loggerTestClass"></aop:after>
        </aop:aspect>
    </aop:config>

aop:pointcut标签 id属性下面要用到这个匹配规则    expression属性设置表达式匹配规则

aop:aspect ref属性引用上面声明的切面类        下面的五种方法对应五种切面        method对应切面类的方法名        pointcut-ref对应上面的匹配规则

常用匹配规则
public * addNewUser(entity.User);

"*"表示匹配所有类型的返回值

public void *(entity.User);

"*"表示匹配所有方法名

public void addNewUser(..);

".."表示匹配任意参数个数和类型

* com.service.*.*(..);

匹配com.service包下所有类的所有方法

* com.service..*.*(..);匹配com.service包及其子包下所有类的所有方法
3.结果

 aop注解

  • Spring支持AspectJ的注解式切面编程。
  • @Aspect 声明一个切面(类上) 
  • 使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数。
  • @After 在方法执行之后执行(方法上) 
  • @Before 在方法执行之前执行(方法上) 
  • @Around 在方法执行之前与之后执行(方法上)
@Aspect
@Component("loggerAdvice")
public class LoggerAdvice {

    @Before("execution(public void print())")
    public void before(JoinPoint jp){
        System.out.println("前置增强");
    }
}

一定要先使用@Aspect注解声明,要记得在spring配置文件使用<context:component-scan>扫描

匹配规则直接在方法上面execution里声明

(完)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值