1. 创建Maven工程,导入pom文件中Spring-Context依赖。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aura.spring</groupId>
<artifactId>Spring-xml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
</dependencies>
</project>
2. 在src/main/java中创建package。
dao:数据持久层,用于数据库的CURD操作。
service:业务层,用于实现各个模块间具体的业务逻辑。
handler:控制层,用于处理、调用业务层的具体业务。
3. 创建持久层类、业务层类、控制层类。
以注解的方式配置spring不需要在类中声明set、get方法。
持久层类和接口:
package com.aura.spring.dao;
public interface EmployeeDAO {
public void insertEmp();
}
package com.aura.spring.dao;
import org.springframework.stereotype.Repository;
/**
* 持久层:数据库的CURD操作
* @author panghu
*
*/
/*
* @Repository是代表持久层的注解,相当于xml配置方式中的
* <bean id="employeeDAOImpl" class="com.aura.spring.dao.EmployeeDAOImpl"></bean>
*
* 默认情况下,注解的id是类名首字母小写。即id=employeeDAOImpl对应EmployeeDAOImpl类
* 注解的id可以通过@注解名(value="xxx")来手动指定,其中value也可省略
* 如:@Repository(value="emp")或者@Repository("emp")
*
*/
@Repository
public class EmployeeDAOImpl implements EmployeeDAO{
@Override
public void insertEmp() {
System.out.println("EmployeeDAOImpl insert success!");
}
}
业务层:
package com.aura.spring.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aura.spring.dao.EmployeeDAO;
/**
* 业务层:处理业务,实现各个模块的业务逻辑
* @author panghu
*
*/
@Service
public class EmployeeService {
@Autowired
private EmployeeDAO empDAO;
public void doRegist() {
empDAO.insertEmp();
}
}
控制层:
package com.aura.spring.handler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.aura.spring.service.EmployeeService;
/**
* 控制层:请求处理器,用于调用业务层的具体业务
* @author panghu
*
*/
@Controller("handler")
public class EmployeeHandler {
@Autowired
private EmployeeService empService;
public void regist() {
empService.doRegist();
}
}
4. 在src/main/resource新建spring bean configuration file。
5. 配置spring配置文件。
首先需要点击配置文件下方的Namespaces,然后勾选context,否则context:component-scan无法使用!
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--
组件扫描
base-package:将指定包及其子包下有注解的类添加到spring容器中进行管理
-->
<context:component-scan base-package="com.aura.spring"></context:component-scan>
</beans>
6. 创建test类测试。
package com.aura.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aura.spring.dao.EmployeeDAO;
import com.aura.spring.dao.EmployeeDAOImpl;
import com.aura.spring.handler.EmployeeHandler;
import com.aura.spring.service.EmployeeService;
public class TestSpring {
@Test
public void test() {
// 创建spring容器对象
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取spring容器内的组件对象
EmployeeDAO dao = ctx.getBean("employeeDAOImpl",EmployeeDAOImpl.class);
System.out.println(dao);
EmployeeService service = ctx.getBean("employeeService", EmployeeService.class);
System.out.println(service);
EmployeeHandler handler = ctx.getBean("handler", EmployeeHandler.class);
System.out.println(handler);
// 调用方法
handler.regist();
}
}