java spring(1) HelloWorld
来源:互联网 发布:解析软件 编辑:程序博客网 时间:2024/06/06 00:35
一. spring 的eclipse 插件
eclipse -->Help--> install new-->add --> location = http://springide.org/updatesite
勾选4个带IDE 的项目,如图1
图 1
二. 代码(具体的spring 创建步骤,以及每一步的作用,在代码的注释文件中有)
1. 代码 bean 类HelloWorld.java
package com.atguigu.spring.beans;/** * Hello world! * */public class HelloWorld {private String name;public void setName2(String name){System.out.println("setName方法:"+name);this.name = name;}public String getName() {return name;}public void hello(){System.out.println("hello: "+name);}public HelloWorld(){System.out.println("helloworld constructor");}}
代码service 类 Main.java
package com.atguigu.spring.beans;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {/* 传统方法,需要3步。而用了spring 之后,第一步第二步可以交给spring 来做 //1.创建HelloWorld 的一个对象HelloWorld helloWorld = new HelloWorld();//2.为name 属性赋值helloWorld.setName("atguigu");*///3.调用hello 方法//helloWorld.hello();///////////////////////////////spring 方法创建对象//////////////////////////////////1.创建Spring 的IOC 容器对象 如果只执行这一句,spring帮我们做两件事,构造方法 helloworld constructor set 方法 setName方法:SpringApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//ApplicationContext 是个接口,代表spring里面的IOC 容器//2.从IOC 容器中获取Bean 实例//HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");//参数是bean 的id//3. 调用hello 方法//helloWorld.hello();}}
配置文件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 --><!-- 这里的name 属性要和类中的set方法后面的名字相对应 --><bean id="helloWorld" class="com.atguigu.spring.beans.HelloWorld"><!-- class 是全类名,id 是类名第一个字母小写 --><property name="name2" value="Spring"></property><!-- 这是是用反射的方式,由spring 帮我们创建一个对象 --></bean></beans>
4. 代码结构如图2
图 2
5. 运行结果
hello: spring
1 0
- java spring(1) HelloWorld
- spring-1:Spring helloWorld
- 1、Spring之HelloWorld
- spring(1) helloworld
- Spring-1 helloworld
- Spring In Action (1) -- HelloWorld
- Java Web实战03--Spring之HelloWorld
- Helloworld Spring Security Java config页面翻译
- Spring(1)--初识Spring之HelloWorld
- Spring helloworld
- spring helloworld
- spring helloworld
- Spring -- HelloWorld
- spring helloworld
- Spring HelloWorld
- Spring--HelloWorld
- spring HelloWorld!
- spring-HelloWorld
- 分析iOS Crash文件:符号化iOS Crash文件的3种方法
- Apache Spark Jobs 性能调优(一)
- 织梦tag标签伪静态
- 关于UILabel和UIButton添加下划线
- 归档和反归档
- java spring(1) HelloWorld
- 解决无法修改注册表的情况
- LeetCode Longest Valid Parentheses
- 预处理优化例题
- showModalDialog打开的页面中调用父窗口的方法
- Animation动画详解(四)——ValueAnimator基本使用
- 提高录音音量
- shell
- 大数据量下高并发同步的讲解