实例
1、创建项目
2、创建dao层
public interface IUserDAO {
public String say(String msg);
}
@Override
public String say(String msg) {
System.out.println("--->"+msg);
return "hello";
}
3、创建service层
public interface IUserService {
public String say(String msg);
}
private IUserDAO dao;
@Override
public String say(String msg) {
return dao.say(msg);
}
public void setDao(IUserDAO dao) {
this.dao = dao;
}
4、创建control层
public class UserController {
private IUserService service;
public void setService(IUserService service) {
this.service = service;
}
public void say(String msg){
System.out.println("controller:"+service.say(msg));
}
}
5、配置文件
<?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">
<!-- 注册dao层实例 -->
<bean class="com.sxt.dao.impl.UserDAOImpl" id="userDAOImpl"></bean>
<!-- 注册service实例 -->
<bean class="com.sxt.service.UserServiceImpl" id="userServiceImpl">
<property name="dao" ref="userDAOImpl"></property>
</bean>
<!-- 注册controller实例 -->
<bean class="com.sxt.controller.UserController">
<property name="service" ref="userServiceImpl"></property>
</bean>
</beans>
6、测试
public class test {
public static void main(String[] args) {
ApplicationContext ac=
new ClassPathXmlApplicationContext("applicationContext.xml");
UserController controller = ac.getBean(UserController.class);
controller.say("aaa");
}
}