The use of XML, annotations to assemble the bean, because of its convenient and fast, by everyone's favorite. The general form is as follows:
1 @Autowired 2 Private Beanobjectinterface Beanobject;
However, in some special scenarios, it is necessary to have the object as a bean in spring management, and to get the bean in the code immediately, and to assemble it in the form of annotations is not enough to meet the requirements.
Looking for data, Spring provides the Applicationcontextaware interface, which facilitates real-time access to beans.
First, create a Util class that inherits the Applicationcontextaware interface:
1 Public classSpringcontextutilImplementsApplicationcontextaware {2 3 //Spring Application Context Environment4 Private StaticApplicationContext ApplicationContext;5 6 /**7 * Implement the callback method of the Applicationcontextaware interface, set the context environment8 * @paramApplicationContext9 * @throwsbeansexceptionTen */ One Public voidSetapplicationcontext (ApplicationContext ApplicationContext)throwsbeansexception { ASpringcontextutil.applicationcontext =ApplicationContext; - } - the Public StaticApplicationContext Getapplicationcontext () { - returnApplicationContext; - } - + /** - * Get Objects + * @paramname A * @returnObject one with type at * @throwsbeansexception - */ - - Public StaticObject Getbean (Class requiredtype)throwsbeansexception { - returnApplicationcontext.getbean (requiredtype); - } in - /** to * Get Objects + * @paramname - * @returnObject An instance of a bean registered with the given name the * @throwsbeansexception * */ $ Public StaticObject Getbean (String name)throwsbeansexception {Panax Notoginseng returnApplicationcontext.getbean (name); - } the + /** A * Get an object of type Requiredtype the * If the bean cannot be converted by type, the corresponding exception will be thrown (beannotofrequiredtypeexception) + * @paramName Bean Registry Name - * @paramrequiredtype return Object type $ * @returnobject returns requiredtype Type objects $ * @throwsbeansexception - */ - Public StaticObject Getbean (String name, Class requiretype)throwsbeansexception{ the returnApplicationcontext.getbean (name, requiretype); - }Wuyi the /** - * Returns True if Beanfactory contains a bean definition that matches the given name Wu * @paramname - * @returnBoolean About */ $ Public Static BooleanContainsbean (String name) { - returnApplicationcontext.containsbean (name); - } - A /** + * Determine whether the bean defined as registered in the given name is a singleton or a prototype. the * If the bean definition corresponding to the given name is not found, an exception will be thrown (nosuchbeandefinitionexception) - * @paramname $ * @returnBoolean the * @throwsnosuchbeandefinitionexception the */ the Public Static BooleanIssingleton (String name)throwsnosuchbeandefinitionexception { the returnApplicationcontext.issingleton (name); - } in the /** the * @paramname About * @returntype of Class registration object the * @throwsnosuchbeandefinitionexception the */ the Public StaticClass GetType (String name)throwsnosuchbeandefinitionexception { + returnApplicationcontext.gettype (name); - } the Bayi /** the * If the given bean name has an alias in the bean definition, these aliases are returned the * @paramname - * @return - * @throwsnosuchbeandefinitionexception the */ the Public StaticString[] Getaliases (String name)throwsnosuchbeandefinitionexception { the returnapplicationcontext.getaliases (name); the } - the}
The corresponding bean configuration is then added to the Appcontext.xml file so that spring can load springcontextutil at project startup, completing ApplicationContext initialization.
If you are loading beans using annotation scanning, you need to label the @Component above Springcontextutil:
1 @Component 2 Public class Implements Applicationcontextaware {3 // ... Content no longer repeat 4 }
References:
HTTPS://WWW.JIANSHU.COM/P/4145F507F3E7HTTP://BLOG.KAZAFF.ME/2014/12/05/@Scope (prototype)%e7%9a%84%e6%ad%a3%e7% A1%ae%e7%94%a8%e6%b3%95/https://www.ktanx.com/blog/p/326p.s. There is also a short book blog that cannot be found. There are many ways to learn from it.
Spring: Assembling Beans with Applicationcontextaware