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.itcast.c_property.User" >
         <property name="name" value="xxx"></property>
     </bean>
    -->
    <!-- p名称空间优化后 -->
    <bean id="user" class="loaderman.c_property.User" p:name="Jack0001"></bean>
     
</beans>      

 

转载于:https://www.cnblogs.com/loaderman/p/10039132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值