标签:style blog http color io 使用 ar java strong
这里是关于Hello World的一些基本的操作
Spring 是一个重量级的容器框架,用来配置bean并维护bean之间的关系的框架
想要最初的使用Spring就要学会最基本的配置
<1>引入包,Spring.jar和common-logging.jar这是最基本的两个包
<3>创建一个包com.sun.service,在下面创建一个UserService.java文件
package com.sun.service;
public class UserService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello() {
System.out.print("hello----"+this.getName());
}
}
<2>创建spring的一个核心文件,applicationContext.xml[相当于hibernate.cfg.xml],一般放在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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="userService" class="com.sun.service.UserService"> <property name="name"> <value>sunxin</value> </property> </bean> </beans>
<3>创建一个包com.sun.test,在下面创建一个测试文件Test.java
package com.sun.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sun.service.UserService;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//UserService userService = new UserService();
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ac.getBean("userService");
//us.setName("sunzhiyan");
us.sayHello();
}
}
这样就能输出 “hello----sunxin”,完成了Spring的最基本的配置运用
标签:style blog http color io 使用 ar java strong
原文地址:http://www.cnblogs.com/sunxun/p/4025810.html