spring案列——xml配置
一、需要的jar包
spring.jar(官网下载)
commons-logging.jar
二、项目结构
三、entity(实体类)
package com.team.model; public class User { private String username;
private String password;
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
package com.team.dao; import com.team.model.User; public interface UserDAO {
public void save(User user);
}
package com.team.dao; import com.team.model.User; public class UserDAOImpl implements UserDAO{ @Override
public void save(User user) {
System.out.println("保存");
} }
五、service
package com.team.service; import com.team.model.User; public interface UserService {
public void add(User user);
}
package com.team.service; import com.team.dao.UserDAO;
import com.team.model.User; public class UserServiceImpl implements 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;
} }
六、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="u" class="com.team.dao.UserDAOImpl"></bean> <bean id="userService" class="com.team.service.UserServiceImpl" >
<property name="userDAO" ref="u"></property>
</bean>
</beans>
七、test
package com.team.service; import static org.junit.Assert.*; import org.junit.Test;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.team.model.User; public class UserServiceTest { @Test
public void testAdd() {
//ApplicationContext继承BeanFactory(最根)
//BeanFactory有的功能,ApplicationContext也有,并且能控制bean的生命周期(BeanFactory不能)
//ClassPathXmlApplicationContext()可以放置多个配置文件String[] 比如配置UserService 、TeacherService 多人开发有利
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService) ac.getBean("userService");
User u=new User();
userService.add(u);
} }
八、效果
spring案列——xml配置的更多相关文章
- spring案列——annotation配置
一.需要的jar包 spring.jar commons-loggin.jar commons-loggin.jar commons-annotation.jar 二.项目结构 三.entity pa ...
- Spring 入门 web.xml配置详解
Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...
- spring之pom.xml配置
spring之pom.xml配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- 这一次搞懂Spring Web零xml配置原理以及父子容器关系
前言 在使用Spring和SpringMVC的老版本进行开发时,我们需要配置很多的xml文件,非常的繁琐,总是让用户自行选择配置也是非常不好的.基于约定大于配置的规定,Spring提供了很多注解帮助我 ...
- Spring IOC-基于XML配置的容器
Spring IOC-基于XML配置的容器 我们先分析一下AbstractXmlApplicationContext这个容器的加载过程. AbstractXmlApplicationContext的老 ...
- Spring Ioc容器xml配置
Spring Ioc容器xml配置基本结构: <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- Spring 使用AOP——xml配置
目录 AOP介绍 Spring进行2种实现AOP的方式 导入jar包 基于schema-based方式实现AOP 创建前置通知 创建后置通知 修改Spring配置文件 基于schema-based方式 ...
- Spring RedisTemplate操作-xml配置(1)
网上没能找到全的spring redistemplate操作例子,故特意化了点时间做了接口调用练习,基本包含了所有redistemplate方法. 该操作例子是个系列,该片为spring xml配置, ...
- 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决
问题描述: 在配置spring的applicationContext.xml中的默认事务管理器的时候可能会出现这样的错误: Error occured processing XML 'org/spri ...
随机推荐
- show_space查看对象空间使用情况
CREATE OR REPLACE PROCEDURE show_space(p_segname IN VARCHAR2, p_owner IN VARCHAR2 DEFAULT USER, p_ty ...
- Cause: org.xml.sax.SAXParseException; lineNumber: 45; columnNumber: 62; 元素内容必须由格式正确的字符数据或标记组成。
三月 09, 2018 12:13:39 下午 org.apache.catalina.core.StandardContext listenerStart严重: Exception sending ...
- 如何快速实现一个command
新建一个类,实现icoomand接口 定义一个委托,为测试方便,先不考虑CanExecute的情况. 越简单越好. 代码如下: public class ExitHandler : ICommand ...
- RegExp.$1 简单理解
RegExp 是javascript中的一个内置对象.为正则表达式.RegExp.$1是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串, 以此类推,RegExp. ...
- Error running : Address localhost:1099 is already in use
运行报错: Error running : Address localhost:1099 is already in use 解决方法: 打开任务管理器,将后台的java.exe进程都关掉,再次运行 ...
- JdbcTemplate实现CRUD操作
------------------siwuxie095 JdbcTemplate 实现 CRUD 操作 1.J ...
- 使用VisualSVN Server搭建SVN服务器[xyytit]
使用 VisualSVN Server来实现主要的 SVN功能则要比使用原始的 SVN和 Apache相配合来实现源代码的 SVN管理简单的多,上手也没有那么复杂. 下面就看看详细的说明 Visual ...
- Python学习记录day7
目录 Python学习记录day7 1. 面向过程 VS 面向对象 编程范式 2. 面向对象特性 3. 类的定义.构造函数和公有属性 4. 类的析构函数 5. 类的继承 6. 经典类vs新式类 7. ...
- phpcms如何给已有的模块添加新功能?
phpcms如何给已有的模块添加新功能? 方法一:直接在模块里的控制器文件中添加功能. 不建议使用此方法,因为一旦phpcms升级,有可能会覆盖模块中的文件, 导致你添加的功能丢失. 方法二:新建一个 ...
- Apache Cordova vs Adobe PhoneGap: the differences and which one to use
http://www.makehybridapps.com/2014/06/09/cordova-vs-phonegap-the-differences-and-which-one-to-use/