Spring 一二事(1)

简介: 简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下第一个小工厂先暂时不用maven,下一个会用maven来来配置 jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新,基本核心都是一样的),引入spring.

简单介绍一下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!");
    }
}

测试:

 1 package com.lee.spring001.createobject;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class HelloWorldTest {
 8 
 9     @Test
10     public void testHelloWorld() {
11         
12         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
13         
15         HelloWorld hello = (HelloWorld)context.getBean("helloWorld");
16         hello.hello();
17     }
18 }

这样运行junit,成功

 

github地址:https://github.com/leechenxiang/maven-spring001-helloworld

相关文章
|
Java Spring
FreeMarker 一二事 - 静态模板结合spring展示
freemarker可以脱离web使用 前一篇文章使用了普通的方法 这回说说结合spring pom额外引入这个jar包 1 2 org.springframework 3 spring-context-support 4 ${spring.
966 0
|
NoSQL Java Redis
Redis 一二事 - 在spring中使用jedis 连接调试单机redis以及集群redis
Redis真是好,其中的键值用起来真心强大啊有木有, 之前的文章讲过搭建了redis集群 那么咋们该如何调用单机版的redis以及集群版的redis来使用缓存服务呢? 先讲讲单机版的,单机版redis安装非常简单,不多说了,直接使用命令:  1 [root@nginx bin]# .
967 0
|
Java Spring
Spring 一二事(2)
静态工厂方法及实例工厂的使用: applicationContext.xml: 1    2 4 5 6 8 9 HelloWorldFactory.
791 0
|
Java Spring
Spring 一二事(3) - 别名
别名就是可以通过另外一个名字来访问如下,已有bean:helloWorld3,那么定义别名(alias )后,就能使用“abc”来访问 1    3 4 5 6 HelloWorld hello = (HelloWorld)context.
735 0
|
Java Spring 前端开发
Spring 一二事(4) - 单例
spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session 在配置spring mvc的action时,可以对action使用 prototype 1    2 4 5 6 bean的创建销毁过程: 1 package com.
867 0
|
Java Spring
Spring 一二事(5) - 依赖注入
1 2 3 4 5 6 7 8 9 10 11 a 12 b 13...
888 0
|
前端开发 Java Spring
Spring 一二事(6) - IOC MVC 简易搭建
1 2 3 4 5 6 7 IStuDAO.java 1 package com.lee.
829 0
|
Java Spring 容器
Spring 一二事(7) - annotation
之前的文章大多都是一带而过,一方面比较简单,一方面不是用的注解形式 在企业开发中,主要还是使用的注解来进行开发的 1   5   主要还是用 @Resource,另外2个不常用 1 package com.
859 0
|
前端开发 Java Spring
Spring 一二事(8) - annotation 形式的 MVC
1 5 IStuDAO.java 1 package com.lee.spring012.scan.mvc.annotation; 2 3 public interface IStuDAO { 4 public void saveStu(); 5 } IStuService.
1092 0
|
XML Java 数据格式
Spring 一二事(9) - xml 形式的 AOP
AOP在spring中是非常重要的一个 在切面类中,有5种通知类型: aop:before  前置通知 aop:after-returning  后置通知 aop:after  最终通知 aop:after-throwing  异常通知 aop:around  环绕通知 1   ...
917 0