初识Spring(四)

spring注入和MVC

action->service->dao

以前我们需要在action中new service的对象才能调用service中的方法,对应service调用dao层中的方法也是如此,现在spring要把这个解放出来。

step1 新建entity对象

User.java

package cn.itcast.c_property;

public class User {

    private int id;
    private String name;

    //////////////////  --> 通过容器注入属性值
    public void setId(int id) {
        this.id = id;
    }
    // //--> 通过容器注入属性值
    public void setName(String name) {
        this.name = name;
    }

    ////////////////
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }



    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }




    public User() {
        super();
        System.out.println("------User对象创建【无参数构造器】------");
    }


    public User(int id, String name) {
        System.out.println("-----User对象创建【带参数构造器】--------");
        this.id = id;
        this.name = name;
    }


    public void init_user() {
        System.out.println("创建对象之后,初始化");
    }
    public void destroy_user() {
        System.out.println("IOC容器销毁,user对象回收!");
    }

}

UserDao.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;
    }
}

step2 配置bean.xml


<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">

    
    
    <bean id="user1" class="cn.itcast.c_property.User" scope="prototype">
        <constructor-arg value="100">constructor-arg>
        <constructor-arg value="Tom">constructor-arg>
    bean>

    
    <bean id="user" class="cn.itcast.c_property.User" scope="prototype">
        <property name="id" value="101">property>
        <property name="name" value="Jack">property>
    bean>

    
    
    <bean id="userDao" class="cn.itcast.c_property.UserDao">bean>

    
    <bean id="userService" class="cn.itcast.c_property.UserService">
        <property name="userDao" ref="userDao">property>
    bean>

    
    <bean id="userAction1" class="cn.itcast.c_property.UserAction">
        <property name="userService" ref="userService">property>
    bean>
beans>      

Test.java
首先我们测试一下使用set方法给对象赋值

    @Test
    public void testSet() {
        // 从容器中获取
        User user = (User) ac.getBean("user");

        System.out.println(user);
    }

运行结果:

——User对象创建【无参数构造器】——
User [id=101, name=Jack]

第二:

package cn.itcast.c_property;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    // 创建容器对象
    private ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/c_property/bean.xml");

    @Test
    public void testExecuteAction() {
        // 从容器中获取Action
        UserAction userAction = (UserAction) ac.getBean("userAction1");
        userAction.execute();

    }
}

运行结果:

DB:保存用户

还有一个p空间需要spring 3.x以上才支持

你可能感兴趣的:(spring)