创建一个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表示实例化对象,后台输出: