Spring学习笔记1(初级入门)

spring一个轻量级框架,最大的两个特点IOC(控制反转)和Aop(面向切面编程).

1.环境的搭建
我是myeclipse8.5,所用的spring版本为spring3.1.0,将解压后dist中的jar包导入,在spring-framework-1.1.4\lib\jakarta-commons中将commons-logging.jar导入
2.后台代码的编写
UserDAO.java
package com.dong.dao;

import com.dong.model.User;

public interface UserDAO {
	
	public void save(User user);
}


UserDAOImpl .java
package com.dong.impl;

import com.dong.dao.UserDAO;
import com.dong.model.User;

public class UserDAOImpl implements UserDAO{
	public void save(User u){
		System.out.println("haha");
	}
}

User.java
package com.dong.model;

public class User {
	private String username;
	private String userpassword;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUserpassword() {
		return userpassword;
	}
	public void setUserpassword(String userpassword) {
		this.userpassword = userpassword;
	}
	
}


UserService .java
package com.dong.service;

import com.dong.dao.UserDAO;
import com.dong.impl.UserDAOImpl;
import com.dong.model.User;


public class UserService {
	private UserDAO userDAO;
	//增加用户
	public void add(User u){
		this.userDAO.save(u);
	}
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
}

具体结构入图01
3.xml文件编写
编写beans.xml,代码如下
<?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:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.1.xsd 
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

<bean id="u" class="com.dong.impl.UserDAOImpl"/>
<bean id="userService" class="com.dong.service.UserService">
	<property name="userDAO" ref="u"></property>
         <!--此处还一种写法,即将ref分开写成property的子节点-->
<!--         
         <property name="userDAO">
          <ref baen="u"/>
         </property>

-->
</bean>
</beans>

4.编写测试类
好了之后编写一个测试类,现在我想测试UserService.java,我首先可以右键中UserService.java,new-->最后一个others-->Junit-->Junit Test Case-->如图2页面-->next-->选择add(User)这个方法-->finish之后会自动将Junit4导入到项目中.
如图2页面:选择New Junit 4 Test,package改为com.dong.test,这个test专门放测试的,name就不用管了,他默认为XxxTset.(Xxx就是所选的java名)
好了之后就开始写UserServiceTest.java了
package com.dong.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dong.model.User;
import com.dong.service.UserService;

public class UserServiceTest {

	@Test
	public void testAdd() {
                 //此处的beans.xml就是我们刚才写的xml
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
                  //获取xml中的userService,直接通过容器将其初始化
		UserService userService=(UserService)applicationContext.getBean("userService");
		User user =new User();
		user.setUsername("张氏");
		user.setUserpassword("123");
		userService.add(user);
	}

}

好了,现在只要运行一下就出来了
此处再介绍一下将api文档放到myeclipse中,首先找到你导入的包,右键选择properties,点击左边的“Javadoc Location”,直接Javadoc URL的javadoc location path直接选择spring中的spring3.1.0.RC1\docs\javadoc-api,加好了之后测试下,在UserServiceTest.java中选中ApplicationContext ,按键盘F1就会出来了。
此处再上传spring jar包说明,详见图03

你可能感兴趣的:(spring,xml)