<context:annotation-config/>的原理         版本       Spring3.1

1.    定义Student类

package cn;
/**
 * Student类
 * @author Administrator
 *
 */
public class Student {
    /**
     * 定义学生说话的方法
     */
    public void say(){
        System.out.println("学生说话");
    }

}

2.    定义Person类

package cn;
/**
 * Student类
 * 
 */
import javax.annotation.Resource;

public class Person {
    /**
     * @Resource是依赖注解
     *
     */
    @Resource
    private Student student;
    
    public void say(){
        this.student.say();
    }
}

3.    定义配置文件

<?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-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <!-- 
        1.导入context命名空间
        xmlns:context="http://www.springframework.org/schema/context"
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd"
     -->
    <!-- 2.注解配置解析器 用于依赖注入 -->
    <context:annotation-config/>
    <!-- 3.在所需要注解的地方配置@Resource -->
    
    <!-- 配置bean -->
    <bean id="student" class="cn.Student"></bean>
    <bean id="person" class="cn.Person"></bean>

</beans>

4.    定义测试类

package cn;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 测试类   
 *
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = context.getBean("person",Person.class);
        person.say();
    }

}

结果显示:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
学生说话

<context:annotation-config/>的原理

1.启动Spring容器,并且加载配置文件

2.创建对象

3.当解析到<context:annotation-config/>的时候,会启动依赖注入的解析器

4.会在纳入Spring管理的bean的范围内,看那些bean的属性上有@Resource注解

5.如果@Resource注解的值为"",那么会把注解所在的属性的名称和Spring容器中bean的id进行匹配, 如果匹配成功,则把id对应的对象赋值给该属性,如果匹配不成功,则按照类型进行匹配。

6.如果@Resource注解的值不为"",那么会把name属性的值和Spring容器中的bean的id匹配,如果匹配成功,则赋值;如果匹配不成功,则报错

时间: 2024-07-28 17:47:04

<context:annotation-config/>的原理         版本       Spring3.1的相关文章

[org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on

<2015-4-10 下午02时55分35秒 CST> <Info> <Security> <BEA-090905> <Disabling the CryptoJ JCE Provider self-integrity check for better startup performance. To enable this check, specify -Dweblogic.security.allowCryptoJDefaultJCEVerifica

Context namespace element &#39;annotation-config&#39; and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser]

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class pa

Context namespace element &#39;component-scan&#39; and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nested exception is java.lang.IllegalStateException: Context

Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException

org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'configController' for bean class [net.mingyang.modules.system.ConfigController] conflicts with existing, non-compatible bean definition of same

context、config

Tomcat启动时已经创建了context,并使用它读取了web.xml中的参数,后台可以从context里获取参数 后台获取参数代码: ServletContext context = getServletContext(); String size = context.getInitParameter("size"); web.xml中参数配置: <context-param> <param-name>size</param-name> <

(五)Spring Cloud教程——Config(F版本)

参考:方志鹏的专栏 Spring Cloud Config 实现配置中心,看这一篇就够了 1. 简介 Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Cloud 项目,通过简单的配置即可实现功能. 对于一些简单的项目来说,我们一般都是直接把相关配置放在单独的配置文件中,以 properties 或者 yml 的格式出现,更省事的方式是直接放到 appli

Flex Viewer(三)——Config的原理

一.概述 在上文<深入浅出Flex Viewer(二)——体系结构>中,笔者详细介绍了到Flex Viewer框架,使得读者能够对该框架源代码的关键目录和文件结构和这些文件中所包含或涉及到的系统的哪些构件:以及这些构件间的逻辑关系和连接这些构件所用的关键技术,如:消息总线(EventBus).配置项管理(Config Manager).数据共享机制(DataManager)等内容有一个大概地了解.在本文中将继续介绍Flex Viewer中框架配置技术的设计和实现原理,及其和框架其它组件间的关系

Spring Cloud Config Client原理讲解

# Spring Cloud Config Client ## 预备知识 ### 发布/订阅模式 `java.util.Observable` 是一个发布者 `java.util.Observer` 是订阅者 发布者和订阅者:1 : N 发布者和订阅者:N : M ### 事件/监听模式 `java.util.EventObject` :事件对象 ? * 事件对象总是关联着事件源(source) `java.util.EventListener` :事件监听接口(标记) ## Spring 事件

ibatitsnet 因为会Dao.config 配置数据版本太低导致的问题

ProjectReview.Test.SqlMapTest.TestSqlMap:IBatisNet.Common.Exceptions.ConfigurationException : - The error occurred while configure DaoSessionHandler.- The error occurred in <property name="resource" value="App_Data\sqlMap.config" xm