目录结构:

Person.java

 package com.wss.entity;

 import com.wss.service.doHomeWork;

 public class Person {

     private String name;
private int age;
private doHomeWork dohomework; public void setDoHomeWork(doHomeWork dohomework) {
this.dohomework = dohomework;
}
public doHomeWork getDoHomeWork() {
return this.dohomework;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", dohomework=" + dohomework + "]";
}
}

别说话,点我

doHomeWork.java

 package com.wss.service;

 public class doHomeWork {

     public String write() {
return new String("write");
}
}

别说话,点我

PersonTest.java

 package com.wss.test;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wss.entity.Person; public class PersonTest { @Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Person person = context.getBean("person", Person.class);
System.out.println(person.toString());
System.out.println(person.getDoHomeWork().write());
}
}

别说话,点我

spring-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置person实例,其实现类是Person -->
<bean id="person" class="com.wss.entity.Person">
<property name="name" value="wss"></property>
<property name="age" value="24"></property>
<!-- 驱动调用person的setDoHomeWork()方法,将容器中的stoneAxe作为参数传入 -->
<property name="doHomeWork" ref="dohomework"></property>
</bean>
<!-- 配置dohomework实例,其实现类是doHomeWork -->
<bean id="dohomework" class="com.wss.service.doHomeWork"></bean>
</beans>

别说话,点我

运行结果:

两种注入方式:

spring 管理bean的更多相关文章

  1. (转)编码剖析Spring管理Bean的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52832434 在Spring的第一个案例中,我们已经知道了怎么将bean交给Spring容器进 ...

  2. 采用Spring管理Bean和依赖注入

    1. 实例化spring容器和从容器获取Bean对象 实例化Spring容器常用的两种方式: 方法一: 在类路径下寻找配置文件来实例化容器 [推荐使用] ApplicationContext ctx ...

  3. Spring管理bean的生命周期

    1: bean的创建:   如果我们默认的scope配置为Singleton的话, bean的创建实在Spring容器创建的时候创建: 如果scope的配置为Prototype的话,bena的创建是在 ...

  4. Spring、编码剖析Spring管理Bean的原理

    引入dom4j jar包 1.新建Person接口和PersonBean public interface PersonIService { public void helloSpring(); } ...

  5. Spring第三弹—–编码剖析Spring管理Bean的原理

    先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  6. Spring——管理Bean的生命周期

    我们可以自定义bean的初始化和销毁方法,这里所指的的初始化和bean的构造不同,初始化是在bean构造完成后,对bean内部的属性或一些逻辑进行初始化. 首先要弄清一些概念: 构造(对象创建) 单实 ...

  7. 编码剖析Spring管理bean的原理

    project目录 MyClassPathXMLApplicationContext读取xml,以及实例化bean. 因为是一开始实例化配置文件所有bean,所以需要构造器完成这些工作. packag ...

  8. Spring管理Bean的三种创建方式

    1.使用类构造器实例化  (90%以上都是使用这种) <bean id=“orderService" class="cn.itcast.OrderServiceBean&qu ...

  9. 简单模拟Spring管理Bean对象

    1: 首先我们要利用dom4j进行xml的解析,将所有的bean的配置读取出来. 2:利用java的反射机制进行对象的实例化. 3: 直接获得对象 package cn.Junit.test; imp ...

随机推荐

  1. LR报:Error 27796 Failed to connect to server

    原错误信息: Action.c(58): Error -27796: Failed to connect to server "10.1.44.68:7013": [10048] ...

  2. EasyHook Creating a remote file monitor

    In this tutorial we will create a remote file monitor using EasyHook. We will cover how to: 使用EasyHo ...

  3. 如何解决excel工作簿保护密码

    自己的excel文档设置了“保护工作簿”密码,但是密码又忘记了,怎么办? 如果你会编写代码,那么这个问题非常好解决.Excel内置功能不能解决的事儿,自己编写一段代码或许就可以搞定了. 第一步,大家已 ...

  4. 歌手详情数据处理和Song类的封装

    我们现在每首歌曲的数据都是这样的 我们需要在这个数据里面去提取我们需要的部分,来构造成我们需要的数据对象 那我们要和创建singer.js一样  同样也要创建song.js类 我们还要获取到每首歌对应 ...

  5. 阶段3 1.Mybatis_09.Mybatis的多表操作_8 mybatis多对多操作-查询角色获取角色下所属用户信息

    一个角色对应多个用户 生成getter和setter 查看两个表的数据 中间表定义了谁有角色,谁没有角色 根据中间表的关系,最终查询出来的列表的数据样子.这需要两个左外链接才能实现功能. 第一个左外链 ...

  6. 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_06.mybatis的环境搭建

    创建实体类,实现Serializable接口 属性名和数据库的字段名保持一致 Date字段需要导入包 生成gettter和setter,再生成一个ToString的方法 创建持久层Dao 创建接口 里 ...

  7. HTML学习之==>CSS

    一.CSS选择器 id选择器 class选择器 标签选择器 标签层级选择器 class层级选择器 id层级选择器 id组合选择器 class组合选择器 属性选择器 <!DOCTYPE html& ...

  8. 【算法与数据结构】二叉堆和优先队列 Priority Queue

    优先队列的特点 普通队列遵守先进先出(FIFO)的规则,而优先队列虽然也叫队列,规则有所不同: 最大优先队列:优先级最高的元素先出队 最小优先队列:优先级最低的元素先出队 优先队列可以用下面几种数据结 ...

  9. input函数以及while处理列表和字典

    一.函数input()的工作原理 .input()函数:获取输入的字符串 示例: message = input('请输入信息,方便电脑显示') print(message) print('您输入的信 ...

  10. tomcat启动失败的三种方法

    Tomcat启动失败的解决办法 1. 重复映射 用eclipse开发时,用Eclipse开发,新建了的servlet会有一个url-pattern声明: 这样就不需要再在web.xml中添加映射,如果 ...