码迷,mamicode.com
首页 > 其他好文 > 详细

注入--注解---扫描

时间:2015-05-30 18:12:36      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

@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();
    }
}

 

注入--注解---扫描

标签:

原文地址:http://www.cnblogs.com/jsnan/p/4540675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!