Spring框架搭建_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Spring框架搭建

Spring框架搭建

 2013/12/19 16:09:18  mwxxhdb  程序员俱乐部  我要评论(0)
  • 摘要:本教程中使用的Spring版本:spring-framework-4.0.0.M3-dist.zip教程目的:搭建基于Spring的J2SE框架(非J2EE)本次项目导入的jar包有:spring-beans-4.0.0.M3.jarspring-context-4.0.0.M3.jarspring-core-4.0.0.M3.jarspring-context-support-4.0.0.M3.jarcommons-logging-1.0.4.jarspring-aop-4.0.0.M3
  • 标签:Spring

本教程中使用的Spring版本:spring-framework-4.0.0.M3-dist.zip

教程目的:搭建基于Spring的J2SE框架(非J2EE

?

本次项目导入的jar包有:

spring-beans-4.0.0.M3.jar
spring-context-4.0.0.M3.jar
spring-core-4.0.0.M3.jar
spring-context-support-4.0.0.M3.jar
commons-logging-1.0.4.jar
spring-aop-4.0.0.M3.jar
spring-expression-4.0.0.M3.jar

?

配置文件

class="xml" name="code"><?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.hdb.*"></context:component-scan>

</beans>

?

package com.hdb.module.user.dao;

import org.springframework.stereotype.Component;

@Component
public class UserDao {
	public UserDao() {
		System.out.println(UserDao.class);
	}

	public void test() {
		System.out.println("test");
	}
}

?

package com.hdb.module.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hdb.module.user.dao.UserDao;

@Component
public class UserServiceImpl {
	@Autowired
	private UserDao userDao;

	public UserServiceImpl() {
		System.out.println(UserServiceImpl.class);
	}

	public void test() {
		this.userDao.test();
	}

}

?

?
package com.hdb.framework.common;

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

public final class Global {
	private static ApplicationContext context;

	public final static <T> T getBean(Class<T> cls) {
		init();
		T bean = context.getBean(cls);
		return bean;
	}

	public final static void init() {
		if (context == null) {
			context = new FileSystemXmlApplicationContext("config/spring/spring.xml");
		}
	}
}
?
package com.hdb.module.user;

import com.hdb.framework.common.Global;
import com.hdb.module.user.service.UserServiceImpl;

public class TestUser {
	public static void main(String[] args) {
		UserServiceImpl userService = Global.getBean(UserServiceImpl.class);
		userService.test();
	}
}
发表评论
用户名: 匿名