您的位置:首页 > 编程语言 > Java开发

spring-HelloWorld

2016-02-22 22:07 525 查看
准备工作,安装SpringIDE,找到SpringSource-tool-suit,然后按照,

关键的一部在与找到之后该安转那些呢,答案是只安转带有SpringIDE的,

有四个,且不要点击联网进行更新

第一步:新建一个Dynamic Web Project,名字叫SpringMvc-1

第二步:jar 包:
commons-logging-1.1.3.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

添加配置文件(在src目录下)

<?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,反射,创建类,set方法  -->
<bean id="helloWorld" class="com.atguigu.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>

</bean>
</beans>


实体类

package com.atguigu.spring.beans;

public class HelloWorld
{
String name;
public void setName(String name)
{
this.name = name;
}
public void hello()
{
System.out.println("hello "+name);
}
public HelloWorld()
{
System.out.println("construct...");
}
}


测试类

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)
{
//创建对象,
HelloWorld helloWorld = new HelloWorld();
//为属性赋值
helloWorld.setName("atguigu");
//调用方法
helloWorld.hello();
//创建spring的IOC容器
// 1.Appl ctx = new ClassPathXmlApplica
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC中获取bean实例
//HelloWorld helloWorld2 =  (HelloWorld) act.getBean("helloWorld");
//3.调用hello方法
//helloWorld2.hello();
}
}


* IOC(Inversion of Control):其意思是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源(比如程序读取配置文件).
* 作为回应,容器适时的返回资源,而应用了IOC之后,则是容器主动地将资源推送给他所管理的组件,组件所要做的仅是选择一种合适的方式来接受资源
* 这种行为也被称为查找的被动形式,
* DI(dependency Injection)---IOC的另一中表达方式:即组件以一些预先定义的方式(例如setter方法)接受来自容器的资源注入
* 相对于IOC而言,这种表述更加直接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: