本博客为​​这篇博客​​的辅助博客,主要是其中自己写的一些Demo.java代码


目录

​日志的用法​

​Demo1.java​

​log4j.properties​

​控制反转IOC的测试​

​Demo2.java​

​applicationContext.xml1​

​UserService.java​

​UserServiceImpl.java​

​依赖注入​

​CustomerDaoImpl.java​

​CustomerServiceImpl.java​

​Demo3.java​

​applicationContext.xml2​

​依赖注入的方式2:构造方法​

​Car1.java​

​Person.java​

​Demo4.java​

​applicationContext.xml3​

​依赖注入方式3:p名称空间的注入(纯了解即可)​

​Car2.java​

​applicationContext.xml4​

​Demo4.java1​

​数组,集合(List,Set,Map),Properties等的注入​

​User.java​

​applicationContext.xml5​

​Demo4.java3​

​Spring框架的配置文件分开管理(了解)​

​applicationContext.xml​

​applicationContext2.xml​

​Demo4.java4​

​Spring框架整合WEB(简单的Struts整合)​

​CustomerDao.java​

​CustomerDaoImpl.java​

​CustomerService.java​

​CustomerServiceImpl.java​

​CustomerAction.java​

​struts.xml​

​applicationContext.xml(ZH)​

​导包:spring-web-4.2.4.RELEASE.jar(spring依赖包)​

​web.xml​

​固定配置:​


 

日志的用法

Demo1.java

package com.itheima.demo1;
import org.apache.log4j.Logger;
import org.junit.Test;
/**
* 演示日志用法
* @author Administrator
*
*/
public class Demo1 {

//创建日志对象
private Logger log=Logger.getLogger(Demo1.class);

@Test
public void run1(){
System.out.println("执行了...");
log.info("执行了...");
}
}

log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

### 在此配置文件里将inof改为off 则,所有的日志输出都会失效,就像全部被注释了一样
### 日志文件是spring依赖包里拷贝进来的,默认值就是info
log4j.rootLogger=info, stdout

控制反转IOC的测试

Demo2.java

package com.itheima.demo2;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
* 测试IOC程序
* @author Administrator
*
*/
public class Demo2 {

/**
* 原来的方式
*/
@Test
public void run1(){
//创建实现类
//UserServiceImpl usi = new UserServiceImpl();
//usi.sayHello();
UserService us=new UserServiceImpl();
us.sayHello();
}

/**
* 使用的是Spring框架的方式
*/
@Test
public void run2(){
//创建工厂,加载核心配置文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//一读取配置文件,你配置的对象就全帮你配置完了
//从工厂中获取对象 参数就写配置的id值
UserService us = (UserService) ac.getBean("userService");//一般都强转为接口 写接口拓展容易 接口可以有多个实现类 都能被赋值
//调用对象方法执行
us.sayHello();
}

/**
* 老的工厂版本 过时了 不推荐用
* BeanFactory
*/
@Test
public void run4(){
//创建工厂,加载核心配置文件
BeanFactory factory =new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//从工厂中获取对象
UserService us = (UserService) factory.getBean("userService");//老工厂 getBean时才会创建
//调用对象的方法执行
us.sayHello();

}

/**
* 演示销毁方法(了解)
*/
@Test
public void run5(){
//创建工厂,加载核心配置文件
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//一读取配置文件,你配置的对象就全帮你配置完了
//从工厂中获取对象 参数就写配置的id值
UserService us = (UserService) ac.getBean("userService");//一般都强转为接口 写接口拓展容易 接口可以有多个实现类 都能被赋值
//调用对象方法执行
us.sayHello();
//关闭工厂 里面的对象会全部销毁
ac.close();

}


//依赖注入 入门演示

/**
* 原始方式
*/
@Test
public void run6_0(){
//创建实现类
UserServiceImpl usi = new UserServiceImpl();
usi.setName("云天河");
usi.sayHello();
}

/**
* 依赖注入 : 不用set属性值 直接配置属性值
*/
@Test
public void run6(){
//创建工厂,加载核心配置文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//一读取配置文件,你配置的对象就全帮你配置完了
//从工厂中获取对象 参数就写配置的id值
UserService us = (UserService) ac.getBean("userService");//一般都强转为接口 写接口拓展容易 接口可以有多个实现类 都能被赋值
//调用对象方法执行
us.sayHello();
}

}

applicationContext.xml1

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 使用bean标签 -->
<!-- class是实现类的全路径(不是接口) id随便取的名字 -->
<!-- <bean id="userService" class="com.itheima.demo2.UserServiceImpl" init-method="init" destroy-method="destory"/>
-->
<!--
init-method="init" destroy-method="destory" (了解即可 需要用时来查 不需要记忆 只记忆重点的配置,获得对象的方式下面依赖注入的第一种方式set,这种重点)
配置了之后 对象被创建时会执行javaBean中的init()方法
对象被销毁时会创建javaBean中的destory()方法
-->

<!-- 依赖注入 -->
<bean id="userService" class="com.itheima.demo2.UserServiceImpl">
<!-- 注意name属性需要有set方法 -->
<property name="name" value="云天河" /><!-- 创建对象时帮你把属性辅助 -->
</bean>

</beans>

UserService.java

package com.itheima.demo2;

public interface UserService {
public void sayHello();
}

UserServiceImpl.java

package com.itheima.demo2;

public class UserServiceImpl implements UserService {

private String name;
public void setName(String name) {
this.name = name;
}

@Override
public void sayHello() {
System.out.println("hello spring!! "+name);
}

//创建对象之前 框架会先调用init方法(前提配置了)
public void init(){
System.out.println("UserServiceImpl创建了...");
}

//销毁时调用此方法
public void destory(){
System.out.println("UserServiceImpl销毁了...");
}

}

依赖注入

(也是最常用的一种方式 即set属性方式)

CustomerDaoImpl.java

package com.itheima.demo3;

public class CustomerDaoImpl {
public void save(){
System.out.println("我是持久层的dao....");
}
}

CustomerServiceImpl.java

package com.itheima.demo3;

public class CustomerServiceImpl {
//不准new了,那么必须要提供成员属性让框架帮你封装了


//提供成员属性,提供set方法 (此类需要用到dao,这就是依赖)
private CustomerDaoImpl customerDao;
public void setCustomerDao(CustomerDaoImpl customerDao) {
this.customerDao = customerDao;
}


public void save() {
System.out.println("我是业务层的service.....");

//原始的方式
//new CustomerDaoImpl().save();

//spring的方式
customerDao.save();
}
}

Demo3.java

package com.itheima.demo3;

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

public class Demo3 {
/**
* 原始方法
*/
@Test
public void run1(){
CustomerServiceImpl cs = new CustomerServiceImpl();
cs.save();
}

/**
* spring方式
*/
@Test
public void run2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerServiceImpl cs = (CustomerServiceImpl) ac.getBean("customerService");
cs.save();
}
}

applicationContext.xml2

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


<!-- 真正演示dao的依赖注入 --><!-- set方法的注入方式是标准常用的注入方式 -->
<bean id="customerDaoImpl" class="com.itheima.demo3.CustomerDaoImpl"/>
<bean id="customerService" class="com.itheima.demo3.CustomerServiceImpl">
<property name="customerDao" ref="customerDaoImpl"/>
</bean>
<!-- 上面的解释:首先配置了两个bean,那么框架就会帮你new相应的类对象,然后service类需要用到dao类对象,
则就要将配好的dao类注入到service类里 注意注入方式是引用上面配好的bean
-->

</beans>

依赖注入的方式2:构造方法

注意普通属性值参数用value,引用其他类对象则用ref

Car1.java

package com.itheima.demo4;

public class Car1 {
private String name;
private Double price;

//public Cart() {}//注意没有空参构造方法 配置时必须提供参数
public Car1(String name, Double price) {
super();
this.name = name;
this.price = price;
}

@Override
public String toString() {
return "Car1 [name=" + name + ", price=" + price + "]";
}

}

Person.java

package com.itheima.demo4;

public class Person {
private String name;
private Car1 car1;//没有内存的引用 配置时不能写value 要写ref
public Person(String name, Car1 car1) {
super();
this.name = name;
this.car1 = car1;
}
@Override
public String toString() {
return "Person [name=" + name + ", car1=" + car1 + "]";
}

}

Demo4.java

package com.itheima.demo4;

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

public class Demo4 {
/**
* 通过构造方法实现注入 用得不多 标准用法是set方法
*/
@Test
public void run1(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Car1 car1=(Car1) ac.getBean("car1");
System.out.println(car1);
}
//补上 引用的配置
@Test
public void run2(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person) ac.getBean("person");
System.out.println(person);
}


/**
* 重复写一个set方法注入方式 以强调这是标准的注入方式
*/
@Test
public void run3(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Car2 car2= (Car2) ac.getBean("car2");
System.out.println(car2);
}

}

applicationContext.xml3

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 演示构造方法的注入方式 (不常用)-->
<bean id="car1" class="com.itheima.demo4.Car1">
<!-- 配置构造方法的参数 那么框架创建此类时就拿着参数调用对应的构造方法 -->
<!-- <constructor-arg name="name" value="奇瑞QQ"/>
<constructor-arg name="price" value="35000"/> -->

<!-- 另一种配置参数的方式 index="0"表示第一个构造参数 ="1"表示第二个构造参数-->
<constructor-arg index="0" value="奇瑞QQ2"/>
<constructor-arg index="1" value="50000"/>
</bean>

<bean id="person" class="com.itheima.demo4.Person">
<constructor-arg name="name" value="天河"/>
<constructor-arg name="car1" ref="car1"/><!-- 注意引用类实例 不能写value-->
</bean>

<!-- 重复演示set方法注入方式 -->
<bean id="car2" class="com.itheima.demo4.Car2">
<property name="name" value="飞车"/>
<property name="price" value="99999"/>
</bean>

</beans>

 

依赖注入方式3:p名称空间的注入(纯了解即可)

Car2.java

package com.itheima.demo4;

public class Car2 {
private String name;
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
//方法多了没事 少了不行

@Override
public String toString() {
return "Car2 [name=" + name + ", price=" + price + "]";
}

}

applicationContext.xml4

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

<!-- 采用p名称空间的方式注入(了解) 引入名称空间时为了避免同名属性冲突,去了别名p 下面也要加上别名p:-->
<!-- <bean id="car2" class="com.itheima.demo4.Car2" p:name="保时捷" p:price="1000"/> --><!-- 一行即可 也很简单 -->

<!-- SpEL表达式方式注入 -->
<bean id="car2" class="com.itheima.demo4.Car2">
<property name="name" value="#{'飞车2'}"/>
<property name="price" value="#{99999}"/>
</bean>
<!-- 这种方式的好处没演示出来 可以直接像el表达式一样用'.'调用属性 -->

</beans>

Demo4.java1

@Test
public void run3(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Car2 car2= (Car2) ac.getBean("car2");
System.out.println(car2);
}

 

数组,集合(List,Set,Map),Properties等的注入

自己写的不多,但是三大框架整合时需要用到这些配置

User.java

package com.itheima.demo4;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class User {
private String[] arrs;
public void setArrs(String[] arrs) {
this.arrs = arrs;
}

private List<String> list;
public void setList(List<String> list) {
this.list = list;
}

private Map<String, String> map;
public void setMap(Map<String, String> map) {
this.map = map;
}

private Properties pro;//java.utils包下的 属性文件类
public void setPro(Properties pro) {
this.pro = pro;
}

@Override
public String toString() {
return "User [arrs=" + Arrays.toString(arrs) + ", list=" + list
+ ", map=" + map + ", pro=" + pro + "]";
}
}

applicationContext.xml5

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

<!-- 注入集合 -->
<bean id="user" class="com.itheima.demo4.User">
<property name="arrs">
<!-- 数组或者集合的值需要在list子标签里配置了 -->
<list>
<value>天河</value>
<value>紫英</value>
<value>梦璃</value>
<value>菱纱</value>
</list>
</property>

<property name="list">
<list>
<value>玄霄</value>
<value>天青</value>
<value>夙玉</value>
<!-- <ref="person"/> 同样list中放的是对象的时候value换成ref-->
</list>
</property>

<!-- set集合配置是 <list>改成<set> -->
<property name="map">
<map>
<!-- key是对象时用key-ref value是对象时用value-ref -->
<entry key="a" value="苹果"/>
<entry key="b" value="香蕉"/>
</map>
</property>

<!-- 属性文件类也只是一个普通的属性 配置没太多特殊 直接在这里写属性文件的内容罢了 -->
<property name="pro">
<props>
<prop key="username">root</prop>
<prop key="password">1234</prop>
</props>
</property>

</bean>

</beans>

Demo4.java3

package com.itheima.demo4;

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

public class Demo4 {

/**
* 测试注入集合
*/
@Test
public void run4(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User user= (User) ac.getBean("user");
System.out.println(user);
}

}

 

Spring框架的配置文件分开管理(了解)

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


<!-- 注入集合 -->
<!-- <bean id="user" class="com.itheima.demo4.User">
<property name="arrs">
数组或者集合的值需要在list子标签里配置了
<list>
<value>天河</value>
<value>紫英</value>
<value>梦璃</value>
<value>菱纱</value>
</list>
</property>

<property name="list">
<list>
<value>玄霄</value>
<value>天青</value>
<value>夙玉</value>
<ref="person"/> 同样list中放的是对象的时候value换成ref
</list>
</property>

set集合配置是 <list>改成<set>
<property name="map">
<map>
key是对象时用key-ref value是对象时用value-ref
<entry key="a" value="苹果"/>
<entry key="b" value="香蕉"/>
</map>
</property>

属性文件类也只是一个普通的属性 配置没太多特殊 直接在这里写属性文件的内容罢了
<property name="pro">
<props>
<prop key="username">root</prop>
<prop key="password">1234</prop>
</props>
</property>

</bean> -->


<!-- 配置文件写多个(多写集合从配置文件) 分模块写 方便管理 但是要在主配置文件中引入从配置文件 -->
<import resource="applicationContext2.xml"/>
<!-- 同一个目录下直接写相对路径 在某个包写要加完整包名 com/itheima/xxx/applicationContext2.xml 和struts一样 -->



</beans>

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

<!-- 注入集合 -->
<bean id="user" class="com.itheima.demo4.User">
<property name="arrs">
<!-- 数组或者集合的值需要在list子标签里配置了 -->
<list>
<value>天河</value>
<value>紫英</value>
<value>梦璃</value>
<value>菱纱</value>
</list>
</property>

<property name="list">
<list>
<value>玄霄</value>
<value>天青</value>
<value>夙玉</value>
<!-- <ref="person"/> 同样list中放的是对象的时候value换成ref-->
</list>
</property>

<!-- set集合配置是 <list>改成<set> -->
<property name="map">
<map>
<!-- key是对象时用key-ref value是对象时用value-ref -->
<entry key="a" value="苹果"/>
<entry key="b" value="香蕉"/>
</map>
</property>

<!-- 属性文件类也只是一个普通的属性 配置没太多特殊 直接在这里写属性文件的内容罢了 -->
<property name="pro">
<props>
<prop key="username">root</prop>
<prop key="password">1234</prop>
</props>
</property>

</bean>

</beans>

Demo4.java4

package com.itheima.demo4;

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

public class Demo4 {

/**
* 测试配置文件分模块管理
*/
@Test
public void run4(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User user= (User) ac.getBean("user");
System.out.println(user);
}

/**
* 可以直接加载多个多个配置文件 此时就不必在主配置文件中引入其他配置文件了
* 但是要是真的有多个配置文件 还是建议引入而不写这种代码 (倾向于在配置文件中写)
*/
@Test
public void run5(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
User user= (User) ac.getBean("user");
System.out.println(user);
}

}

 

Spring框架整合WEB(简单的Struts整合)

主要解决apllication.xml配置文件如何只加载一次的问题

先搭建struts2环境(核心过滤器别忘了),再搭建spring最简单的环境(6个包)

CustomerDao.java

package com.itheima.dao;

public interface CustomerDao {
public void save();
}

CustomerDaoImpl.java

package com.itheima.dao;

import com.itheima.service.CustomerService;

public class CustomerDaoImpl implements CustomerService{

@Override
public void save() {
System.out.println("持久层:保存客户...");
}

}

CustomerService.java

package com.itheima.service;

public interface CustomerService {
public void save();
}

CustomerServiceImpl.java

package com.itheima.service;

import com.itheima.dao.CustomerDaoImpl;

public class CustomerServiceImpl implements CustomerService {

private CustomerDaoImpl customerDao;
public void setCustomerDao(CustomerDaoImpl customerDao) {
this.customerDao = customerDao;
}

@Override
public void save() {
System.out.println("业务层:保存客户...");
customerDao.save();
}

}

CustomerAction.java

package com.itheima.web.action;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.itheima.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;

/**
* 客户的Action
* @author Administrator
*
*/
public class CustomerAction extends ActionSupport{//spring学完后 实战之前 将所有博客和代码读一遍 复习一下下 最好有个小总结 简单复习而已 不要苛求什么

/**
* 保存客户
* @return
*/
public String save(){
System.out.println("WEB层保存客户...");
/*//使用工厂 这样写每次请求action都会加载一次配置文件,也即都会创建一个工厂,很明显这不合适 这个时候就需要整合操作了
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService cs= (CustomerService) ac.getBean("customerService");
cs.save();
return NONE;*/


/**
* 那么需要导包 spring-web... 然后监听ServletContext类(最大的全局共享域)
* 这个类是在服务器开启时创建,关闭时销毁
* 有个专门的监听器是监听这个类的创建和销毁的
*
* 过程如下:启动服务器->ServletContext对象被创建->ServletContext监听器对象方法执行->在监听器里执行加载配置文件的代码
* ->那么配置文件只会加载一次,里面所有的类对象也都创建好了
*
* spring-web-4.2.4.RELEASE.jar包里就有监听器 配置一下即可
*/


//解决方案:需要使用web的工厂方式
//action里获得servletContext有个好值栈啊
ServletContext servletContext = ServletActionContext.getServletContext();
//获得web工厂
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//利用web工厂获得对象
CustomerService cs= (CustomerService)context.getBean("customerService");
cs.save();
return NONE;

}
}

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="crm" namespace="/" extends="struts-default">
<action name="customer_*" class="com.itheima.web.action.CustomerAction" method="{1}"/><!--{1}取得是前面name属性中第1个* -->
</package>

</struts>

applicationContext.xml(ZH)

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

<!-- 配置客户的业务层 -->
<bean id="customerService" class="com.itheima.service.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>

<!-- 配置客户的dao -->
<bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl"/>

</beans>

导包:spring-web-4.2.4.RELEASE.jar(spring依赖包)

web.xml

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

<!-- 配置web整合的监听器(最原始的Listener) -->
<listener>
<!-- 此监听器默认情况下只能加载WEB-INF目录下的配置文件 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 提供配置方式,让他加载src目录下的配置文件(配置一个指定的全局参数) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<!-- 核心过滤器千万别忘记了 -->
<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>

<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>
</web-app>

固定配置:

<!-- 配置web整合的监听器(最原始的Listener) -->
<listener>
<!-- 此监听器默认情况下只能加载WEB-INF目录下的配置文件 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 提供配置方式,让他加载src目录下的配置文件(配置一个指定的全局参数) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<!-- struts2核心过滤器千万别忘记了 -->
<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>