首先说明一下,在实际的企业项目开发中,用到的都会是 Eclipse 而非 MyEclipse。因为Eclipse是完全免费的。公司当然会节省成本。估计用惯了MyEclipse的朋友,在使用Eclipse的时候会茫然失措,不知道如何下手。因为Eclipse中很多的文件是需要自己手动建立的。这也恰恰能锻炼实际能力。所以,推荐使用Eclipse而非MyEclipse。现在我用的是3.5版本.
那要使用Spring,怎么在Eclipse配置呢?先去下一个spring的版本包。这里是spring-framework-2.5.6。百度一下应该就能找到。
1. 当然要导入所需要的jar包。作为入门级配置,这里只导入四个jar包:..\spring-framework-2.5.6\dist\spring.jar,..\spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar,..\spring-framework-2.5.6\lib\jakarta-commons\commons-logging.jar,..\spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar.
看清楚了吧。把它们导入到自己建立的project里面就可以了。至于如何导入......既然到达了要学习spring的阶段,我看就不用介绍了吧...
2. 需要一个配置文件来管理spring的配置。一般名为applicationContext.xml.这里也不要觉得难,只要网上找一个,复制到 src 下即可,这里附件提供一个吧,省得你们找了。
3. 编写业务逻辑,原来怎么写,现在还怎么写。这里给个简单的例子:用户管理。首先建立一个用户bean。简单的三个字段。
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
然后写个DAO来做数据库相关操作,这里只写个简单的save方法来保存一个用户。现在都流行面向接口的编程。那我们就先建立一个接口。
public interface UserInfoDao {
public void save(UserInfo userInfo);
}
再写个实现类,为了简化起见,实现类里只打印一句话:
public class UserInfoDaoImpl4MySQL implements UserInfoDao {
@Override
public void save(UserInfo userInfo) {
System.out.println("Save a user");
}
}
注: 面向接口的编程好处从上面的类命名就可以看的出来,上面写的是MySQL实现,所以,你也可以写SqlServer,Oracle等等,然后在spring的配置里做相应的改变,可以很容易实现数据库转换。
接下来写一个service来实现用户保存。
public class UserManagerService {
private UserInfoDao userInfoDao;
public void setUserInfoDao(UserInfoDao userInfoDao) {
this.userInfoDao = userInfoDao;
}
public void save(UserInfo userInfo){
this.userInfoDao.save(userInfo);
}
}
这里提供一个userInfoDao的set方法是为了在spring里面做配置。
下面来看看spring配置文件的内容:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:sa="http://sannotations.sourceforge.net/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://sannotations.sourceforge.net/context http://sannotations.sourceforge.net/context.xsd">
<bean id="userInfoDao" class="com.xxx.spring.dao.UserInfoDaoImpl4MySQL" />
<bean id = "userManagerService" class = "com.xxx.spring.service.UserManagerService" >
<property name="userInfoDao" ref="userInfoDao" />
</bean>
</beans>
可以看到,userInfoDao被当做service的属性配置到service里面了。到这里,配置就算是完成了。
下面做个测试,建立一个包含主函数的测试类:
public class Test {
public static void main(String[] args) {
ApplicationContext atx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManagerService ums = (UserManagerService) atx.getBean("userManagerService");
UserInfo u = new UserInfo();
ums.save(u);
}
其中ApplicationContext atx = new ClassPathXmlApplicationContext("applicationContext.xml");为读取配置文件。然后用UserManagerService ums = (UserManagerService) atx.getBean("userManagerService");
这句就可以取到UserManagerService 。
测试跑一下,就可以看到结果...
Save a user
搞定...
分享到:
相关推荐
Spring是一个开源的Java平台,它提供了全面的企业级应用程序开发框架,包括依赖注入(Dependency Injection, DI),面向切面编程(Aspect-Oriented Programming, AOP)以及各种容器和服务。 2. **Bean的概念**: ...
### Spring入门级教程知识点解析 #### 一、Spring框架概览 **1.1 核心概念** Spring框架的核心在于其轻量级容器(Container),它实现了控制反转(IoC)和面向切面编程(AOP)的概念,使得开发过程更为灵活且非侵入性。...
这本书籍“一本很不错的关于spring入门的书籍”显然是针对初学者设计的,旨在帮助新手快速掌握Spring的基本概念和核心功能。 Spring框架的核心特性包括: 1. **依赖注入(Dependency Injection,简称DI)**:这是...
《Spring入门代码项目详解》 在IT行业中,Spring框架无疑是最受欢迎的Java企业级应用开发框架之一。本项目旨在帮助初学者快速入门Spring,通过实际的代码操作来理解和掌握Spring的核心概念与用法。以下是对每个子...
SSM(Spring、SpringMVC、MyBatis)是一个经典的Java企业级应用开发组合,广泛应用于各种项目中。本教程主要关注Spring的基础配置和注解使用,确保在集成SSM后能够正常运行。 首先,Spring的核心是IoC(Inversion ...