Ten years Ali, only this Java development system is left >>>
As we all know, when the project starts, spring reads the XML file, instantiating all the configured beans or the Controller service DAO under annotations. Then inject it into the code to use. So how do we get an instantiated bean on our own? It's useless to own new.
For a scene,
Suppose I wrote a class
public class A{@Resourceprivate static ADao aDao;static{ aDao.select();}public static void getA(){ ...}}
When the other class calls A.geta (), the call is static method, A is not instantiated, just initialize, that is to say adao-null;
May think I set this class A very wonderful, but some of the scene is this, in order to improve performance, I only allow Chaku once, later read memory. (not put to redis for security), so use a static block of code that will only be executed the first time it is initialized.
This time I need to get the Adao myself.
So how do you get it?
First, we need to know a few questions.
1, Adao has been instantiated, just in the spring container, still do not know how to fetch.
2, if using the MyBatis interface programming, then Adao is just an interface, his implementation class is Mapperfactorybean, not itself,
<bean id= "ADap" class= ".../.../adao"/> This is wrong.
3, ApplicationContext context=new classpathxmlapplicationcontext ("Spring-config.xml");
Context.getbean ("Beanname");
This is the usual way to load a configuration file to instantiate a bean when you do not start spring.
In a normal startup project, this is not the case and will cause spring to re-instantiate and re-establish the database connection pool. This is the equivalent of restarting a running project, which is very serious.
So we're going to use spring to provide the interface to us.
Method One: Implement the Beanfactoryaware interface
The code is as follows:
@Service Public class beanutils implements beanfactoryaware {//Spring's Bean factoryPrivateStaticBeanfactory beanfactory;@OverridePublic void setbeanfactory(beanfactory Factory) throws Beansexception{beanfactory=factory; }Public static<T> T getbean(String beanname) {return(T) Beanfactory.getbean (beanname); }}
The meaning of this class is roughly so many
1, implement the Beanfactoryaware interface, this interface allows us to get an instance of the Bena factory after the project is started, so we can get the bean instance we want.
The 2,setbeanfactory () method is a spring dependency injection, and the set method is one of three injection methods. Such instances are injected into the class.
Now that we have to inject, we have to set beanutils this class to be instantiated by spring when the project is started, and I am using annotations to @service.
3, static Getbean method, here the method must be static, because if you go to the new Beanutils class yourself, beanfactory will not be injected into.
Method Two, a very simple use.
ContextLoaderListener.getCurrentWebApplicationContext().getBean ("beanName");
Use the context listener to get the context, and then get the bean, this method of careful classmate should be able to see, this is the above I said a change of the wrong usage. 3, ApplicationContext context=new classpathxmlapplicationcontext ("Spring-config.xml"); In a nutshell, new doesn't work, Taking out of the container is the right way.
About Contextloaderlistener This listener is not much to say.
There are only two types listed here, and there are several ways to get them.
If it is wrong to explain, I hope the big God pointing under the younger brother, after all, their own groping, will inevitably make mistakes.
spring-How to get a bean instance in case of project startup