spring extral features-----AnnotationConfigApplicationContext
/** * */package cn.ythd.spring.service.impl;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import cn.ythd.spring.Quality;import cn.ythd.spring.service.UserService;/** * @author * 2013-6-29 */@Quality@Component@Scope(value="prototype")public class UserServiceImpl2 implements UserService {/***----2013-6-29*/public void save(String name, String password) {System.out.println("user service impl 2 save method");}}
? 测试类如下:
/** * */package cn.ythd.spring.test;import junit.framework.Assert;import org.junit.Test;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import cn.ythd.spring.service.UserService;import cn.ythd.spring.service.impl.UserServiceImpl2;/** * @author gongz * 2013-6-30 */public class AnnotationConfigApplicationContextTestDemo {@Testpublic void test1(){//该类继承自GenericApplicationContext 所以也具有动态创建对象、处理依赖的特性AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();//该对象也是一个BeanDefinitionRegistry对象//context.registerBeanDefinition(beanName, beanDefinition)//注册的注解类可以没有任何的注解,但是如果需要一些个特殊需求(例如bean的个数,事务特性)的时候,就需要定义注解 context.register(UserServiceImpl2.class);UserService userService=context.getBean(UserService.class);Assert.assertNotNull(userService);UserService userService2=context.getBean(UserService.class);Assert.assertNotSame(userService, userService2);System.out.println(userService2==userService);//falsecontext.refresh();userService.save("123456", "password");context.close();}}
?
主要通过注解的方式来手动添加要定义的类。