spring之第一个spring程序
spring具体描述:
- 轻量级
- IOC:依赖注入
- AOP:面向切片编程
- 容器:spring是一个容器,包含并且管理应用的生命周期
- 框架
- 一站式
一、搭建spring开发环境
在eclipse中新建一个java项目,在项目中新建一个lib文件夹,并将以下spring需要的jar包放在lib文件夹下:

选中这五个包,点击鼠标右键,选择bulid path-->add to build path,
最终的项目目录如下:

在src下新建一个包,在包里面新建两个java文件。在src下新建一个Spring bean configure file类型的文件:applicationContext.xml
二、第一个spring程序
HelloWorld.java
package com.gong.spring.beans;
public class HelloWorld {
public HelloWorld() {
System.out.println("构造方法");
}
private String name;
public void setName(String name) {
this.name = name;
}
public void show() {
System.out.println("姓名是:"+this.name);
}
}
Main.java
package com.gong.spring.beans; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
/*
* 普通的java对象的创建
HelloWorld helloworld = new HelloWorld();
helloworld.setName("tom");
helloworld.show();
*/ //通过spring创建的对象
//1.创建spring的IOC容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从容器中获取Bean实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//3.调用方法
helloWorld.show();
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Bean -->
<bean id="helloWorld" class="com.gong.spring.beans.HelloWorld">
<property name="name" value="jack"></property>
</bean> </beans>
使用spring管理java对象时需要注意的地方:
在applicationContext.xml中,id是标识该类的名称,class是具体的某一个类名。property 是类的属性标识,name代表属性名,value代表值。
在Main.java中,先创建IOC容器对象,然后获取Bean实例,getBean()中的名称就是applicationContext.xml中id的内容。
最后输出:

可以看到,该类的构造方法也会被调用。我们可以得出,通过spring获取对象的实例会调用该类的构造方法,并且为相应的属性赋予初始值。
spring之第一个spring程序的更多相关文章
- 【Spring】创建一个Spring的入门程序
3.创建一个Spring的入门程序 简单记录 - Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)- Spring的基本应用 Spring与Spring MVC的 ...
- 第一章 第一个spring boot程序(转载)
第一章 第一个spring boot程序 本编博客转发自:http://www.cnblogs.com/java-zhao/p/5324185.html 环境: jdk:1.8.0_73 mave ...
- 第一个Spring MVC程序
最近公司项目要开始使用Spring MVC替代Struts2了,就学习了一下Spring MVC的使用.这是第一个Spring mvc程序,分别使用xml和注解两种方式. 一.使用xml格式进行构建 ...
- 2、Spring的 IoC详解(第一个Spring程序)
Spring是为了解决企业应用开发的复杂性而创建的一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架.在这句话中重点有两个,一个是IoC,另一个是AOP.今天我们讲第一个IoC. IoC概念 ...
- 我的第一个spring boot程序(spring boot 学习笔记之二)
第一个spring boot程序 写在前面:鉴于spring注解以及springMVC的配置有大量细节和知识点,在学习理解之后,我们将直接进入spring boot的学习,在后续学习中用到注解及其他相 ...
- 第一个Spring Boot程序
Windows 10家庭中文版,java version "1.8.0_152", Eclipse Oxygen.1a Release (4.7.1a),Spring Tools ...
- 干净win7要做几步才能运行第一个Spring MVC 写的动态web程序
干净win7要做几步才能运行第一个Spring MVC 写的动态web程序: 1. 下载安装jdk 2. 配置Java环境变量 3. 测试一下第1,2两步是否完全成功:http://jingyan.b ...
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
- 搭建Spring开发环境并编写第一个Spring小程序
搭建Spring开发环境并编写第一个Spring小程序 2015-05-27 0个评论 来源:茕夜 收藏 我要投稿 一.前面,我写了一篇Spring框架的基础知识文章,里面没 ...
随机推荐
- 9-1进程,进程池和socketserver
一 进程: # 什么是进程 : 运行中的程序,计算机中最小的资源分配单位# 程序开始执行就会产生一个主进程# python中主进程里面启动一个进程 —— 子进程# 同时主进程也被称为父进程# 父子进程 ...
- Oracle中组合索引的使用详解(转)
在Oracle中可以创建组合索引,即同时包含两个或两个以上列的索引.在组合索引的使用方面,Oracle有以下特点: 1. 当使用基于规则的优化器(RBO)时,只有当组合索引的前导列出现在SQL语句的w ...
- @codechef - SONATR@ Sonya and Tree
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 p 为 0~N-1 的一个排列,并给定一棵 N 个点的树. ...
- OpenStack组件系列☞Keystone
Keystone(OpenStack Identity Service)是 OpenStack 框架中负责管理身份验证.服务规则和服务令牌功能的模块.用户访问资源需要验证用户的身份与权限,服务执行操作 ...
- ODT 珂朵莉树 入门
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- SSM项目整合第一步 注册登陆实现
SSM项目整合第一步 注册: 项目目录: 一.数据库建表: 源码: ; -- ---------------------------- -- Table structure for t_user - ...
- zoj 2954 Hanoi Tower
Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...
- ImportError: DLL load failed: 找不到指定的模块。 TensorFlow 1.13
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zhenlingcn/article/details/88647288问题描述 问题环境TensorF ...
- 2018-8-10-使用-RetroShare-分享资源
title author date CreateTime categories 使用 RetroShare 分享资源 lindexi 2018-08-10 19:16:51 +0800 2018-02 ...
- [C++] WinAES的问题
WinAES是个不错的windows CAPI封装. 如果C++程序需要和java的程序进行aes加解密通讯,那么WinAES的代码是有问题的. java的aes代码缺省不会设置IV而且采用ECB模式 ...