【spring教程之中的一个】创建一个最简单的spring样例
1、首先spring的主要思想,就是依赖注入。简单来说。就是不须要手动new对象,而这些对象由spring容器统一进行管理。
2、样例结构
如上图所看到的,採用的是mavenproject。
2、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringExample001</groupId>
<artifactId>SpringExample001</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
</dependencies>
</project>
3、spring.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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"> <bean id="do" class="com.test.pro.Do"/>
</beans>
4、Do.java
package com.test.pro;
public class Do {
public void speaking()
{
System.out.println("speaking.......");
}
}
5、測试类
package com.test.pro; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
Do did=(Do)ctx.getBean("do");
did.speaking(); } }
6、输出
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRidWx1b2dl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
7、分析
我们能够看到,在核心的spring配置文件里的spring.xml中仅仅有一句话:<bean id="do" class="com.test.pro.Do"/>,这句话指明的是有一个bean文件。名称为do,其类的地址是com.test.pro.Do。
然后就是我们的測试类里面的一段话:
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
Do did=(Do)ctx.getBean("do");
did.speaking();
这里表示声明一个上下文类,这个上下文类装载了配置文件,注意,假设这里不是採用mavenproject的话,一定要注意spring.xml的相对地址。假设实在不确定相对地址是什么。能够採用绝对地址的方式。比如:
ApplicationContext ctx=new ClassPathXmlApplicationContext("file:H:/spring.xml");
然后就是利用上下文对象来获得在配置文件里声明过的bean的演示样例,而且能够直接调用。
那么这个bean文件是什么时候实例化的,假设bean的scope是prototype的,则该Bean的实例化是在第一次使用该Bean的时候进行实例化 ,能够參考这篇文章:http://www.iteye.com/problems/93479
【spring教程之中的一个】创建一个最简单的spring样例的更多相关文章
- [Android开发教程]Android官网developer training中文版教程 - 1.1.1 创建一个Android项目
本系列持续更新中.转载请注明来源. 前言:近期打算系统学习一下Android开发,发现Android官网上的developer training也是个非常好的学习资料,于是想到一边学习一边写一个中文版 ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(一)
.新建项目File-->New-->Project-->Spring Initializr 点击Finish,一个Spring Boot web应用就创建好了.
- struts2官方 中文教程 系列一:创建一个struts2 web Application
先贴了本帖地址,以免被爬 http://www.cnblogs.com/linghaoxinpian/p/6898779.html 本教程将会通过安装struts2框架来创建一个简单的应用程序.虽然 ...
- 3dmax实例教程-使用3ds Max 创建一个完整的场景
本篇教程讲述了利用3ds max创建一个完整的场景. 灵感来源:当我在遇到一些事情睡不着觉的时候我便在努力想象一些别的事情,于是我便想到了这个场景,其实对于我的这个角色我即没有参考图也没有草稿图,有的 ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(二)
1. 创建一个Controller类 package com.example.demo; import org.springframework.web.bind.annotation.RequestM ...
- (41)Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】
已经好久没有讲一些基础的知识了,这一小节来点简单的,这也是为下节的在Spring Boot中使用多数据源做准备. 从Spring 3.0开始,增加了一种新的途径来配置Bean Definition,这 ...
- Spring Boot 使用Java代码创建Bean并注册到Spring中
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/catoop/article/details/50558333 声明同一个类下的多个实例: packa ...
- Openfire/XMPP学习之——一个简单的Smack样例
昨天讲了Openfire的搭建和配置,今天来讲一下Smack.如果对如何搭建和配置Openfire的,可以参考Openfire/XMPP学习之——Openfire的安装.配置. Smack是一个开源, ...
- Thrift源代码分析(八)--总结加一个完整的可执行的Thrift样例
前面七篇文章分析了Thrfit的方方面面,看到这里时应该对Thrift有了深入的理解. Thrift源代码分析(一)-- 基本概念 Thrift源代码分析(二)-- 协议和编解码 Thrift源代码分 ...
随机推荐
- c++11特性与cocos2d-x 3.0之std::bind与std::function
昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0 ...
- shell实现除法,保留小数点后N位
$more get_wstts_success.sh #!/bin/bash open_gw_1=$( "sh /home/admin/bin/check_wstts_success.sh& ...
- python 在Unicode和普通字符串 str 之间转换
unicodestring = u"Hello world" # 将Unicode转化为普通Python字符串:"encode" utf8string = un ...
- nginx 405 not allowed问题的解决
转载自: http://www.linuxidc.com/Linux/2012-07/66761.htm Apache.IIS.Nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否 ...
- Nginx_lua缓存问题,关闭lua_code_cache
打开nginx.conf配置server{ lua_code_cache off; //关闭lua缓存 重启后生效 server_name localhost; default_type 'text/ ...
- Linux启动与禁止SSH用户及IP的登录
以下就针对SSH方面讨论一下.假设有人特别关注Linux环境的安全性,第一就从login方面来进行讨论 1:Linux启动或禁止SSH root用户的登录 2:Linux限制SSH用户 事实上这些东西 ...
- BeamNG.drive物理引擎评鉴
BeamNG.drive是一款由BeamNG公司开发并于2013年首次发布的软体物理模拟游戏.作为模拟游戏,特别是物理模拟的粉丝,我早早就开始使用BeamNG.drive.我立即对崩溃的准确性和细节印 ...
- [转]安装openfire后admin无法登录管理控制平台
安装完openfire登录管理控制提示: Login failed:make sure your username and password are correct and that you’re a ...
- 【转】【MySql】Update批量更新与批量更新多条记录的不同值实现方法
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...
- debian下系列下的apt-get 命令与deb包的手动安装的dpkg命令
手动下载的deb包的相关操作: 操作deb 使用dpkg 命令工具, dpkg 是Debian package的简写. 下面列举常用的 操作: dpkg –I name.deb 查看 包的详细信息( ...