这里我们使用model<实体>,Dao<数据局操作>,Service<业务>模拟。
1.新建项目,导入两个jar包
A、 Spring.jar内部包含控制反转的内容
B、 Commons-logging.jar是我们用来输出日志用的
2.编写你要用到的实体类(model,这里我们使用User)
package com.xxx.model;
public class User {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
3.编写你的DAO(数据操作需要的类)
package com.xxx.dao;
import com.xxx.model.User;
public interface UserDAO {
public void save(User user);
}
和它的Impl类(有一个好处,用于扩展XXXDAO的功能):
package com.hw.dao.impl;
import com.xxx.dao.UserDAO;
import com.xxx.model.User;
public class UserDAOImpl implements UserDAO {
public void save(User user) {
System.out.println("user succeed add!");
}
}
4.好了,接下来可以写业务实现了。
package com.xxx.service;
import com.xxx.dao.UserDAO;
import com.xxx.model.User;
public class UserService {
private UserDAO userDAO;
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
5.好的接下来,我们来配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="userDAO" class="com.hw.dao.impl.UserDAOImpl"></bean>
<bean id="userService" class="com.hw.service.UserService">
<property name="userDAO" ref="userDAO" />
</bean>
</beans>
6.接下来就可以建立一个JUnit的测试类来测试了。
package com.xxx.service;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hw.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
//这里用到的是配置文件的名称,初始化。
BeanFactory applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)applicationContext.getBean("userService");
User jack = new User();
jack.setUsername("Joy");
service.add(jack);
}
}
7.让我查看结果:
2014-2-20 13:46:03 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1fae3c6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1fae3c6]; startup date [Thu Feb 20 13:46:03 CST 2014]; root of context hierarchy
2014-2-20 13:46:03 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2014-2-20 13:46:03 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1fae3c6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@12a3793
2014-2-20 13:46:03 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12a3793: defining beans [userDAO,userService]; root of factory hierarchy
user succeed add!
已经正确输出。
当我们配置完第一个小程序之后,我们来回头看看。最后任务交到了BeanFactory applicationContexr , 初始化一个UserService service,这样就接管了你的程序控制能力。(也是一种依赖注入)
从model到DAO到Service,也许很烦,一个小小的程序写了这么多包。但是,随着程序越来越复杂,你会发现这个设计的好处。。
简单属性注入:
那个属性对应哪个类,就在那个类中直接设置,用的比较少。
比如我想在UserDAO中增加 age 属性,我只要在设置 age 的getter setter方法,然后在配置文件中,给出property的值。这种用法用的并不多~~