1.官网上下载spring的jar包。
2、新建一个web项目。把上面的jar包拷贝到lib里面。确保web容器发布可以找到
3、在buildpatch中把这些包引进去。确保编译可以找到
4、定义person接口和两个实现类Northman和southman
5、在web.xml中加入servlect过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>spitter</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spitter</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6、在applicationContext。xml中装配bean
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<!-- 设置连接数据库的驱动、URL、用户名、密码
连接池最大连接数、最小连接数、初始连接数等参数 -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost:3306/hrsystem"
p:user="root"
p:password="root"
p:maxPoolSize="40"
p:minPoolSize="1"
p:initialPoolSize="1"
p:maxIdleTime="20"/>-->
<!--装配bean -->
<bean id="north" class="com.test.bean.NorthMan"></bean>
<bean id="south" class="com.test.bean.SouthMan"></bean>
</beans>
7、新建test类
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.test.bean.NorthMan;
import com.test.bean.SouthMan;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new FileSystemXmlApplicationContext("src/applicationContext.xml");
NorthMan n =(NorthMan) ac.getBean("north");
n.eat();
n.drink();
SouthMan s = (SouthMan) ac.getBean("south");
s.eat();
s.drink();
}
}
运行图如下