• Spring学习篇(二)


    Spring学习篇(二)

    1 使用注解annotation

    1.1 配置spring-config文件

    1.1.1 组件扫描

    a 用法

    <context:component-scan base-package="需要扫秒的文件的最上层目录">context:component-scan>
    
    • 1

    b 实际代码

    <context:component-scan base-package="com">context:component-scan>
        
    
    • 1
    • 2
    1.1.2 开启注解模式
    <context:annotation-config>context:annotation-config>
    
    • 1

    2.1 给UserService类加上@Service注解

    2.1.1 没有指定id时
    a 语法
    @Service
    
    • 1
    b UserService类
    package com.service;
    
    import org.springframework.stereotype.Service;
    //@Service用于业务层,不写id默认就是首字母小写后的类名即userService
    @Service
    //@Scope("singleton") 也可以用@Scope注解指定是单例模式还是多例模式(原型模式)
    public class UsersService {
        public void login(){
            System.out.println("执行用户登录业务");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    c 测试类代码
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.service.UsersService;
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-config.xml");
            UsersService us= (UsersService) ac.getBean("usersService");
            us.login();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    d 运行代码截图

    在这里插入图片描述

    2.1.2 指定id时
    a 语法
    @Service("id值")
    
    • 1
    b UserService类
    package com.service;
    
    import org.springframework.stereotype.Service;
    //@Service用于业务层,写了id就只能通过id名,不能通过类名去查找了
    @Service("us")
    public class UsersService {
        //UsesDao的创建由容器去创建
        public void login(){
            System.out.println("执行用户登录业务");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    c 测试类代码
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.service.UsersService;
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-config.xml");
            UsersService us= (UsersService) ac.getBean("us");
            us.login();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    d 运行代码截图

    在这里插入图片描述

    2.1.3 getBean中通过类名.class去获取
    a 语法
    类名 自定义变量名= (类名) ac.getBean(类名.class);
    
    • 1
    b UserService类
    package com.service;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    //@Service用于业务层,写了id就只能通过id名,不能通过类名去查找了,但是可以通过类名.class去查找的
    @Service("us")
    //@Scope("singleton")  scope注解可以指定是单例还是多例
    public class UsersService {
        //UsesDao的创建由容器去创建
        public void login(){
            System.out.println("执行用户登录业务");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    c 测试类代码
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.service.UsersService;
    
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-config.xml");
            UsersService us= (UsersService) ac.getBean(UsersService.class);
            us.login();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    d 运行代码截图

    在这里插入图片描述

    2.2 使用注解注入属性

    2.2.1 @Autowired
    a 用法
     @Autowired 按照其下方的数据类型匹配(要求数据类型必须唯一,还是去匹配id),
     默认需要该bean在容器中存在,如果不存在就报错
    
    如果需要允许改bean为空 可以设置required=false
    
    • 1
    • 2
    • 3
    • 4
    b 示例代码
    b.1 UserService类
    package com.service;
    
    import com.dao.UsersDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    @Service("us")
    public class UsersService {
        @Autowired
        private UsersDao ud;
        public void login(){
            System.out.println("执行用户登录业务");
            ud.login();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    b.2 UserDao类
    package com.dao;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class UsersDao {
        public void login(){
            System.out.println("--用户登录查询--");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    c 测试代码
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.service.UsersService;
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-config.xml");
            UsersService us= (UsersService) ac.getBean(UsersService.class);
            us.login();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    d 测试代码运行截图

    在这里插入图片描述

    e 不存在这样的数据类型的报错截图

    在这里插入图片描述

    2.2.2 @Resource
    a 用法
    @Resource 先按照名称(下方的形参名)查找 如果没有找到 再按照类型查找 都未找到 就报错
    
    • 1
    b 示例代码
    b.1 UserService类
    package com.service;
    
    import com.dao.UsersDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    @Service("us")
    public class UsersService {
        @Resource
        private UsersDao ud;
        public void login(){
            System.out.println("执行用户登录业务");
            ud.login();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    b.2 UserDao类
    package com.dao;
    import org.springframework.stereotype.Repository;
    @Repository
    public class UsersDao {
        public void login(){
            System.out.println("--用户登录查询--");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    c 测试代码
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.service.UsersService;
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-config.xml");
            UsersService us= (UsersService) ac.getBean(UsersService.class);
            us.login();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    d 测试代码运行截图

    在这里插入图片描述

    e 不存在这样的数据类型的报错截图

    在这里插入图片描述

    2.2.3 @Qualifier(“id值”)
    a 用法
    @Autowired
    @Qualifier("ud")
    /* @Qualifier是不能单独使用的,需要与autoWired配合使用,
        找的是四大注解(@component,@service,@controller,@repository)里面所写的id值*/
    
    • 1
    • 2
    • 3
    • 4
    b 示例代码
    b.1 UserService类
    package com.service;
    
    import com.dao.UsersDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    @Service("us")
    public class UsersService {
        /* @Qualifier是不能单独使用的,需要与autoWired配合使用,
        找的是四大注解(@component,@service,@controller,@repository)里面所写的id值*/
        @Autowired
        @Qualifier("ud")
        private UsersDao ud;
        public void login(){
            System.out.println("执行用户登录业务");
            ud.login();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    b.2 UserDao类
    package com.dao;
    import org.springframework.stereotype.Repository;
    @Repository("ud")
    public class UsersDao {
        public void login(){
            System.out.println("--用户登录查询--");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    c 测试代码
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.service.UsersService;
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-config.xml");
            UsersService us= (UsersService) ac.getBean(UsersService.class);
            us.login();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    d 测试代码运行截图

    在这里插入图片描述

    e 不存在id时出现的报错信息

    在这里插入图片描述

  • 相关阅读:
    让无数人头疼的网络攻击到底是什么?究竟隐藏了哪些不为人知的秘密?
    Redis 的网络框架是实现了 Reactor 模型吗?
    【无标题】
    语音和噪声相关数据集(持续更新)
    详解Python中的切片(一看就懂版)
    Java异常、继承结构、处理异常、自定义异常、SpringBoot中全局捕获处理异常
    Ubuntu 22.04 一次及其繁琐的 允许 Traceroute 探测漏洞修复之旅
    【计算机视觉 | CNN】Image Model Blocks的常见算法介绍合集(三)
    魔改车钥匙实现远程控车:(3)通过蓝牙与手机通信并持久化保存参数设置
    vue开发游戏知识
  • 原文地址:https://blog.csdn.net/SSS4362/article/details/127806394