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. 创建持久层类、业务层类、控制层类。
以xml文件配置spring容器,需要在类的内部提供set/get方法。
持久层类和接口:
package com.aura.spring.dao;
public interface EmployeeDAO {
public void insertEmp();
}
package com.aura.spring.dao;
/**
* 持久层,用于数据库的CURD操作
* @author panghu
*
*/
public class EmployeeDAOImpl implements EmployeeDAO {
@Override
public void insertEmp() {
System.out.println("EmployeeDAOImpl insert sucess!");
}
}
业务层类:
package com.aura.spring.service;
/**
* 业务层:用于处理业务,实现各个模块间的业务逻辑。
* @author panghu
*
*/
import com.aura.spring.dao.EmployeeDAO;
public class EmployeeService {
private EmployeeDAO employeeDao;
public EmployeeDAO getEmployeeDao() {
return employeeDao;
}
public void setEmployeeDao(EmployeeDAO employeeDao) {
this.employeeDao = employeeDao;
}
public void doRegist() {
employeeDao.insertEmp();
}
}
控制层类:
package com.aura.spring.handler;
/**
* 控制层:请求处理器,调用业务层的具体业务
* @author panghu
*
*/
import com.aura.spring.service.EmployeeService;
public class EmployeeHandler {
private EmployeeService employeeService;
public EmployeeService getEmployeeService() {
return employeeService;
}
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public void regist() {
employeeService.doRegist();
}
}
4. 在src/main/resource新建spring bean configuration file。
5. 配置spring配置文件。
bean:管理每个对象组件。
id:唯一标识。
class:全类名。
property:为类中的属性装配值。
name:类中的属性名,准确的来说是set后面的属性名。
ref:当前容器中管理的某个对象的id。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 通过xml的方式管理组件 -->
<!--
bean:管理每个对象组件
id:唯一标识
class:全类名,根据反射创建类的对象
property:为类中的属性装配值
name:类中的属性名,准确的来说是set后面的属性名
ref:当前容器中某个对象的id值
-->
<!-- <bean id="employeeDao" class="com.aura.spring.dao.EmployeeDAOImpl"></bean> -->
<!-- 如果需要更换dao实现类,直接修改实现类全类名即可 -->
<bean id="employeeDao" class="com.aura.spring.dao.EmployeeDAOMybatisImpl"></bean>
<bean id="employeeService" class="com.aura.spring.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
<bean id="employeeHandler" class="com.aura.spring.handler.EmployeeHandler">
<property name="employeeService" ref="employeeService"></property>
</bean>
</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.handler.EmployeeHandler;
import com.aura.spring.service.EmployeeService;
public class TestSpring {
@Test
public void test() {
// 创建Spring容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取具体的组件对象
/* 参数1:spring容器中对象组件的id
* 参数2:对应id类.class,可以是接口
*/
EmployeeDAO employeeDAO = context.getBean("employeeDao", EmployeeDAO.class);
System.out.println(employeeDAO);
EmployeeService employeeService = context.getBean("employeeService",EmployeeService.class);
System.out.println(employeeService);
EmployeeHandler employeeHandler = context.getBean("employeeHandler",EmployeeHandler.class);
System.out.println(employeeHandler);
employeeHandler.regist();
}
}