`
Alvin871111
  • 浏览: 17886 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

spring学习笔记(8)

阅读更多
属性注入方式

1。settter注入
1)简单属性:
beans.xml配置如下:
<bean id="userServiceBean" class="com.neusoft.main.object.User">
<property name="id" value="100"/>
<property name="name" value="test"/>
</bean>

新增User.java:

package com.neusoft.main.object;

/**
* @author <a href="mailto:">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午11:07:01
*/
public class User {
   
    private String id;
   
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
   

}
单元测试类中添加如下方法:
@Test
    public void baseObject() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) ctx.getBean("userServiceBean");
        System.out.println("id: "+user.getId());
        System.out.println("name:" + user.getName());
    }

执行结果如下:
2010-6-1 13:23:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 13:23:52 CST 2010]; root of context hierarchy
2010-6-1 13:23:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 13:23:52 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@fec107
2010-6-1 13:23:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@fec107: defining beans [userDaoImpl,factory,userDaoImpl3,userServiceBean]; root of factory hierarchy
factory2 born bean.
id: 100
name:test

2)对象属性-外部bean注入:在上面的bean中修改如下配置:
<bean id="userServiceBean" class="com.neusoft.main.object.User">
<property name="id" value="100"/>
<property name="name" value="test"/>
<property name="userDao" ref="userDaoImpl"/>
</bean>

修改User.java,增加UserDao属性:

package com.neusoft.main.object;

import com.neusoft.main.dao.UserDao;

/**
* @author <a href="mailto">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午11:07:01
*/
public class User {
   
    private String id;
   
    private String name;
   
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
   

}

修改单元测试方法如下:

@Test
    public void baseObject() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) ctx.getBean("userServiceBean");
        System.out.println("id: "+user.getId());
        System.out.println("name:" + user.getName());
        if(null != user.getUserDao() ) {
            System.out.println("userdao is not null.");
        }
    }
执行结果如下:
2010-6-1 13:33:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 13:33:52 CST 2010]; root of context hierarchy
2010-6-1 13:33:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 13:33:52 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@132e13d
2010-6-1 13:33:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@132e13d: defining beans [userDaoImpl,factory,userDaoImpl3,userServiceBean]; root of factory hierarchy
factory2 born bean.
id: 100
name:test
userdao is not null.

3)对象属性-内部bean注入:
修改beans.xml
<property name="userDao1">
<bean id="temp" class="com.neusoft.main.dao.impl.UserDaoImpl" />
</property>
修改User.java:加入如下片段:
private UserDao userDao1;

    public UserDao getUserDao1() {
        return userDao1;
    }

    public void setUserDao1(UserDao userDao1) {
        this.userDao1 = userDao1;
    }

修改单元测试方法:

@Test
    public void baseObject() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) ctx.getBean("userServiceBean");
        System.out.println("id: "+user.getId());
        System.out.println("name:" + user.getName());
        if(null != user.getUserDao() ) {
            System.out.println("userdao is not null.");
        }
        if(null != user.getUserDao1()) {
            System.out.println("userdao1 is not null.");
        }
    }
执行单元测试方法结果如下:
2010-6-1 13:48:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 13:48:49 CST 2010]; root of context hierarchy
2010-6-1 13:48:50 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 13:48:50 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@132e13d
2010-6-1 13:48:50 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@132e13d: defining beans [userDaoImpl,factory,userDaoImpl3,userServiceBean]; root of factory hierarchy
factory2 born bean.
id: 100
name:test
userdao is not null.
userdao1 is not null.

集合属性注入:
修改User.java如下:
package com.neusoft.main.object;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.neusoft.main.dao.UserDao;

/**
* @author <a href="mailto:">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午11:07:01
*/
public class User {

    private String id;

    private String name;

    private UserDao userDao;

    private UserDao userDao1;

    private List<String> telephone;

    private Set<String> set;

    private Map<String, String> map;

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public List<String> getTelephone() {
        return telephone;
    }

    public void setTelephone(List<String> telephone) {
        this.telephone = telephone;
    }

    public UserDao getUserDao1() {
        return userDao1;
    }

    public void setUserDao1(UserDao userDao1) {
        this.userDao1 = userDao1;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

修改beans.xml如下:
<property name="telephone">
<list>
<value>02488796789</value>
<value>18678905656</value>
<value>04344456651</value>
</list>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="map">
<map>
<entry key="one" value="1"/>
<entry key="two" value="3"/>
<entry key="three" value="3"/>
</map>
</property>

修改单元测试方法:
@Test
    public void baseObject() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) ctx.getBean("userServiceBean");
        System.out.println("id: "+user.getId());
        System.out.println("name:" + user.getName());
        if(null != user.getUserDao() ) {
            System.out.println("userdao is not null.");
        }
        if(null != user.getUserDao1()) {
            System.out.println("userdao1 is not null.");
        }
        for(int i = 0;i < user.getTelephone().size();i++) {
            System.out.println("List " +i + ":" + user.getTelephone().get(i));
        }
        for(Iterator<String> iter = user.getSet().iterator();iter.hasNext();) {
            System.out.println("set: " + iter.next());
        }
        for(Iterator<String> iter = user.getMap().keySet().iterator();iter.hasNext();) {
            String key = iter.next();
            String value = user.getMap().get(key);
            System.out.println("map key" + key + " value " + value);
        }
    }

执行结果如下
factory2 born bean.
id: 100
name:test
userdao is not null.
userdao1 is not null.
List 0:02488796789
List 1:18678905656
List 2:04344456651
set: set1
set: set2
set: set3
map keyone value 1
map keytwo value 3
map keythree value 3
分享到:
评论

相关推荐

    Springcloud学习笔记.md

    Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Spring...

    Spring学习笔记( spring视频笔记)

    Spring学习笔记( spring视频笔记)

    Spring学习笔记 自我总结

    spring学习笔记

    Spring学习笔记(精华全记录)

    ### Spring学习笔记(精华全记录) #### Spring框架概述 Spring框架源自Rod Johnson的个人项目,最初于2002年末发布。Spring并非一开始就作为一个完整的框架出现,而是从一个项目逐步发展而来。随着项目的成熟,...

    Spring学习笔记+学习源码.zip

    这份"Spring学习笔记+学习源码.zip"资源包含了深入学习Spring及其相关技术的知识点,以及实践代码,对提升Spring技能将大有裨益。 首先,我们来详细讨论Spring框架的主要组件和功能: 1. **依赖注入(Dependency ...

    Spring学习笔记&源码

    本资料“Spring学习笔记&源码”是基于网易云课堂黑马程序员的Spring四天精通课程,旨在帮助学习者深入理解和实践Spring框架。 笔记部分可能会涵盖以下内容: 1. **Spring概述**:介绍Spring框架的历史、特点和主要...

    Spring学习笔记(马士兵spring视频笔记).doc

    Spring学习笔记(马士兵spring视频笔记).docSpring学习笔记(马士兵spring视频笔记).docSpring学习笔记(马士兵spring视频笔记).docSpring学习笔记(马士兵spring视频笔记).doc

    Spring学习笔记.zip

    根据提供的压缩包文件名,我们可以推测这是一个逐步学习Spring的系列笔记。从"Spring_day1"开始,可能涵盖了Spring的基础概念、环境搭建和基本配置。"Spring_day2"可能涉及了依赖注入和AOP的深入讲解。"Spring_day3...

    Spring框架学习笔记

    这份"Spring框架学习笔记"涵盖了Spring框架的基础知识、核心组件以及高级特性,对于初学者来说是一份宝贵的资料。 一、Spring框架概述 Spring框架是为了解决企业应用开发的复杂性而设计的,它提供了一个全面的基础...

    javaSpring学习笔记

    “Java Spring学习笔记”是一份宝贵的资源,专门为想要学习和掌握Java Spring框架的开发者而设计。这份学习笔记提供了详细而系统的教程和实践指南,帮助初学者快速入门,并带领已经有一定经验的开发者深入理解和应用...

    Spring6学习笔记

    Spring6学习笔记,师承老杜

    springsecurity学习笔记

    在"springsecurity学习笔记"中,你可能会涉及以下主题: - Spring Security的基本配置,包括web安全配置和全局安全配置。 - 如何自定义认证和授权流程,比如实现自定义的AuthenticationProvider和...

    Spring Cloud 学习笔记.pdf

    Spring Cloud是一套微服务架构下的分布式系统解决方案,提供了在分布式系统环境下快速构建一些常见模式的工具,如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式...

    尚学堂Spring学习笔记

    "尚学堂Spring学习笔记" 本文档记录了尚学堂Spring学习笔记的重要知识点,涵盖了Spring配置文件的设置、普通属性的注入、自定义属性编辑器、公共属性的注入、Spring注解等内容。 一、Spring配置文件的设置 在...

    spring学习笔记

    ### Spring学习笔记知识点详解 #### 一、Spring框架概述 **Spring** 是一个开源的、分层的企业级应用开发框架,旨在简化Java EE应用程序的开发。它的主要目标是提高开发效率,减少耦合度,并提供一种更为简洁的...

    Spring学习笔记.doc

    ### Spring学习笔记知识点详解 #### 一、Spring框架概述 **1.1 什么是Spring** Spring框架是一个开源的轻量级应用框架,主要用于简化企业级应用程序的开发过程。它的核心特性在于提供了一种灵活的方式来组织和...

    Spring学习笔记.xmind

    Spring学习笔记.xmind

    Spring学习笔记

    Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记Spring学习笔记

    spring实用学习笔记(能够跟着实操的)

    spring实用学习笔记(能够跟着实操的)spring实用学习笔记(能够跟着实操的)spring实用学习笔记(能够跟着实操的)spring实用学习笔记(能够跟着实操的)spring实用学习笔记(能够跟着实操的)spring实用学习笔记...

    狂神springcloud学习笔记加代码

    狂神springcloud学习笔记加代码 笔记可以看:https://www.kuangstudy.com/bbs/1374942542566551554#header4 视频链接:https://www.bilibili.com/video/BV1jJ411S7xr

Global site tag (gtag.js) - Google Analytics