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的对象容器是不知道他需要注入的。

 

猜你喜欢

转载自wangchen2009hao.iteye.com/blog/2026795