java_spring_Web项目中使用spring

本文详细介绍了一个基于Spring框架的Web项目的搭建过程,包括项目创建、依赖引入、配置文件编写、类扫描配置、web.xml注册等步骤,并通过具体示例验证了配置的有效性。

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

创建一个web项目,名称取为:springWebProject

1. 引入包

引入spring的包,不了解的话可以全引入。

还需要引入写日志的包:commons-logging.jar

 

2. 创建配置文件:

src目录下新建配置文件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:tx="http://www.springframework.org/schema/tx" 

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context" 

xmlns:jee="http://www.springframework.org/schema/jee"

xsi:schemaLocation="

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd" 

>

</beans>

 

3. 测试

做一步测一步,可以更早发现问题所在。

 

1.添加一个测试service类:

public class TestService {

private String name;

public void cout(){

System.out.println("test:"+name);

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

2.添加一个main测试类:

public class Test {

public static void main(String[] args) {

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

TestService testService = (TestService) ac.getBean("testService");

testService.cout();

}

}

3.在配置文件中配置bean:

<bean id="testService" class="com.glodon.test.TestService">

<property name="name" value="张三"/>

</bean>

 

4.测试运行main

后台输出:test:张三

说明spring配置文件装载成功。

 

4. 创建项目结构

src目录下创建各种包:

  

5. 扫描类文件

使用注解的方式需要对相应的类文件进行扫描,因此需要在applicationContext.xml中配置:

<context:component-scan base-package="com.glodon.dao"></context:component-scan>

<context:component-scan base-package="com.glodon.model"></context:component-scan>

<context:component-scan base-package="com.glodon.service"></context:component-scan>

只有这三个包下的文件才需要扫描,其他的包下不需要。

 

6. web.xml中注册applicationContext.xml

<!-- 加载spring的配置文件 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<!-- 监听application的上下文,放入application对象中 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

7. 测试

service包中添加一个service类:

@Service

public class UserService {

public void print(){

System.out.println("userservice");

}

}

因为已扫描过,所以可以直接使用注解@Srvice

 

写一个测试类:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"classpath:applicationContext.xml"})

public class TestUserService {

@Autowired

private UserService userService;

@Test

public void hander(){

System.out.println("userAPI");

userService.print();

}

}

使用@Autowired表示实例化对象,后台输出:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值