我们有时候也会遇到一些脱离spring容器创建的类实例,如何把spring容器内的对象注入到这些类实例内呢。

      我们可以用org.springframework.beans.factory.config.AutowireCapableBeanFactory.createBean(Class<?> beanClass, int
 autowireMode, boolean dependencyCheck) 来创建这个脱离容器的对象,该方法返回脱离容器创建的对象,只不过对象的创建交给spring了。

    我们可以用(也许这情况比较多,因为此脱离容器创建的对象也许会有其它框架创建)org.springframework.beans.factory.config.AutowireCapableBeanFactory.autowireBean(Object existingBean),把this对象传进来。

  代码示例:

/*
 * Copyright (C) 2014- now() The  spring4-2015  Authors
 *
 * https://github.com/sdcuike
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.doctor.spring4.blog.code;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.stream.Stream;

import javax.annotation.Resource;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;

/**
 * Wire object dependencies outside a Spring Container->脱离容器管理创建的对象进行依赖注入
 * 
 * 脱离容器管理创建的对象进行依赖注入
 * 
 * @see <a href= "http://www.javacodegeeks.com/2012/09/wire-object-dependencies-outside-spring.html">http://www.javacodegeeks.com/2012/09/wire-object-dependencies-outside-spring.html </a>
 * 
 * @author doctor
 *
 * @time 2015年6月16日 上午9:33:54
 */
public class WireObjectDependenciesOutsideSpring {

	@Rule
	public ExpectedException expectedException = ExpectedException.none();

	/**
	 * Instantiate the bean and then inject dependencies using AutoWireCapableBeanFactory.autowireBean(instance)
	 * 看下输出容器内的所有bean,容器内并没有person对象。只是创建返回此对象,并对该对象内的依赖进行注入。
	 */
	@Test
	public void test_create_bean_and_inject_dependencies() {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
		Person person = (Person) applicationContext.getAutowireCapableBeanFactory().createBean(Person.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);

		assertNotNull(person.getContext());
		Stream.of(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
		applicationContext.close();
	}

	/**
	 * Instantiate the bean and then inject dependencies using AutoWireCapableBeanFactory.autowireBean(instance)
	 */
	@Test
	public void test_only_inject_dependencies() {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
		Person person = new Person();
		assertNull(person.getContext());
		applicationContext.getAutowireCapableBeanFactory().autowireBean(person);

		assertNotNull(person.getContext());
		Stream.of(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);

		applicationContext.close();
	}

	 

	@Configuration
	static class SpringConfig {

	}

	static class Person {
		@Resource
		private ApplicationContext context;

		public ApplicationContext getContext() {
			return context;
		}

	}

	@Configuration
	@EnableSpringConfigured
	@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
	static class SpringConfig2 {

	}

	@Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true)
	static class Person2 {
		@Resource
		private ApplicationContext context;

		public ApplicationContext getContext() {
			return context;
		}

	}

}


上述示例还有一个是用aop实现,不过要开启java选项之类的,网上还有在tomcat下报错的,没仔细学习。


*      现在看看springJunit怎么实现测试用例实例内依赖注入的,代码:
 *      org.springframework.test.context.support.DependencyInjectionTestExecutionListener
 *      类的方法:
 *
 *      protected void injectDependencies(final TestContext testContext) throws Exception {
 *      Object bean = testContext.getTestInstance();
 *      AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
 *      beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
 *      beanFactory.initializeBean(bean, testContext.getTestClass().getName());
 *      testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
 *      }
 *
 *      是不是一样的原理。呵呵!
 *


参考:http://www.javacodegeeks.com/2012/09/wire-object-dependencies-outside-spring.html

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐