Spring对象依赖关系

Spring中,如何给对象的属性赋值?  【DI, 依赖注入】

         1) 通过构造函数

         2) 通过set方法给属性注入值

         3) p名称空间

    4)自动装配(了解)

         5) 注解


 

 

package loaderman.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("loaderman/c_property/bean.xml");

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

        System.out.println(user);
    }

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

    }
}
package loaderman.c_property;


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


public class App_p {

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


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

        System.out.println(ac.getBean("user"));
    }
}
package loaderman.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对象回收!");
    }

}
package loaderman.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;
    }
}
package loaderman.c_property;

public class UserDao {

    public void save() {
        System.out.println("DB:保存用户");
    }
}
package loaderman.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();
    }
}
<?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">
    
    <!-- ###############对象属性赋值############### -->
    <!--  1) 通过构造函数 -->
    <bean id="user1" class="loaderman.c_property.User" scope="prototype">
        <constructor-arg value="100"></constructor-arg>
        <constructor-arg value="Tom"></constructor-arg>
    </bean>
    
    <!-- 2) 通过set方法给属性注入值 -->
    <bean id="user" class="loaderman.c_property.User" scope="prototype">
        <property name="id" value="101"></property>
        <property name="name" value="Jack"></property>
    </bean>
    
    <!-- 
        案例:
            action/service/dao
     -->
    <!-- dao instance -->
    <bean id="userDao" class="loaderman.c_property.UserDao"></bean>

    <!-- service instance -->
    <bean id="userService" class="loaderman.c_property.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    
    <!-- action instance -->
    <bean id="userAction1" class="loaderman.c_property.UserAction">
        <property name="userService" ref="userService"></property>
    </bean>
    
    
    <!-- ##############内部bean############## -->
    <bean id="userAction2" class="loaderman.c_property.UserAction">
        <property name="userService">
            <bean class="loaderman.c_property.UserService">
                <property name="userDao">
                    <bean class="loaderman.c_property.UserDao"></bean>
                </property>
            </bean>
        </property>
    </bean>
    <!-- 
        给对象属性注入值:
            # p 名称空间给对象的属性注入值
             (spring3.0以上版本才支持)
     -->
</beans>      

<?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">
    
    <!-- ###############对象属性赋值############### -->
    
    <!-- 
        给对象属性注入值:
            # p 名称空间给对象的属性注入值
             (spring3.0以上版本才支持)
     -->
     <bean id="userDao" class="loaderman.c_property.UserDao"></bean>
     
     <bean id="userService" class="loaderman.c_property.UserService" p:userDao-ref="userDao"></bean>
     
     <bean id="userAction" class="loaderman.c_property.UserAction" p:userService-ref="userService"></bean>
    
    <!-- 传统的注入: 
     <bean id="user" class="cn.loaderman.c_property.User" >
         <property name="name" value="xxx"></property>
     </bean>
    -->
    <!-- p名称空间优化后 -->
    <bean id="user" class="loaderman.c_property.User" p:name="Jack0001"></bean>
     
</beans>      

 

posted on 2018-11-29 16:23  LoaderMan  阅读(600)  评论(0编辑  收藏  举报

导航