Spring使用笔记复习~20230707

文章介绍了Spring项目中常用的依赖,如spring-webmvc、junit和slf4j-nop。接着讲解了XML配置,包括自动配置注解的支持。然后详细阐述了注解的使用,如@Nullable、@Autowired、@Qualifier和@Value等,以及不同层次的注解在Web开发中的应用。还提到了Java配置Spring的方式,展示了@Configuration和@ComponentScan的用法。最后,文章讨论了SpringAOP的两种实现方式,一种是使用原生SpringAPI,另一种是自定义类作为切面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring笔记复习

1. spring项目常用依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc-->
        <dependency>
<!--            公司名-->
            <groupId>org.springframework</groupId>
<!--            项目名-->
            <artifactId>spring-webmvc</artifactId>
<!--            版本-->
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.2</version>
            <type>jar</type>
        </dependency>
    </dependencies>

2.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: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/context
       http://www.springframework.org/schema/context/spring-context.xsd">
<!--    自动配置注解的支持,非常重要!!!  -->
    <context:annotation-config/>
</beans>

3.自动装配注解

  • @Nullable
  • 字段标记了这个注解,说明这个字段可以为null
  • @Autowired
  • 根据ByType() 来进行自动装配,如果存在多个相同类型的bean则使用下面的注解来进行自动装配
  • @Qualifier(value = “id”)
  • 根据ByName() 来进行自动装配
  • @Resource
  • 先根据ByName() 进行自动装配,如果不成功则通过ByType() 来进行自动装配,连个都不成功,则报错
  • @Component
  • 放在类上,说明这个类被Spring管理了,就是bean
  • @Value()
  • 放在属性上,表示数据的注入

4. 使用注解开发

  1. 在Spring4之后,要使用注解开发,必须要保证aop包的导入
  2. 使用注解需要导入context约束,增加注解支持 xml <context:annotation-config>
  3. @Component 属性注入使用@Value(“注入Date‘”)
  4. @Component 有几个衍生注解,我们在web开发中,会按照mvc三层结构分层,以下的注解,功能都是一样的
  • Dao【@Repositiry】
  • service【@Service】
  • controller【@Controller】

5.小结

  1. xml与注解:
    • xml更加万能,适用于任何场合,维护简单
    • 注解不是自己的类使用不了,维护相对困难
  2. xml与注解最佳实践:
    • xml用来管理bean
    • 注解只负责完成属性的注入
    • 我们在使用的过程中,只需要注意一个问题,必须让注解生效,就需要开启注解的支持

使用Java的方式配置Spring

  1. 不使用Spring 的xml 配置,全权交给Java来做
//XiaoChengConfig.class
//  这个也会被Spring容器托管,注册到Spring中,因为他本身就是Component
//  config代表一个配置类 = xml
@Configuration
@ComponentScan("com.xiaocheng.pojo")
public class XiaoChengConfig {
//    <bean id = "getUser" class = "User"/>
    @Bean
    public User getUser(){
//        返回要注入Bean的对象
        return new User();
    }
}

// User.class
// 这个注解的意思是这个类被Spring管理了
@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }
    //    注入数据
    @Value("DDDerek")
    public void setName(String name) {
        this.name = name;
    }
}

SpringAOP

1.方式1:Spring

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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">
    <!--    自动配置注解的支持,非常重要!!!  -->
    <context:annotation-config/>

    <bean id = "userService" class="com.xiaocheng.service.UserServiceImpl"/>
    <bean id = "log" class = "com.xiaocheng.log.Log"/>
    <bean id = "afterLog" class = "com.xiaocheng.log.AfterLog"/>
    <bean id = "testService" class="com.xiaocheng.service.TestServiceImpl"/>

    <!--    方式1.使用原生 Spring API 接口-->
    <!--    配置 Aop-->
    <aop:config>
    <!--        切入点-->
    <!--        expression = 表达式 excution(要执行的位置! * * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.xiaocheng.service.UserServiceImpl.*(..))"/>
        <aop:pointcut id="pointcut1" expression="execution(* com.xiaocheng.service.TestServiceImpl.*())"/>
    <!--        执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut1"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut1"/>
    </aop:config>
</beans>
  1. 方式2:自定义类
    <aop:config>
<!--        自定义切面,ref要引用的类-->
        <aop:aspect ref="diy">
<!--            切入点-->
<!--            execution 表达式-->
<!--            execution() 表达式主体-->
<!--            第一个* 表示返回类型-->
<!--            包名,表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包-->
<!--            第二个* 表示类名,*表示所有的类-->
<!--            最后的*(..) 表示所有的方法,..表示方法的参数,..表示所有的参数-->
            <aop:pointcut id="point" expression="execution(* com.xiaocheng.service.UserServiceImpl.*())"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值