注入--注解---扫描

@Component
public class Student {
    public void say() {
        System.out.println("student say");
    }
}
@Component
public class Person {
    //@Resource(name="student")
    @Resource()
    private Student student;

    public void say(){
        this.student.say();
    }
}
<?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:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:component-scan base-package="com.sn.domain"></context:component-scan>
</beans>
/**
 * 原理
 *    1、启动spring容器
 *    2、spring容器解析
 *         <context:component-scan base-package="com.itheima10.spring.scan">
               </context:component-scan>
        3、在base-package指定的包及子包中扫描,看哪些类上面是否含有@Component注解
        4、如果有该注解
            @Component
        public class Person {
        }
        ==
        <bean id="person" class="..Person">
        @Component("aa")
        public class Person {
        }
        ==
        <bean id="aa" class="..Person">
       5、按照@Resource的解析步骤执行
    说明:
         整个过程扫描两次,效率越来越低,书写越来越简单
 * @author zd
 *
 */
public class AnnotationTest {
    @Test
    public void testAnnotation(){
        ApplicationContext context =
                 new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person)context.getBean("person");
        person.say();
    }
}
时间: 2024-08-19 04:26:41

注入--注解---扫描的相关文章

spring注解与springMVC注解扫描的问题

在将spring与springMVC结合使用时,当我们使用注解的时候,一般都是在spring配置文件中配置注解扫描dao层.service层的包,在springMVC配置文件中配置注解扫描controller,自己在练习spring+SpringMVC+mybatis的项目时对这种做法一知半解,所以在练习项目的时候在实践中对自己的一些想法进行了验证. 一般的配置 spring配置文件中 <?xml version="1.0" encoding="UTF-8"?

谈谈Spring的IoC之注解扫描

问题 ??IoC是Inversion of Control的缩写,翻译过来即"控制反转".IoC可以说是Spring的灵魂,想要读懂Spring,必先读懂IoC.不过有时候硬着头皮直接看源码,就像雾里看花,并不能一窥真谛.想要理解Spring的IoC,不如反过来思考一下,如果我们自己去实现IoC,需要考虑什么?遵循依赖大于配置的原则,我们这里只讨论注解式的控制反转容器,XML配置的方式暂不考虑.由此我想到下面几个问题: 注解扫描 实例化 循环依赖 泛型注入 本文的重点,就是围绕这几个问

SQLiScanner:一款基于SQLMAP和Charles的被动SQL 注入漏洞扫描工具

项目地址:SQLiScanner 简介 叕一款基于SQLMAP和Charles的被动SQL 注入漏洞扫描工具 从内部安全平台 分离出来的一个模块, 支持 Har 文件的扫描(搭配 Charles 使用: Tools=>Auto Save) 特性 邮箱通知 任务统计 sqlmap 复现命令生成 依赖 Python 3.x Django 1.9 PostgreSQL Celery sqlmap redis 支持平台 Linux osx 截图 安装 克隆项目到本地 git clone https://

傀儡SQL注入批量扫描工具 V6.0

有Access显错扫描.Mysql显错扫描.Mssql显错扫描.And数字注入.And字符注入.Xor注入测试.Svn源代码泄露.备份扫描等多种注入方式扫描,可以批量,具体自己发挥吧 注:此工具为java开发,需要安装java环境 链接: https://pan.baidu.com/s/1kWqJ5WV 密码: guse 原文地址:https://www.cnblogs.com/xyuan7/p/11740334.html

Spring学习笔记——关于Spring注解扫描不能注入new对象问题

这几天Leader让我用工厂模式对部分业务逻辑代码进行重构,过程是痛苦的(这里就不详细说了),结果很甜蜜.下面记录一下我在重构过程中遇到一个问题. 部分代码如下: @service(orderFactory) public class OrderFactory implements IOrderFactory{ public OrderCreate factory(String type){ if(type != null && type.indexOf("1")! =

Spring组件注解和注入注解内部方式的区别

一.@Component.@Repository.@Service.@Controller区别 Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3个注释和 @Component 相

spring IOC快速入门,属性注入,注解开发

我们使用spring框架也会使用到配置文件,我们需要在src下创建一个关于spring的配置文件,一般情况名称叫applicationContext.xml 基本约束: <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> IOC快速入门 inversion of Controller

spring mvc 注解扫描问题 ,扫描不到controller, use-default-filters=&quot;false&quot;

今天搭了个spring mvc项目,怎么也扫描不到controller,最后发现问题在use-default-filters="false"上面,乱copy出的问题 (默认值是true,它的作用就是扫描一些相关的注解,包括了@controller,@Component,@Service,@Repository等,) <context:component-scan base-package="com.example" use-default-filters=&q

Java Spring各种依赖注入注解的区别

Spring对于Bean的依赖注入,支持多种注解方式: @Resource javax.annotation JSR250 (Common Annotations for Java) @Inject javax.inject JSR330 (Dependency Injection for Java) @Autowired org.springframework.bean.factory Spring 直观上看起来,@Autowired是Spring提供的注解,其他几个都是JDK本身内建的注解,