• spring学习第四天_Spring Ioc基于注解的形式


    配置文件的实现方式在bean对象比较多的情况下,配置文件会越来越复杂,这时我们可以通过扫描加注解的方式来简化我们的操作。

    1 添加AOP支持依赖

    基于注解的方式需要用到AOP的支持,所以我们需要添加AOP的依赖
    在这里插入图片描述

    2 添加扫描

    通过context标签的component-scan属性指定我们的扫描路径

    <?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: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/context			http://www.springframework.org/schema/context/spring-context.xsd
    	"
    	>
    
    <!--  扫描指定包下的 带有Component标签的类,纳入spring管理 -->
         <context:component-scan base-package="com.zyz.spring"></context:component-scan>  
            
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    @Component注解
    在需要注册到容器的类上添加@Component标签,标识这个类由Spring容器接管

    import org.springframework.stereotype.Component;
    
    @Component
    public class Pet {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    @Resource和@Autowired
    在注解的场景下依赖注入我们可以通过 @Resource和@Autowired实现,他们的区别是@Resource默认是根据name属性来查找的,而@Autowired注解默认是根据类型查找的,@Autowired需要和@Qualifier配合实现基于name的查找。
    @Autowired+@Qualifier=@Resource在实现上

    	@Resource(name="daoMysql")
    	//@Autowired
    	//@Qualifier("daoMysql")
    	UserDao dao;
    
    • 1
    • 2
    • 3
    • 4

    注入的注解
    常用的注解以及使用范围

    • @Component 一般用在身份不明确的组件上
    • @Service 一般用在Service层
    • @Controller 一般用在控制层
    • @Repository 一般用在数据库访问层

    设置特殊的扫描条件
    context:component-scan base-package
    在这里插入图片描述
    基于注解实现Java配置类
    @Configuration相当于applicationContext.xml,可以实现IOC

    package com.zyz.spring;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /*** Java配置类 * 被@Configuration注解标注的Java类就是一个配置类 * 作用和 applicationContext.xml 是一样的 */ 
    @Configuration
    public class JavaConfig {
    
    	/*** 被@Bean注解修饰的方法 等价于 *  * 默认的name为方法的名称 * @return */ 
    	@Bean("abc") 
    	public UserBean getUserBean(){ 
    		return new UserBean("张无忌", 18); }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    UserBean类

    package com.zyz.spring;
    
    public class UserBean {
    
    	private String name;
    	private int age;
    	public UserBean(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	@Override
    	public String toString() {
    		return "UserBean [name=" + name + ", age=" + age + "]";
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    测试启动类

    package com.zyz.spring;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class AppStart {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		// 基于Java配置类的 IoC初始化 
    		
    		ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class); 
    		// 获取实例 
    		UserBean user = ac.getBean("abc", UserBean.class); 
    		System.out.println(user.toString());
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    图神经网络-GraphSage
    python 文件分割成几份
    4. null 和 undefined区别?
    保姆级 Keras 实现 Faster R-CNN 十三 (训练)
    某农商银行IT智能运维一体化建设研究与实践
    HTML+CSS期末网页设计前端作品(大三)
    【HuggingFace文档学习】Bert的token分类与句分类
    sql server 分区表
    汇付国际亮相第五届进博会“2022贸易数字化促进跨境电商发展论坛”
    哈希(hash)——【C++实现】
  • 原文地址:https://blog.csdn.net/qq_37200262/article/details/126652513