springIoc案例之actionservicedao

    xiaoxiao2021-03-26  3

    UseDao.java

    package cn.itcast.c_property; public class UserDao { public void save() { System.out.println("DB:保存用户"); } } UserService.java

    package cn.itcast.c_property; public class UserService { private UserDao userDao; // = new UserDao(); // IOC:对象的创建交给spring的外部容器完成 public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save() { userDao.save(); } } UserAction.java

    package cn.itcast.c_property; public class UserAction { // Service: springIOC容器注入 private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String execute() { userService.save(); return null; } }

    bean.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:p="http://www.springframework.org/schema/p" 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.xsd"> <!-- 案例: action/service/dao --> <!-- dao instance --> <bean id="userDao" class="cn.itcast.c_property.UserDao"></bean> <!-- service instance --> <bean id="userService" class="cn.itcast.c_property.UserService"> <property name="userDao" ref="userDao"></property> </bean> <!-- action instance --> <bean id="userAction1" class="cn.itcast.c_property.UserAction"> <property name="userService" ref="userService"></property> </bean> </beans> 输出:

    DB:保存用户

    转载请注明原文地址: https://ju.6miu.com/read-650243.html

    最新回复(0)