Spring学习第二天:Spring的注解开发

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Spring学习第二天:Spring的注解开发

文章目录

准备工作

持久层创建

在com/spring/dao包下创建IaccountDao接口

内容:

package com.spring.dao;
public interface IaccountDao {
    public void saveUser();
}

在com/spring/dao/impl包下创建AccountDaoImpl类

package com.spring.dao.impl;
import com.spring.dao.IaccountDao;
import org.springframework.stereotype.Repository;
public class AccountDaoImpl implements IaccountDao {
    public void saveUser() {
        System.out.println("保存成功");
    }
}

业务层创建

在com/spring/server包下创建IAccountserver接口

package com.spring.server;
public interface IAccountserver {
    public void saveAccount();
}

在com/spring/server/impl包下创建Accountserverimpl类

package com.spring.server.impl;
public class Accountserverimpl implements IAccountserver {
    private IaccountDao dao ;
    public Accountserverimpl() {
        System.out.println("accountserverimpl"+"被创建了");
    }
    @Override
    public void saveAccount() {
        dao.saveUser();
    }
}

表示层创建

package com.spring;
import com.spring.server.IAccountserver;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取对象
        IAccountserver accountserver=(IAccountserver)ac.getBean("acServer");
        accountserver.saveAccount();
    }
}

更改配置文件

改变bean.xml

<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:component-scan base-package="com.spring"/>
</beans>

注解:

注解大体可以分为四类

1.创建对象 ---- 对应xml中的bean标签

2.注入数据 ---- 对应xml中的property标签

3.改变作用范围(多例,单例 ,request ,session ,global—session) ---- 对应xml中的scope属性

4.和生命周期相关 ---- 对应xml中的init-method和destroy-method

创建对象的注解

Component:

属性:

value:用于指定类的id:当此属性是空的时,id为首字母改为小写的类名,

以下三个注解功能和Component相同 只是针对不同的层级提供了不同的名称,以便区分

Controller:一般用于表现层

Service:一般用于业务层

Repository:一般用于持久层

可以试一下对Accountserverimpl类做出以下修改

@Service(value = "acServer")
public class Accountserverimpl implements IAccountserver {
    private IaccountDao dao ;
    public Accountserverimpl() {
        System.out.println("accountserverimpl"+"被创建了");
    }
    @Override
    public void saveAccount() {
        dao.saveUser();
    }
}

主函数:

    public static void main(String[] args){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取对象
        IAccountserver accountserver=(IAccountserver)ac.getBean("acServer");
        System.out.println(accountserver);
    }

此时运行即可看到结果

image.png

同样给AccountDaoImpl添加注解 以便后面的使用

@Repository(value = "AD")
public class AccountDaoImpl implements IaccountDao {
    @Override
    public void saveUser() {
        System.out.println("保存成功");
    }
}

注入数据注解

Autowired:(此注解只能够自动注入)

          作用:自动按照类型注入,只要容器中有一个bean对象和要注入的类型匹配就可以注入
                若ioc容器中没有任何一个类型与其匹配则注入失败
                若ioc容器中有多个匹配项则先根据需要注入变量的数据名称来注入
           出现位置:成员变量或方法上

Qualifier

           作用:按照类型注入的基础上再按照名称注入 在给类成员注入时不能单独使用,但在方法传参时可以单独使用
           属性:
               value:用于指定注入bean的id

这时我们更改以下主类让其调用save方法

Resource

  作用:按照类型注入的基础上再按照id注入,可以单独使用
  属性:name:传入id
  《--------------------------以上三个标签都只能注入bean数据类型而不能注入基本数据类型和string---------------------------------》

value

  作用:基本数据类型和string的注入
  属性:
      value:用于指定数据的值 可以使用spel
        spel的写法:${表达式}

演示:

public static void main(String[] args){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取对象
        IAccountserver accountserver=(IAccountserver)ac.getBean("acServer");
        System.out.println(accountserver);
        accountserver.saveAccount();
    }

此时运行会发现出现了空指针异常

原因很明显Accountserverimpl中的dao没有初始化

那么我们就要对dao进行注入

这里使用@Autowired注解

Service(value = "acServer")
public class Accountserverimpl implements IAccountserver {
    @Autowired
    private IaccountDao dao ;
    public Accountserverimpl() {
        System.out.println("accountserverimpl"+"被创建了");
    }
    @Override
    public void saveAccount() {
        dao.saveUser();
    }
}

此时便可以成功运行

由此可知@Autowired是通过数据类型在ioc容器中查找对应数据类型来注入的

但是又出现了新的问题,如果同时存在两个IaccountDao 的实现类,那spring会注入哪个呢

可以试一下:

我们复制一份AccountDaoImpl,取名AccountDaoImpl2

更改@Repository(value = "AD")@Repository(value = "AD2")此时再次运行主程序便会报错

这是因为同时有两个bean拥有相同数据类型时,spring不知道你想要的具体是哪个对象,我们有两种方式进行修改:

  1. 修改类中的变量名称使其与ioc容器中的id相同(显然不是什么好方法)
  2. 使用@Qualifier注解
    @Qualifier注解在给类成员变量进行注入时需要与@Autowired配合使用

如:

@Service(value = "acServer")
public class Accountserverimpl implements IAccountserver {
    @Autowired
    @Qualifier("AD2")
    private IaccountDao dao ;
    public Accountserverimpl() {
        System.out.println("accountserverimpl"+"被创建了");
    }
    @Override
    public void saveAccount() {
        dao.saveUser();
    }
}

此时便可以正常运行,并获取到你想要的bean

但用两个注解是不是繁琐了一点

这里便有了Resource注解

我们使用@Resource(name = "AD")@Resource(name = "AD2")注解来实现功能

即:

public class Accountserverimpl implements IAccountserver {
    @Resource(name = "AD2")
    private IaccountDao dao ;
    public Accountserverimpl() {
        System.out.println("accountserverimpl"+"被创建了");
    }
    @Override
    public void saveAccount() {
        dao.saveUser();
        System.out.println(s);
    }
}

!!----------------------注意以上三个注解只能注入bean对象而不能注入基本数据类型和String--------------------------!!

这时我们在Accountserverimpl中添加一个私有string类型的变量

我们可以通过value注解进行注入

@Service(value = "acServer")
@Scope(value = "prototype")
public class Accountserverimpl implements IAccountserver {
    @Resource(name = "AD2")
    private IaccountDao dao ;
    @Value(value = "hello")
    private String s;
    public Accountserverimpl() {
        System.out.println("accountserverimpl"+"被创建了");
    }
    @Override
    public void saveAccount() {
        dao.saveUser();
        System.out.println(s);
    }
}

此时调用saveAccount即可看见

image.png

改变作用范围

(singleton,prototype ,request ,session ,global—session) ---- 对应xml中的scope属性

Scope 与创建对象标签一起使用

作用:与xml中的scope属性一致

属性:value 传入singleton,prototype ,request ,session ,global—session

@Service(value = "acServer")
@Scope(value = "prototype")
public class Accountserverimpl implements IAccountserver {
    @Resource(name = "AD2")
    private IaccountDao dao ;
    @Value(value = "hello")
    private String s;
    public Accountserverimpl() {
        System.out.println("accountserverimpl"+"被创建了");
    }
    @Override
    public void saveAccount() {
        dao.saveUser();
        System.out.println(s);
    }
}

此时更改主程序

public class Test {
    public static void main(String[] args){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取对象
        IAccountserver accountserver=(IAccountserver)ac.getBean("acServer");
        IAccountserver accountserver2=(IAccountserver)ac.getBean("acServer");
        System.out.println(accountserver==accountserver2);
//        accountserver.saveAccount();
    }
}

即可看见运行结果,已经成功的变为多例

image.png

生命周期注解

@PostConstruct 指定初始化函数

@PreDestroy指定销毁函数

@Service(value = "acServer")
public class Accountserverimpl implements IAccountserver {
    @Resource(name = "AD2")
    private IaccountDao dao ;
    @Value(value = "hello")
    private String s;
    public Accountserverimpl() {
        System.out.println("accountserverimpl"+"被创建了");
    }
    @Override
    public void saveAccount() {
        dao.saveUser();
        System.out.println(s);
    }
    @PostConstruct
    public void init(){
        System.out.println("初始化");
    }
    @PreDestroy
    public void end(){
        System.out.println("对象销毁");
    }
}

配置文件注解:

在前面的这些注解中能够大部分的去除对xml的依赖,但仍无法脱离xml

比如创建jar包中类的bean就不太可能,所以我们有了下面这些注解

Configuration和ComponentScar

Configuration

作用:指定当前类是一个配置类

ComponentScar

作用:用于通过注解指定spring在创建容器时要扫描的包

属性

value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。

我们使用此注解就等同于在xmL中配置了:<context: component-scan base-package=“com.itheima”></context: component-scan>

@Configuration
@ComponentScan("com.spring")
public class SpringConfig {
}

这样就可以声明一个配置类了

bean

Bean

作用:用于把当前方法的返回值作为bean对象存入spring的ioc器中

属性:

name:用于指定bean的id,当不写时,默认值是当前方法的名称

当我们使用注解配置方法时,如果方法有参数, spring框架会去容器中查找有没有可用的bean对象。查找的方式和Autoviredi解的作用是一样的 当我们要指定容器中bean的id时,使@Qualifier注解

@Configuration
@ComponentScan("com.spring")
public class SpringConfig {
    @Bean
    @Scope("prototype")
    public QueryRunner createQueryRunner(@Qualifier("ds1") DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    @Bean(name="ds1")
    public  DataSource createDataSource(){
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring_test");
            dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            dataSource.setUser("root");
            dataSource.setPassword("adminadmin");
            return dataSource;
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

可以看见Bean注解是可以和Scope配合使用的

import

import

作用:导入其他配置类

属性:

Class<?>[] value() 传入一个或多个类的句柄(.class)有impact注解的为主配置,而其他为子配置类

PropertySource

PropertySource

作用:读取properties配置文件

属性:

value:指定文件名称和路径。

关键字:classpath 表示在类路径下

示例:

由于前面获取参数的方式过于粗暴

我们就可以使用PropertySource来获取参数

image.png

首先创建jdbcconfig.properties

jdbc.url=jdbc:mysql://localhost:3306/spring_test
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=adminadmin

然后在SpringConfig文件里做修改

@Configuration
@ComponentScan("com.spring")
@PropertySource("classpath:jdbcconfig.properties")
public class SpringConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.user}")
    private String user;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String url;
    @Bean
    @Scope("prototype")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    @Bean
    public  DataSource createDataSource(){
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setJdbcUrl(url);
            dataSource.setDriverClass(driver);
            dataSource.setUser(user);
            dataSource.setPassword(password);
            return dataSource;
        }
        catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

此时注解就调用成功了

在junit中调试时

在测试类中,如果需要使用spring直接写注解是无法使用的,我们首先需要导入依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.10</version>
        </dependency>

在测试类加入

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class DbutilTest {
    @Autowired
    private IAccountService service;
    @Test
    public void testFindAll() {
}

此时在测试类中才能正常使用spring

总结

我们在注解配置时,并没有说所有情况变得更加方便,反而在对jar包配置时更加繁琐,所以在实际开发中,我们通常采用xml和注解相结合的方式来进行的,哪个方便就用哪个。


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4天前
|
Java 开发者 Spring
深入理解Spring Boot的@ComponentScan注解
【4月更文挑战第22天】在构建 Spring Boot 应用时,@ComponentScan 是一个不可或缺的工具,它使得组件发现变得自动化和高效。这篇博客将详细介绍 @ComponentScan 的基本概念、关键属性及其在实际开发中的应用。
21 4
|
6天前
|
Java 开发者 Spring
Spring Framework 中的 @Autowired 注解:概念与使用方法
【4月更文挑战第20天】在Spring Framework中,@Autowired 注解是实现依赖注入(Dependency Injection, DI)的一种非常强大的工具。通过使用 @Autowired,开发者可以减少代码中的引用绑定,提高模块间的解耦能力
29 6
|
29天前
|
XML 开发框架 Java
Spring轻量级开发框架(二)
Spring轻量级开发框架
47 0
|
1月前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
29 1
|
1月前
|
开发框架 安全 Java
Spring 框架:企业级应用开发的强大工具
在当今数字化时代,企业级应用开发的需求日益增长。为了满足这一需求,开发者们需要一款功能强大、易于使用的开发框架。Spring 框架作为 Java 领域的领先者,为企业级应用开发提供了全面的解决方案。本文将深入探讨 Spring 框架的各个方面,包括其历史、核心模块、优势以及应用场景。
24 0
|
1月前
|
Java Spring 容器
【Java】Spring如何扫描自定义的注解?
【Java】Spring如何扫描自定义的注解?
35 0
|
1月前
|
Java 测试技术 数据库
SpringBoot:@Profile注解和Spring EL
SpringBoot:@Profile注解和Spring EL
|
1月前
|
Java 数据库 Spring
【spring(四)】Spring事务管理和@Transactional注解
【spring(四)】Spring事务管理和@Transactional注解
|
28天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
66 1
|
28天前
|
存储 XML 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南(一)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南
66 0