Spring整合web項目


Spring整合web項目


UserDao.java
package com.cn.dao;

public class UserDao {
public void add(){
System.out.println("dao.........");
}
}
UserService.java
package com.cn.service;

import com.cn.dao.UserDao;

public class UserService {
private UserDao userDao;

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public void add(){
System.out.println("service.........");
userDao.add();
}
}
UserAction.java
package com.cn.action;

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

import com.cn.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("action.........");
ApplicationContext context=
new ClassPathXmlApplicationContext("bean1.xml");
UserService userService=(UserService) context.getBean("userService");
userService.add();
return NONE;
}
}
Spring配置文件 bean1.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">

<bean id="userService" class="com.cn.service.UserService">
<property name="userDao" ref="userDao"></property>

</bean>
<bean id="userDao" class="com.cn.dao.UserDao"></bean>
</beans>
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="demo1" extends="struts-default" namespace="/">
<action name="userAction" class="com.cn.action.UserAction"></action>
</package>

</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring_day_web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 指定spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean1.xml</param-value>
</context-param>
<!-- struts攔截器配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 配置監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts攔截器過濾路徑 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>




運行圖







 1 演示問題
    (1)action調用service,service調用dao
      每次訪問action時候,都會加載spring配置文件
2 解決方案
   (1)在服務器啟動時候,創建對象加載文件。
    (2)底層使用監聽器,Servicontext對象.


3  在spring里面不需要我們自己寫代碼實現,幫封裝
    (1)封裝了一個監聽器,只需要配置監聽器就可以了。
    (2)配置監聽器之前做事情,導入sping整合web項目jar包
    



     (3) 指定加載spring配置文件位置(系統默認為/WEB-INF/applicationContext.xml)

    所以我們要修改配置文件的位置

改完之后我們會看到,重啟服務器,然后會看到




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com