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