Spring 一二事(1)
简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下
第一个小工厂先暂时不用maven,下一个会用maven来来配置
jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新,基本核心都是一样的),引入spring.jar到lib,如下:
在src下新建applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 把一个类放入到spring容器中,该类就称为bean -->
<!-- 描述一个类 id 唯一标示 class 类名 -->
<bean id="helloWorld" class="com.lee.spring001.createobject.HelloWorld"></bean> </beans>
在com.lee.spring001.createobject包下新建类HelloWorld.java :
package com.lee.spring001.createobject; public class HelloWorld { public HelloWorld() {
System.out.println("HelloWorld...");
} /**
*
*/
public void hello() {
System.out.println("Hello world!");
}
}
测试:
package com.lee.spring001.createobject; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloWorldTest { @Test
public void testHelloWorld() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld hello = (HelloWorld)context.getBean("helloWorld");
hello.hello();
}
}
这样运行junit,成功
github地址:https://github.com/leechenxiang/maven-spring001-helloworld
Spring 一二事(1)的更多相关文章
- Spring 一二事(4) - 单例
spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session 在配置spring mvc的action时 ...
- Spring 一二事(9) - xml 形式的 AOP
AOP在spring中是非常重要的一个 在切面类中,有5种通知类型: aop:before 前置通知 aop:after-returning 后置通知 aop:after 最终通知 aop:af ...
- Spring 一二事(8) - annotation 形式的 MVC
<!-- component:把一个类放入到spring容器中,该类就是一个component 在base-package指定的包及子包下扫描所有的类 --> <context:co ...
- Spring 一二事(7) - annotation
之前的文章大多都是一带而过,一方面比较简单,一方面不是用的注解形式 在企业开发中,主要还是使用的注解来进行开发的 1 <!-- component:把一个类放入到spring容器中,该类就是一个 ...
- Spring 一二事(10) - annotation AOP
先贴出POM的内容,这个毕竟是用的maven来简单构建的 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...
- Spring 一二事(5) - 依赖注入
<!-- 依赖注入的装配过程 --> <bean id="person" class="com.lee.spring007.di.xml.setter. ...
- Spring 一二事(3) - 别名
别名就是可以通过另外一个名字来访问如下,已有bean:helloWorld3,那么定义别名(alias )后,就能使用“abc”来访问 <bean id="helloWorld3&qu ...
- Spring 一二事(2)
静态工厂方法及实例工厂的使用: applicationContext.xml: <!-- factory-method 是指调用静态工厂方法 --> <bean id="h ...
- Spring 一二事(6) - IOC MVC 简易搭建
<bean id="personAction" class="com.lee.spring008.IOC.DI.MVC.PersonAction"> ...
随机推荐
- C语言动态调用库(转)
转自:http://cloverprince.iteye.com/blog/481309 现有一个主程序用C语言写成.现在要允许第三方开发人员编写扩展的模块,约定第三方开发的模块必须提供一系列已知名称 ...
- 为什么要用rem
为什么要用rem 参考文章web app变革之rem 公司使用的375*667(也就是iPhone6)作为缩放比例标准,设计师是按照750px的标准出图 为了保证在不同的屏幕下显示效果基本等同,为此规 ...
- .NET破解之google瓦片下载及拼接
由于最近一些其他事忙,加之电脑显卡坏了,所以,好长一段时间没有更新博客了,感觉对不注关注我的朋友.从本文开始,博客更新频率将会大大降低,但每周都会更新的. 在上帝之眼论坛看到了新出来了一个google ...
- LinearLayout嵌套
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android项目实战(九):CustomShapeImageView 自定义形状的ImageView
一个两年前出来的第三方类库,具有不限于圆形ImageView的多种形状ImageView,项目开发必备 github下载地址:https://github.com/MostafaGazar/Custo ...
- IOS 网络浅析(一 网络监测~Reachability)
网络监测应用于各种需要连接网络的app设计,由于现在开发的app几乎都用到网络,因此,网络监测也成为了较为重点的知识,下面我给大家简单讲解一下网络监测的实际应用,依旧会有代码哦. 想要实现网络监测,可 ...
- 【转】IOS中的release和nil
nil和release的作用: nil就是把一个对象的指针置为空,只是切断了指针与内存中对象的联系:而release才是真正通知内存释放这个对象. 所以nil并没有释放内存,只有release才回真正 ...
- OC中NSArray
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- Android UI之下拉刷新上拉刷新实现
在实际开发中我们经常要用到上拉刷新和下拉刷新,因此今天我写了一个上拉和下拉刷新的demo,有一个自定义的下拉刷新控件 只需要在布局文件中直接引用就可以使用,非常方便,非常使用,以下是源代码: 自定义的 ...
- Effective Java 74 Implement Serializable judiciously
Disadvantage of Serializable A major cost of implementing Serializable is that it decreases the flex ...