Spring uses "1" and spring uses "1"
The previous article explains how to set up a spring development environment. A brief review is to import the jar package of spring into the project. If the jar package is in the lib directory of the Java Web project, then in the web. configure in the xml file and configure the path of the spring configuration file. In the previous article, I forgot to paste the spring configuration file,
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="address" class="com.cn.test.spring.Address"></bean> </beans>
From the configuration file above, we can see that a bean is configured with the ID address, and the class's full-limit class name is com.cn. test. spring. Address. Since spring is used, the creation of classes is managed by spring. in java code, we do not need to use the new keyword to create an object, so how to obtain an object of the class created by spring is the focus of this article.
How to obtain an instance object from the spring Environment
To obtain an instance of a class from spring, you can use the context ApplicationContext object of spring. ApplicationtContext is an interface, also known as the IOC container, and a BeanFactory container. It has several important implementation classes: ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, and XmlWebApplicationContext.
ClassPathXmlApplicationContext
Read the configuration file from the class path, that is, read the spring configuration file from src, as follows,
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( "spring-application.xml"); Address address = (Address) appContext.getBean("address");
Read the spring-application.xml from the class path and get the Instance Object for Address
This method is used to obtain the IOC container under the java project and obtain the instance of the class.
FileSystemXmlApplicationContext
Read the configuration file from the file system. This class is rarely used.
XmlWebApplicationContext
Obtain the applicationContext container in jsp or servlet. The following shows how to obtain applicationContext in servlet,
@Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub WebApplicationContext wac=WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()); //XmlWebApplicationContext wac=(XmlWebApplicationContext) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
Address address=(Address)wac.getBean("address"); address.printInfo(); }
The above code is the init method in servlet. In this method, you can obtain the IOC container in the javaWeb environment (provided that spring has been configured in web. xml ). The preceding two methods can be used. If you want to use the IOC container externally, you can start from here.
The following is a supplement to configuring the spring environment in the javaWeb environment,
In Java projects, it is most common to manually instantiate the ApplicationContext container through the ClassPathXmlApplicationContext class. However, the Web project cannot be started, and the startup of the Web project is the responsibility of the corresponding Web server. Therefore, the instantiation of the ApplicationContext container in the Web project should be done by the Web server. Take the tomcat server as an example.
There are two ways to configure the spring environment in the javaWeb environment: listener and servlet. Both methods use the configuration file, by default, it is the application-context.xml under the class path. If not, you must use the <context-param> label to specify a way to view the previous article.
Use listener
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Use servlet
This method is used to take into account Servlet2.3 and the following specifications of Servlet containers. In tomcat5, servlet2.4 is supported, so the first method is mainstream,
<servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
In summary, spring is used in java projects.
If any error exists, please note that
Thank you.