Bean creation process, implementation of Spring beanfactory, spring aware

Source: Internet
Author: User

The bean creation process:

Spring Beanfactory's Factory mode implementation:

Parse <bean> tag, save to a map<string, object> beans

People o = (people) Factory.getbean ("P");

Because the map has an object type, you need to force the type conversion when you remove it

Bean's life cycle:

The purpose of spring aware is to let the bean get the service of the spring container, and the bean inherits Applicationcontextaware can get all the services of the spring container, but in principle it still uses what interface, inherit what interface. (e.g. Beannameaware,beanfactoryaware ...)

Beannameaware Interface : Gets the id attribute of the bean itself.

void Setbeanname (String name)

The name parameter of the method is the ID property of the bean, and the call to the Setbeanname method allows the bean to obtain its own ID property

@Servicepublic class Beannameawaretest implements Beannameaware {

private String name;

@Override

Public void Setbeanname (String name) {

System.out.println ("Setbeanname method, ID is" +name);

THIS.name = name;

}

Public String Getbeanname () {

return this.name;

}

}

Get Bean:

@Autowiredprivate beannameawaretest beannameawaretest;

public void Aware () {

System.out.println ("Beanname:" +beannameawaretest.getbeanname ());

Beannameawaretest.setbeanname ("Beanname");

System.out.println ("Beanname:" +beannameawaretest.getbeanname ());

}

Results:

The Setbeanname method, which is called automatically when Beannameawaretest//bean is instantiated.

Beanname:beannameawaretest

Setbeanname method, ID is the Setbeanname method that beanname//manually calls Beanname:beanname


Beanfactoryaware Interface : An instance reference for the corresponding beanfactory

void Setbeanfactory (Beanfactory beanfactory)

By creating its beanfactory instance with the parameters of this method, the Beanfactoryaware interface is implemented, allowing the bean to have the ability to access the spring container. Cons: Causes the code to be coupled with the spring API.

@Service

public class Beanfactoryawaretest implements Beanfactoryaware {

Private Beanfactory beanfactory;

@Override

public void Setbeanfactory (Beanfactory beanfactory) throws Beansexception {

This.beanfactory = beanfactory;

}

}

Get bean Factory for bean operation:

@Autowired

Private Beanfactoryawaretest beanfactoryawaretest;

public void Aware () {

System.out.println ("Get a bean from the Bean Factory:"+beanfactoryawaretest.getbeanfactory (). Getbean (" Beanfactoryawaretest "));

}


Applicationcontextaware Interface : Injected ApplicationContext instance

Setapplicationcontext (ApplicationContext context)

Using the parameter context to create a applicationcontext instance of it, the disadvantage is that it causes the code to be coupled with the spring API.

Public class Contextawaretest implements Applicationcontextaware {

@Override

public void Setapplicationcontext (ApplicationContext arg0) {

applicationcontext = arg0;

people= (people) Applicationcontext.getbean ("people");

}

}

Usage scenarios:

For example, depending on the environment, Target needs to correspond to different resources, and you can load a source class first, which has an environment-related property mode;

Get Beanfactory,getbean (source) then judge its Mode property, make different processing, get the same bean, assign to target


Bean creation process, implementation of Spring beanfactory, spring aware

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.