Spring uses "1" and spring uses "1"

Source: Internet
Author: User

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.

 

Related Article

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.