spring2.5 xml以及注解的简单入门示例(ioc)
来源:互联网 发布:初级会计怎么备考 知乎 编辑:程序博客网 时间:2024/06/03 22:42
xml
1.下载spring-framework-2.5.6-with-docs.zip,commons-logging-1.1.1.jar
2.新建Java项目,创建lib文件夹,将spring-framework-2.5.6\dist\spring.jar,commons-logging-1.1.1.jar复制在里面,并且右键jar包,add to build path,将其加入构建路径。
3.在src目录下创建com.dao.StudentDao类,类的主要作用的模拟操作数据库实体的dao层
package com.dao;public class StudentDao { public void save(){ System.out.println("save success!"); }}
4.在src目录下创建com.service.StudentService类,类的主要作用的模拟操作dao层的业务层。依赖注入(ioc)也就体现在这里。此类中创建一个私有变量StudentDao,在addStudent方法中没有new一个StudentDao,就调用StudentDao的方法。就是依靠spring的ioc。
package com.service;import com.dao.StudentDao;public class StudentService { private StudentDao sd; public StudentDao getSd() { return sd; } public void setSd(StudentDao sd) { this.sd = sd; } public void addStudent(){ sd.save(); }}
5.在src目录下创建beans.xml,ioc的相关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="studentDao" class="com.dao.StudentDao"></bean> <bean id="studentService" class="com.service.StudentService"> <property name="sd"><ref bean="studentDao"/></property> </bean></beans>
两个bean分别对应两个类,class写对应类,id是bean的唯一标识。因为在com.service.StudentService中需要注入com.dao.StudentDao,所以在com.service.StudentService对应的bean中加入<property name="sd"><ref bean="studentDao"/></property>
name是com.service.StudentService中私有变量的名称,<ref bean="studentDao"/>
是com.dao.StudentDao对应bean的id
6.在src下创建一个测试类com.test.StudentTest
package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.service.StudentService;public class StudentTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); StudentService ss = (StudentService)ac.getBean("studentService"); ss.addStudent(); }}
运行即可。
annotation
1.下载spring-framework-2.5.6-with-docs.zip,commons-logging-1.1.1.jar
2.新建Java项目,创建lib文件夹,将spring-framework-2.5.6\dist\spring.jar,commons-logging-1.1.1.jar复制在里面,并且右键jar包,add build path,将其加入构建路径。
3.在src目录下创建com.dao.studentDao类,类的主要作用的模拟操作数据库实体的dao层。
此处的@Component相当于在xml中配置了一个bean,里面的”studentDao” 相当于此bean的ID
package com.dao;import org.springframework.stereotype.Component;@Component("studentDao")public class StudentDao { public void save(){ System.out.println("save success!"); }}
4.在src目录下创建com.service.StudentService类,类的主要作用的模拟操作dao层的业务层。依赖注入(ioc)也就体现在这里。此类中创建一个私有变量StudentDao,在addStudent方法中没有new一个StudentDao,就调用StudentDao的方法。就是依靠spring的ioc。
此处的@Component作用与上面相同,@Resource(name=”studentDao”)则相当于xml中的<ref bean="studentDao"/>
,name为注入的bean的id
package com.service;import javax.annotation.Resource;import org.springframework.stereotype.Component;import com.dao.StudentDao;@Component("studentService")public class StudentService { private StudentDao sd; public StudentDao getSd() { return sd; } @Resource(name="studentDao") public void setSd(StudentDao sd) { this.sd = sd; } public void addStudent(){ sd.save(); }}
5.在src目录下创建beans.xml,ioc的相关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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.dao,com.service" /></beans>
注意beans标签里的东西不一样。
必须加入<context:annotation-config />
。
应用注解的包包括<context:component-scan base-package="com.dao,com.service" />
6.在src下创建一个测试类com.test.StudentTest
package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.service.StudentService;public class StudentTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); StudentService ss = (StudentService)ac.getBean("studentService"); ss.addStudent(); }}
运行即可。
相关源码jar包下载地址:http://download.csdn.net/detail/qwcs163/9113683
- spring2.5 xml以及注解的简单入门示例(ioc)
- spring2.5 xml的简单入门示例(aop)
- Spring2.5的注解配置简单介绍
- spring2.5 + hibernate3.2 标注(annotation)开发的简单示例
- 注解的简单示例
- Spring2.5中基于注释的IoC
- Spring2.5中基于注释的IoC
- Spring2.5中基于注释的IoC
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- java注解入门的简单介绍与示例(框架基础)
- Android 设计模式 示例集合 以及IOC注解事例 Demo
- 一个简单的由老师指导实现的模拟spring的ioc的di注解以及注解容器(AnnotationConfigApplicationContext)代码
- 模拟Spring解析xml文件,以及实现IOC (DI)的示例
- Spring IOC(XML和注解)
- spring2.5基于注解的配置
- Spring2.5注解式的配置
- Spring2.5注解式的配置
- lintcode-用栈实现队列-40
- Deep Learning(深度学习)学习笔记整理
- eclipse -- debug增加条件的用法
- sqlserver查询分析器执行sql语句顺序
- ios8.0下CLLocationManager定位服务需要授权了
- spring2.5 xml以及注解的简单入门示例(ioc)
- Struts 2 Hello World Example
- iOS CLLocationManager定位
- 百度2015 在线笔试题(2)
- Android Studio删除module
- After 500: 写了第500篇博客,然后呢?
- 关于Spring中的<idref/>
- UIPickerView 学习
- tcp/ip 连接三次握手,断开四次握手