Spring is separated from the objects created by container management for dependency Injection
We sometimes encounter some class instances created out of the spring container. How can we inject the objects in the spring container into these class instances.
We can use org. springframework. beans. factory. config. AutowireCapableBeanFactory. createBean (Class BeanClass, int
AutowireMode, boolean dependencyCheck) to create the object that is detached from the container. This method returns the object created from the container, but the object is created by spring.
We can use (this may be a lot of cases, because the objects created from the container may be created by other frameworks) org. springframework. beans. factory. config. autowireCapableBeanFactory. autowireBean (Object existingBean) to pass in this Object.
Sample Code:
/** 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 before t 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 unde R 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.doc tor. spring4.blog. code; import static org. junit. assert. assertNotNull; import static org. junit. assert. assertNull; import java. util. stream. stream; import javax. an Notation. 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. retriable; import org. springframework. beans. factory. config. autowireCapableBeanFactory; import org. springframework. context. applicationContext; import org. springf Ramework. 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 C Ontainer-> dependency injection from objects created by container management ** @ see http://www.javacodegeeks.com/2012/09/wire-object-dependencies-outside-spring.html ** @ author doctor ** @ time June 16, 2015 9:33:54 */public class WireObjectDependenciesOutsideSpring {@ Rulepublic ExpectedException expectedException = ExpectedException. none ();/*** Instantiate the bean and then inject dependencies using AutoWireCapableBeanF Acloud. autowireBean (instance) * shows all the beans in the output container. There is no person object in the container. Only create and return this object, and inject the dependencies in this object. */@ Testpublic 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) */@ Testpublic 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 () ;}@ Configurationstatic class SpringConfig {} static class Person {@ Resourceprivate ApplicationContext context; public ApplicationContext getContext () {return context ;}} @ Configuration @ EnableSpringConfigured @ EnableLoadTimeWeaving (aspectjWeaving = AspectJWeaving. ENABLED) static class SpringConfig2 {}@ retriable (autowire = Autowire. BY_TYPE, dependencyCheck = true) static class Person2 {@ Resourceprivate ApplicationContext context; public ApplicationContext getContext () {return context ;}}}
Another example is to use aop, but to enable java options and the like, there are errors reported under tomcat on the Internet, not carefully studied.
Reference: http://www.javacodegeeks.com/2012/09/wire-object-dependencies-outside-spring.html