Spring之服务器启动加载bean.xml

涉及Spring和Web项目整合,需要导入:


1.创建 Service ,Dao,bean.xml

MyService.java

package zh.spring.service;

import javax.annotation.Resource;

import zh.spring.dao.MyDao;

public class MyService {

	@Resource(name="myDao")
	private MyDao myDao;

	public void dao() {
		myDao.dao();
	}

}

Dao.java

package zh.spring.dao;

public class MyDao {

	public void dao() {
		System.out.println("dao...");
	}

}

在src下创建bean.xml。

bean.xml

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 开启注解扫描,扫描zh.spring.* 和  zh.struts.*等包里面的注解 -->
	<context:component-scan base-package="zh.spring"></context:component-scan>

	<bean id="myService" class="zh.spring.service.MyService" scope="singleton"></bean>
	<bean id="myDao" class="zh.spring.dao.MyDao" scope="singleton"></bean>

</beans>

2. 创建Action和struts.xml

MyAction.java

package zh.struts.action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import zh.spring.service.MyService;

import com.opensymphony.xwork2.ActionSupport;

public class MyAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
		MyService myService = (MyService) applicationContext.getBean("myService");
		myService.dao();
		return NONE;

	}

}

在src下创建struts.xml。

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="SpringWeb" namespace="/" extends="struts-default">
		<action name="myAction" class="zh.struts.action.MyAction"></action>
	</package>
</struts>

3.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

	<!-- 配置Struts核心拦截器 -->
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
</web-app>

4.测试

服务器启动时,不加载bean.xml。

而是,每访问一次:http://localhost:8080/Spring1/myAction.action,就加载一次bean.xml。


实际开发中,要求bean.xml只被加载一次。

解决:服务器启动时,会创建ServletContext,ServletContextListener监听器可以监听到ServletContext对象的创建。

因此,在web.xml中注册ServletContextListener监听器,在监听器里面加载bean.xml,将创建的对象存储于域对象。

5.服务器启动时加载bean.xml

Sping已经解决了上述问题,只需要在web.xml中配置 ContextLoaderListener 即可。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

	<!-- 配置Struts核心拦截器 -->
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
  	<!-- 配置监听器 和 参数:服务器启动时,加载指定路径下的bean.xml -->
  	<listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  	</listener>
  	
  	<context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:bean.xml</param-value>
  	</context-param>
  
</web-app>

启动服务器,发现已经加载过了bean.xml。


猜你喜欢

转载自blog.csdn.net/qq_41706150/article/details/81055061