当前位置: 移动技术网 > IT编程>开发语言>Java > spring_02工具及接口案例

spring_02工具及接口案例

2019年02月09日  | 移动技术网IT编程  | 我要评论

 

1.spring工具类:applicationcontextutil.java,可以返回加载配置文件的容器对象

package com.ahd.utils;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class applicationcontextutil {
    static applicationcontext ac=null;
    
    //创建私有无参构造方法
    private applicationcontextutil(){}
    
    static{
        ac=new classpathxmlapplicationcontext("beans.xml");
    }
    public static applicationcontext getapplicationcontext(){
        return ac;
} }

 

2.spring开发提倡接口编程,配合di技术可以层与层的解耦,通过接口,   配置文件可以很容易把一个类信息改变成另一个类

 

3.思路

  1).创建一个接口validateuser

  2).两个类实现接口,

  3).把对象配置到spring容器中

  4).使用(通过接口来获取getbean获得的对象)

 

4.具体实现

  接口validateuser   

package com.ahd.service;

public interface validateuser {
    public void check();
}

 

  

  实现接口的类checkuser1

package com.ahd.serviceimpl;

import com.ahd.service.validateuser;

public class checkuser1 implements validateuser{
    private string username;
    private string password;
     
    @override
    public void check() {
        system.out.println(username+password);
        system.out.println("通过xml验证成功");
    }

    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;
    }
}

 

 

  实现接口的类checkuser2

package com.ahd.serviceimpl;

import com.ahd.service.validateuser;

public class checkuser2 implements validateuser{
    private string username;
    private string password;
     
    @override
    public void check() {
        system.out.println(username+password);
        system.out.println("通过数据库验证成功");
    }

    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;
    } 
}

 

  配置文件beans.xml,配置的两个bean对象可以相互替换(注释的和没有注释的)

<?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.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!-- 在容器文件中配置数据源(bean/entity/service/dao/pojo) -->
    <!-- bean元素的作用是当spring框架加载的时候,spring会自动为bean类 userservice类的属性设置值  id就是创建对象的对象名 -->
        <!-- 
    <bean id="validateuser" class="com.ahd.serviceimpl.checkuser1">
        <property name="username">
            <value>爱华顿g</value>
        </property>
        <property name="password" value="123456"></property>
    </bean>
 -->
    <bean id="validateuser" class="com.ahd.serviceimpl.checkuser2">
        <property name="username">
            <value>爱华顿g</value>
        </property>
        <property name="password" value="123456"></property>
    </bean>
    
</beans>

 

 

  测试代码test

package com.ahd.test;

import static org.junit.assert.*;

import com.ahd.service.validateuser;
import com.ahd.utils.applicationcontextutil;

public class test {

    @org.junit.test
    public void test() {
        //使用接口实现
        validateuser vu=(validateuser) applicationcontextutil.getapplicationcontext().getbean("validateuser");
        vu.check();
    }

}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网