myspringdemo 配置(我用的是4.2的)

本文介绍了一个使用Spring框架的基本示例项目,包括搭建环境、定义Person接口及其实现类、配置DispatcherServlet、创建数据源Bean并进行单元测试等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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();

 }

}

运行图如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值