Spring入门级配置

 首先说明一下,在实际的企业项目开发中,用到的都会是 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

 搞定...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值