Spring out of container management creates objects for dependency injection

Source: Internet
Author: User

We also sometimes encounter instances of classes that are created from the spring container and how to inject objects within 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 this detached container object, which returns objects created from the container, except that the creation of the object is given to spring.

We can use it (maybe it's more, Because the object created by this out-of-container might have other frames created), Org.springframework.beans.factory.config.AutowireCapableBeanFactory.autowireBean (object Existingbean), pass in the This object.

code example:

/* Copyright (C) 2014-now () the spring4-2015 Authors * * https://github.com/sdcuike * * Licensed under the Apache Lic Ense, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License.  * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicable Law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warra Nties 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-> out of container management created objects for dependency injection * * out of container management created objects for Dependency injection * * @see <a href= "Http://www.javacodegeeks.com/2012/09/wire-o Bject-dependencies-outside-spring.html ">http://www.javacodegeeks.com/2012/09/ wire-object-dependencies-outside-spring.html </a> * * @author doctor * * @time June 16, 2015 morning 9:33:54 */public class wireobjectdependenciesoutsidespring {@Rulepublic expectedexception expectedException = Expectedexception.none ();/** * Instantiate the bean and then inject dependencies using Autowirecapablebeanfact Ory.autowirebean (instance) * Look at all the beans in the output container and there is no person object in the container. Just create returns this object and inject the dependencies within that 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::p rintln); Applicationcontext.close () ;} /** * Instantiate the bean and then inject dependencies using Autowirecapablebeanfactory.autowirebean (instance) */@Testpu Blic 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::p rintln); 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 {} @Configurable (Autowire = autowire.by_type, Dependencycheck = True) Static class Person2 {@ Resourceprivate applicationcontext context;public applicationcontext getcontext () {return context;}}}

One of the examples above is implemented with AOP, but to turn on Java options and so on, there is an error in Tomcat under the web, not studying carefully.

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

Spring out of container management creates objects for dependency injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.