spring框架的搭建的初步认识
来源:互联网 发布:国服lol2017有mac版 编辑:程序博客网 时间:2024/06/06 09:31
本文主要初步介绍spring框架的搭建和认识
首先,引入spring.jar和commons-logging.jar两个包,然后创建applicationContext.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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="userService" class="com.service.UserService"><property name="name"><value>siege</value></property><property name="byeService" ref="byeService"></property></bean><bean id="byeService" class="com.service.ByeService"><property name="name"><value>xuhaiyue</value></property></bean></beans>然后编写测试类:
先创建获取ApplicationContext对象的公共方法:
package com.util;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public final class ApplicationContextUtil {private static ApplicationContext ac=null;ApplicationContextUtil(){}static {ac=new ClassPathXmlApplicationContext("applicationContext.xml");}public static ApplicationContext getApplicationContext(){return ac;}}然后编写ByeService.java文件
package com.service;public class ByeService {private String name;public void sayBye(){System.out.println("bye,"+name);}public String getName() {return name;}public void setName(String name) {this.name = name;}}
接着编写UserService.java文件
package com.service;public class UserService {private String name;private ByeService byeService;public void hello(){System.out.println(name+",hello");byeService.sayBye();}public ByeService getByeService() {return byeService;}public void setByeService(ByeService byeService) {this.byeService = byeService;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
最后编写测试类:
package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.service.ByeService;import com.service.UserService;import com.util.ApplicationContextUtil;public class Test {public static void main(String[] args) {/*ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");UserService us=(UserService) ac.getBean("userService");us.hello();ByeService bs=(ByeService) ac.getBean("byeService");bs.sayBye();*/UserService us=(UserService) ApplicationContextUtil.getApplicationContext().getBean("userService");us.hello();}}总结:
0 0
- spring框架的搭建的初步认识
- Spring的初步认识
- spring MVC框架的初步搭建
- Bootstrap框架的初步认识
- 对spring的初步认识
- 对Spring框架初步认识
- SSH框架的初步搭建
- spring-mvc的初步认识
- Spring的MVC框架认识
- Spring的MVC框架认识
- Spring MVC框架初步搭建
- Spring框架的搭建
- Spring框架的搭建
- Spring框架的搭建
- 关于Yaf框架的初步搭建
- 指针的初步认识!
- 类的初步认识
- return的认识,初步
- JAVA实现多线程断点下载
- Different sizes of iPhone
- 关于opencv3.0 beta 编译出现ORB那边的错误的解决
- Nebula_level01
- hdu2159 二维完全背包
- spring框架的搭建的初步认识
- OCaml 中 module 和 Signature 的关系
- T0B9Tair max 女鞋 e11v YHG
- 7. Reverse Integer Leetcode Python
- hdu1171 多重背包
- poj1742 多重背包单调队列
- 服务器tomcat无法发布web项目
- NYOJ 168 房间安排
- 周二先mark一下