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"> ...
随机推荐
- .NET Core常用配置文件示例
.NET Core相关地址: 1.官网:https://www.microsoft.com/net 2..NET Core:http://dotnet.github.io/3.Getting Star ...
- docker入门指南(转载)
原文: http://bg.biedalian.com/2014/11/20/docker-start.html 关于 docker 今天云平台的同事提到, 现在的运维就是恶性循环, 因为大家都在申请 ...
- NEC的学习笔记
写过很多代码后,会有代码的规范有一些需求,会有想写出美观.规范.易懂的代码. 今天学习了NEC,全称Nice Easy CSS(http://nec.netease.com/),顾名思义,就是为了写简 ...
- JSON详解 .net
之前json掌握的不好,浪费了好多时间在查找一些json有关的转换问题,我所知道的方法只有把json序列化和反序列化一下,但是太麻烦了我觉得,所以就在找一些更简单又方便使用的方法.也许这个会有用吧,所 ...
- tomcat已 .war 包的形式发布项目
一:首相将写好的工程打成.war 文件包, 借助eclipse工具完成. 右键项目名称 --> Export --> WAR file 进入如下图 二: 进入到Tomcat的 webap ...
- sublime: useful commands
CMD+R go to function in current file CMD+Option+Down find function definition in another file (from ...
- C安全编码--数组
建议和规则 建议: 理解数组的工作方式 获取数组的长度时不要对指针应用sizeof操作符 显示地指定数组的边界,即使它已经由初始化值列表隐式地指定 规则: 保证数组索引位于合法的范围内 在所有源文件中 ...
- 关于NSDate和NSDateFormatter的几个常用方法
/** * NSDate常见类方法 */ // 获得当前时间 NSDate *date1 = [NSDate date]; // 类方法 // NSDate *date1 = [[NSDate ...
- 【转】IOS开发资源汇总
转自:http://blog.csdn.net/favormm/article/details/6664970 如何用Facebook graphic api上传视频: http://develope ...
- chrome浏览器font-size<12px无效解决办法
当样式设定font-size<12px时,chrome浏览器里字体显示仍为12px:如font-size:11px; 但是chrome还是12px的大小,很不听话. 今天我就遇到了这样的问题?网 ...