• 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时出现的报错信息

    在这里插入图片描述

  • 相关阅读:
    基于RuoYi-Flowable-Plus的若依ruoyi-nbcio支持自定义业务表单流程(一)
    Java数据结构与算法——二分查找
    Paragon NTFS For Mac2023破解版免费下载安装激活
    JAVA基础语法
    gitlab 离线安装问题解决:NOKEY,signature check fail
    spring boot 自定redis缓存注解
    flash attention的CUDA编程和二维线程块实现softmax
    fontforge将.woff文件转换为.ttf文件,查看字体对应关系
    振弦采集模块测量振弦传感器的流程步骤?
    QtWebEngine性能问题
  • 原文地址:https://blog.csdn.net/SSS4362/article/details/127806394