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

0 0
原创粉丝点击