`
wangchen2009hao
  • 浏览: 8604 次
社区版块
存档分类
最新评论

Spring注解<context:annotation-config/>使用

阅读更多

 

<?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"
       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
           ">
           
        <context:annotation-config></context:annotation-config>
          
		<bean id="oracleDao" class="dao.OracleDao"></bean>
		<bean id="anoService" class="service.AnoService"></bean>
          
</beans>

        先上配置文件,发现了吧<context:annotation-config/>这个东西,配合

 

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-2.5.xsd

 

    才能开启注解注入模式。上代码:

     业务bean

    

package service;

import javax.annotation.Resource;

import dao.OracleDao;

public class AnoService {
	
	@Resource
	private OracleDao oracleDao;
	
	public void save() {
		oracleDao.save();
	}

}

 

   模拟OracleDao

   

package dao;

public class OracleDao  {
	
	public  void save() {
		System.out.println("模拟Oracle的保存方法");
	}
	
	
	
}

 

   junit测试类

   

package test;

import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.AnoService;

public class TestAnoService {

	@Test
	public void test() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
		
		AnoService anoService = (AnoService)applicationContext.getBean("anoService");
		anoService.save();
	}

}

 

结果:

2014-3-3 21:24:06 org.springframework.context.support.AbstractApplicationContext prepareRefresh

信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41d05d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@41d05d]; startup date [Mon Mar 03 21:24:06 CST 2014]; root of context hierarchy

2014-3-3 21:24:06 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [beans.xml]

2014-3-3 21:24:06 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@41d05d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15aed57

2014-3-3 21:24:06 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15aed57: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,oracleDao,anoService]; root of factory hierarchy

模拟Oracle的保存方法

 

   乐了吧,成功了!

 

 

    总结一下:

  1. 配置文件得开启注解注入模式
  2. 使用注入的类(例如:AnoService)你也得交给容器管理,你自己new出来他,容器是不会给你注入的,原理是Spring在初始化AnoService的时候发现里面有需要注入的对象,容器就把这个对象注入进去,所以你自己new的对象容器是不知道他需要注入的。

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    异常解决:错误:namespace element 'annotation-config' … on JDK 1.5 and higher

    2. **XML配置问题**:确保你的Spring配置文件(如`applicationContext.xml`)正确包含了`&lt;context:component-scan&gt;`或`&lt;context:annotation-config&gt;`元素,它们是启用注解配置的关键。 3. **编译器设置**:检查你的...

    spring_MVC源码

    10. &lt;context:annotation-config /&gt; 11. &lt;!-- 把标记了@Controller注解的类转换为bean --&gt; 12. &lt;context:component-scan base-package="com.mvc.controller" /&gt; 13. &lt;!-- 启动Spring MVC的注解功能,...

    Spring注解详解

    &lt;context:annotation-config /&gt; ``` 这将隐式地向 Spring 容器注册 `AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`PersistenceAnnotationBeanPostProcessor` 和 `...

    SPring注解及页面跳转实例

    - 使用`&lt;context:component-scan&gt;`标签扫描控制器所在的包。 - 配置`InternalResourceViewResolver`用于解析视图。 ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    Spring的Annotation配置相关讲义

    `&lt;context:annotation-config&gt;`元素会扫描容器中的所有Bean,查找并处理如`@Autowired`、`@Required`、`@PostConstruct`等注解,实现依赖注入。 `&lt;context:component-scan&gt;`元素用于指定需要扫描的包,这样Spring会...

    spring注解

    &lt;context:annotation-config /&gt; ``` 这将隐式地向 Spring 容器注册 `AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`PersistenceAnnotationBeanPostProcessor` 和 `...

    spring注解使用

    `&lt;context:annotation-config/&gt;` 元素用于激活注解驱动的 Bean, `&lt;context:component-scan&gt;` 元素用于自动扫描指定包下的 Bean。 最后,我们可以编写主类来测试: ```java @Service public class Main { @...

    spring使用annotation整合dwr笔记

    - **配置DWR Annotation Config**:利用`&lt;dwr:annotation-config/&gt;`标签启用注解支持,使得开发者能够使用`@RemoteProxy`和`@RemoteMethod`等注解来标注需要暴露给客户端的方法。 ```xml &lt;!-- 配置DWR注解支持 --&gt;...

    Spring基于注释(Annotation)的配置.pdf

    ### Spring基于注释...`&lt;context:annotation-config/&gt;`是Spring提供的一个配置选项,它可以自动注册多个BeanPostProcessor,包括`AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`...

    Spring 注解

    本文将详细介绍Spring 2.5及其后续版本中的注解配置方式,包括常见的注解如`@Autowired`、`@Resource`以及如何使用`&lt;context:annotation-config/&gt;`简化配置等。 #### Spring 2.5 的注释 Spring 2.5 版本中,为了更...

    SSH全注解环境搭建

    &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.zhaolongedu"/&gt; ``` - 配置Spring事务管理器: ```xml &lt;bean id="transactionManager" class="org.springframework.orm.hibernate5...

    spring4注解

    &lt;context:annotation-config/&gt; &lt;context:annotationconfig/&gt; ``` 这两种方式都会隐式地向Spring容器注册四个`BeanPostProcessor`实例:`AutowiredAnnotationBeanPostProcessor`、`...

    spring mvc基础

    &lt;param-value&gt;classpath:config/springAnnotation-*.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- 启动Spring上下文监听器 --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context....

    Spring2.5_注解介绍(3.0通用).pdf

    2. **方式二:使用命名空间`&lt;context:annotation-config/&gt;`** - 这种方式会自动注册多个注解处理器,包括`AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.mvc" /&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-...

    集成springmvc spring hibernate的配置

    &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.mvc.*"&gt; &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt; &lt;/context:component...

    自己写网页spring框架搭建

    - 使用`&lt;context:annotation-config/&gt;`来启用Spring MVC的注解支持。 - **控制器扫描**: - 通过`&lt;context:component-scan base-package="controller"/&gt;`指定要扫描的控制器包名。 - **视图解析器**: - 配置视图...

Global site tag (gtag.js) - Google Analytics