首页 技术 正文
技术 2022年11月15日
0 收藏 487 点赞 4,337 浏览 2028 个字

这里是关于Hello World的一些基本的操作

Spring 是一个重量级的容器框架,用来配置bean并维护bean之间的关系的框架

想要最初的使用Spring就要学会最基本的配置

<1>引入包,Spring.jar和common-logging.jar这是最基本的两个包

<3>创建一个包com.sun.service,在下面创建一个UserService.java文件

<!!!!!注意这里没有将配置文件放入web.xml里面是 因为本测试工程压根就不是一个web功能,因为spring框架不一定需要在web下面才能运行和struts不同>

package com.sun.service;public class UserService {
private String name;

public UserService(){
                     System.out.println(“this is chushihua”);
              }

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的最基本的配置运用

源码:http://pan.baidu.com/s/1o7Flkvg

提供Spring中文手册:http://pan.baidu.com/s/1nvb7OZv

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892