Spring的一个入门例子
例子参考于:Spring系列教材 以及《轻量级JavaEE企业应用实战》
Axe.class
package com.how2java.bean;
public class Axe {
public String chop() {
return "使用斧头砍柴";
}
}
Person.class
package com.how2java.bean;
public class Person {
private Axe axe;
public void setAxe(Axe axe) {
this.axe = axe;
}
public void useAxe() {
System.out.println("我打算去砍点柴!");
System.out.println(axe.chop());
}
}
BeanTest.class
package com.how2java.bean; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { public static void main(String[] args) {
// TODO Auto-generated method stub //创建Spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"beans.xml"});
/*
* Spring 容器获取Bean对象主要有如下两个方法
* 1).Object getBean(String id):根据容器中Bean的id来获取指定Bean,获取Bean之后要进行强制类型转换
* 2).T getBean(String name,Class<T> requiredType): 根据容器中的Bean的id来获取指定Bean,但该方法
* 带一个泛型参数,因此获取Bean后无须进行强制类型转换。
*/
//使用方法2).来获取id为person的Bean
Person p = ctx.getBean("person",Person.class); //使用方法1).来获取id为person的Bean
//Person p = (Person) ctx.getBean("person"); p.useAxe(); } }
beans.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 配置名为person的Bean,其实现类为com.how2java.Person -->
<!-- 下面配置的property元素name为axe,该元素将驱动Spring以反射方式执行person Bean中的setAxe()方法,ref的属性值为
axe,该属性值指定以容器中名为axe的Bean作为执行setter方法的传入参数 也就是在底层,会执行一些方法,最终将name="axe"这个属性反射成setAxe()这个方法,然后因为下面的bean指定了一个名为axe的
Bean,因此通过ref="axe"来指定setter方法的参数为名为axe的Bean --> <bean id="person" class="com.how2java.bean.Person">
<!-- 控制调用setAxe()方法,将容器中的axe Bean作为传入参数 -->
<property name="axe" ref="axe" />
</bean> <!-- 配置名为axe的Bean,其实现类为com.how2java.Axe -->
<bean id="axe" class="com.how2java.bean.Axe" /> </beans>
笔记:
<property>作为<bean>的子元素,它驱动Spring在底层以反射执行一次setter方法,其中<property...>的name属性决定执行哪个setter方法,而value或者是ref决定执行setter方法传入的参数
那么什么时候用ref什么时候用value?
当传入参数是基本类型及其包装类、string等类型,使用value
如果以容器中其他Bean作为传入参数则使用ref指定传入参数
Spring的一个入门例子的更多相关文章
- spring的一个小例子(二)--解析前面的小例子
接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: ...
- spring aop 一个挡板例子
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.ann ...
- spring boot入门例子
最近学习spring boot,总结一下入门的的基础知识 1新建maven项目,修改pom.xml <project xmlns="http://maven.apache.org/PO ...
- 一个简单的iBatis入门例子
一个简单的iBatis入门例子,用ORACLE和Java测试 目录结构: 1.导入iBatis和oracle驱动. 2.创建类Person.java package com.ibeats;import ...
- Hibernate入门1 - Hibernate概述及第一个小例子
一.什么是ORM? ORM,即Object Relational Mapping.我们知道,利用面向对象的思想编写的数据库应用程序最终都是把对象信息保存在关系型数据库中,于是需要编写与底层数据库相关的 ...
- Flexpaper二次开发入门教程》(十) Flexpaper简单使用-第一个Flexpaper例子
4. Flexpaper简单使用 通过上面三章的内容,大家对Flexpaper.SWFTools应该有大概的了解了,SWF文件也已经生成了,我们开始进入Flexpaper的使用的介绍. 本章中只演示F ...
- 一个ioc例子jdk和spring版本导致问题
今天橘子松在做一个简单例子的时候,出现bug让我久久找了半小时... 天啊 不会吧 错误如下: java.lang.NoSuchMethodError: org.springframework.a ...
- MINA经典入门例子----Time Server
原文地址 http://blog.sina.com.cn/s/blog_720bdf0501010b8r.html 貌似java的IO.NIO的入门例子都有相关的Time Server Demo.本例 ...
- Spring Boot 快速入门
Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...
随机推荐
- django 处理静态文件
settings: STATIC_URL = 'static/'STATIC_ROOT = os.path.join(BASE_DIR, 'static') urls: from django.con ...
- pyglet self.
import pyglet class T(pyglet.window.Window): def __init__(self): super(T, self).__init__() self.play ...
- sublime text3 批量查找替换文件夹或项目中的字符
1.点击左上角的“菜单”,在弹出的菜单中选择“打开文件夹”. 2.在文件夹上右击,选择“在文件夹中查找”选项 3.之后会软件底部会弹出对话框,分别输入要查找的内容和替换的内容,最后点击替换按钮 4.再 ...
- mysql优化之使用iotop+pt-ioprofile定位具体top io文件
今天,将一个环境切换成行情优化后的版本后,发现io等待还是挺高,这还是第一次出现的.其他很多套环境都没有这个问题了,故iotop看了下,基本可以确定为是mysql进程的问题,如下: 但是iotop只能 ...
- python简说(二十)操作excel
一.pip install xlrdpip install xlwtpip install xlutils 二.写excel import xlwtbook = xlwt.Workbook() #新建 ...
- 推荐 Net C# 逆向反编译四大工具利器
参考:https://blog.csdn.net/kongwei521/article/details/54927689/
- markdownpad2-注册码-2017-02-23
MarkdownPad2.5/2 注册码 User: Soar360@live.com 授权: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImD ...
- 正则表达式验证HTTP地址是否合法
转载:https://blog.csdn.net/fsdad/article/details/52637426 判断url是否合法 const std::regex urlpattern(" ...
- [内核驱动] miniFilter 内核层与应用程序通信
转载:http://blog.csdn.net/heyabo/article/details/8721611 转载:http://www.cnblogs.com/ljinshuan/archive/2 ...
- log4j2使用介绍
工作中,用到了log4j2,以前只接触过log4j,也没有太过深入,这次就稍微系统的学习了以下log4j2. 一.引入pom.xml 使用maven作为项目的构建环境,pom.xml使用slf4j,s ...