Spring_HelloWorld

目录:

各个类文件:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hy</groupId>
<artifactId>spring-1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>spring-1</finalName>
</build>
<properties>
<!-- Spring -->
<spring-framework.version>4.1.6.RELEASE</spring-framework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring需要的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring-framework.version}</version>
</dependency>
</dependencies>
</project>

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 id="helloWord" class="com.hy.spring.beans.HelloWord">
<property name="name" value="spring"></property>
</bean>
</beans>

HelloWord.java

package com.hy.spring.beans;

public class HelloWord {
private String name;

public void setName(String name) {
System.out.println("setName:" + name);
this.name = name;
}

public void hello(){
System.out.println("Hello:" + name);
}

public HelloWord() {
System.out.println("HelloWorld‘s Constructor");
}
}

Main.java

package com.hy.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
//1.创建 Spring 的 IOC容器的对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取Bean实例
HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
//3.调用hello方法
helloWord.hello();
}

}

时间: 2024-10-17 05:32:23

Spring_HelloWorld的相关文章

1) Spring_HelloWorld

1. Spring Tool Suite™ 方式一:下载对应eclipse版本的文件,离线安装 4.4.2 springsource-tool-suite-3.6.4.RELEASE-e4.4.2-updatesite.zip 87MB 选择 Core / Spring IDE Extensions / Spring IDE Integrations / Spring IDE Resources / Spring IDE 方式二:在线安装 install new sorftware - add

spring 课程

官网 参考文档 // 1. Spring_HelloWorld 20:22 // 2. Spring_IOC&DI概述 08:07 // 3. Spring_配置 Bean 21:58 // 4. Spring_属性配置细节 42:56 // 5. Spring_自动装配 14:05 // 6. Spring_Bean 之间的关系 14:09 // 7. Spring_Bean 的作用域 07:45 // 8. Spring_使用外部属性文件 08:38 // 9. Spring_SpEL 12

java之spring之helloword

这篇文章主要讲 spring的基础的使用案例 项目整体目录结构: 1.新建一个java项目:spring_helloworld 2.在项目下创建一个lib文件夹,并把一些必须的jar包复制过去 新建lib文件夹 把需要的jar包复制到lib文件夹下,jar包如下: commons-logging-1.1.1.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.j