spring实例入门
首先是bean文件:
package onlyfun.caterpillar;
public class HelloBean {
private String helloWord = "";
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
}
public String getHelloWord() {
return helloWord;
}
}
再配置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-2.0.xsd">
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
<property name="helloWord" value="Hello!Justin!"></property>
</bean>
</beans>
最后写测试代码:
package onlyfun.caterpillar;
import java.io.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SpringTest {
public static void main(String[] args) throws IOException {
//BeanFactory方式读取bean
//网上有用FileInputStream读bean.xml的例子,但似乎用的是老版本的spring
Resource res = new ClassPathResource(
"onlyfun/caterpillar/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
HelloBean hello = (HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
// ApplicationContext方式读取bean
// ApplicationContext application = new ClassPathXmlApplicationContext("onlyfun/caterpillar/bean.xml");
// HelloBean hello = (HelloBean) application.getBean("helloBean");
// System.out.println(hello.getHelloWord());
}
}
要导入的包除了有spring的包外,还有commons-logging的包,在网上很容易下载。
本例中各包版本:
spring : spring-framework-2.5.6.SEC01
commons-logging : commons-logging-1.1.1
注意:本例中这3个文件在同一目录下
说
明:两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比,提供了更多的扩展功能,但其主要区别
在于后者是延迟加载,如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用调用getBean方法才会抛出异常;而
ApplicationContext则在初始化自身是检验,这样有利于检查所依赖属性是否注入;所以通常情况下我们选择使用
ApplicationContext.
参考:http://dinghaoliang.blog.163.com/blog/static/126540714201021234644910/
spring实例入门的更多相关文章
- Spring oxm入门实例
O/XMapper是什么? Spring3.0的一个新特性是O/XMapper.O/X映射器这个概念并不新鲜,O代表Object,X代表XML.它的目的是在Java对象(几乎总是一个plainoldJ ...
- Spring MVC入门的实例
作为Spring MVC入门,以XML配置的方式为例.首先需要配置Web工程的web.xml文件. 代码清单14-1:web.xml配置Spring MVC <?xml version=&q ...
- spring boot 系列之一:spring boot 入门
最近在学习spring boot,感觉确实很好用,开发环境搭建和部署确实省去了很多不必须要的重复劳动. 接下来就让我们一起来复习下. 一.什么是spring boot ? spring boot是干嘛 ...
- Spring Cloud 入门教程(一): 服务注册
1. 什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...
- Spring Cloud入门教程(二):客户端负载均衡(Ribbon)
对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...
- Spring Cloud 入门 之 Config 篇(六)
原文地址:Spring Cloud 入门 之 Config 篇(六) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,为了方便开发和维护项目,我们通常会将大项目拆分 ...
- Spring Cloud 入门 之 Zuul 篇(五)
原文地址:Spring Cloud 入门 之 Zuul 篇(五) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口 ...
- Spring Cloud 入门 之 Hystrix 篇(四)
原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...
- spring cloud 入门系列:总结
从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...
随机推荐
- Cisco 日常巡检命令
https://www.cnblogs.com/qzqdy/p/8116903.html 日常排错命令6 交换机的前面板有几个指示灯,用于监控系统的活动和性能.这些指示灯称之为发二极管(LED) 1. ...
- Node 各个版本支持ES2015特性的网站
如果想了解Node 各个版本支持ES2015到那个程度,可以看下面网站. https://node.green/
- 解决Eclipse点击运行后控制台不能自动弹出的问题
解决方案: 选择Window-->Preferences-->Run.Debug-->Console 勾选"Show when program writest to sta ...
- kafka_2.11-2.0.0_安装部署
参考博文:kafka 配置文件参数详解 参考博文:Kafka[第一篇]Kafka集群搭建 参考博文:如何为Kafka集群选择合适的Partitions数量 参考博文:Kafka Server.prop ...
- [福大软工] Z班 第7次成绩排行榜
作业要求 http://www.cnblogs.com/easteast/p/7668887.html 评分细则 本次作业评分较为简单,只包含了两个方面的得分,一个是团队任务的计划(10'),一个是采 ...
- Spring的jdbc模板2:使用开源的连接池
上篇简要介绍了如何在spring中配置默认的连接池和jdbc模板,这篇来介绍开源的连接池配置与属性引入 C3P0连接池配置: 引入jar包 配置c3p0连接池 <?xml version=&qu ...
- Eclipse使用过程中的经验总结
1.Eclipse中如何配置JDK的Documents和Sources? "Windows"-> "Preferences"-> "Jav ...
- nodejs websocket
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> <script ty ...
- InfluxDB部署
InfluxDB介绍 官网:https://www.influxdata.com/ 文档:https://docs.influxdata.com/influxdb/v1.2/introduction/ ...
- 查看linux中的TCP连接数
一.查看哪些IP连接本机 netstat -an 二.查看TCP连接数 1)统计80端口连接数netstat -nat|grep -i "80"|wc -l 2)统计httpd协议 ...