体验Spring的“依赖注入”的优点-阿里云开发者社区

开发者社区> 开发与运维> 正文

体验Spring的“依赖注入”的优点

简介:
1、添加一个接口
名称为UserInfoInterface,包名称为springj2seapp
在该接口中添加一个一个业务方法
   package springj2seapp;
public interface UserInfoInterface
{
          public  boolean doUserLogin(UserInfoVO oneUserInfo);
}
2、再添加一个实现类
类名称为UserInfoManage,包名称为springj2seapp 实现UserInfoInterface
    
     
3、再增加一个业务实体UserInfoVO
 
在该VO类中新增两个成员
private String userName;
private  String userPassWord;
最后将产生出下面的代码
package springj2seapp;
public class UserInfoVO {
       private String userName=null;
       private String userPassWord=null;   
       public UserInfoVO()
       {
              super();
              // TODO 自动生成构造函数存根
       }
       public String getUserName() {
              return userName;
       }
       public void setUserName(String userName) {
              this.userName = userName;
       }
       public String getUserPassWord() {
              return userPassWord;
       }
       public void setUserPassWord(String userPassWord) {
              this.userPassWord = userPassWord;
       }
}
4、编程前面的UserInfoManage类实现功能
package springj2seapp;
public class UserInfoManage implements UserInfoInterface {
 
       public UserInfoManage()
       {
 
              super();
              // TODO 自动生成构造函数存根
       }
       public  boolean doUserLogin(UserInfo oneUserInfo)
       {
              String userName=oneUserInfo.getUserName();
              String userUserPassWord=oneUserInfo.getUserPassWord();
             
//下面的代码实际应该改变为对数据库的访问          
              boolean okOrNot=userName.equals("yang")&&userUserPassWord.equals("1234");
              return okOrNot;
       }
}
5、再添加一个测试的应用主类UserManageTest
 
编程该类
package springj2seapp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class UserManageTest     //该类模拟B/S中的控制层组件(如Servlet或者Action类)
{
       public UserManageTest()
       {
              super();
              ApplicationContext ctx=new   FileSystemXmlApplicationContext("SpringApplication.xml");   
              UserInfoInterface oneUserInfoManage = (UserInfoInterface) ctx.getBean("oneUserInfoManage");
             
//下面的代码模拟用户的表单        
              UserInfoVO oneUserInfo=new UserInfoVO();
              oneUserInfo.setUserName("yang");
              oneUserInfo.setUserPassWord("1234");
             
              boolean returnResult=oneUserInfoManage.doUserLogin(oneUserInfo);
              if(returnResult)
              {
                     System.out.println("您登录成功!");
              }
              else
              {
                     System.out.println("您登录失败!");
              }            
       }    
       public static void main(String[] args)
       {
              new UserManageTest();
       }
}
6、在*.xml中添加我们的业务组件的定义
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--  - Application context definition for "springapp" DispatcherServlet.  -->
<beans>
<bean id="oneUserInfoManage" class="springj2seapp.UserInfoManage">
</bean>
</beans>
7、执行该程序
将出现下面的状态
再模拟错误登录-----只需要将测试类中的参数为错误的参数
将出现下面的错误提示
8、再修改*.xml文件以提供“属性和构造”注入
<bean id="oneUserInfoManage" class="springj2seapp.UserInfoManage">
  <constructor-arg>
      <ref bean="oneUserInfo"/>
  </constructor-arg>
</bean>
 
<bean id="oneUserInfo" class="springj2seapp.UserInfoVO">
    <property name="userName">
              <value>yang </value>
       </property>
       <property name="userPassWord">
              <value>1234 </value>
       </property>
</bean>
9、修改UserInfomanage
package springj2seapp;
public class UserInfoManage implements UserInfoInterface
{
       boolean okOrNot;
       public UserInfoManage()
       {
              super();
              // TODO 自动生成构造函数存根
       }
       public UserInfoManage(UserInfoVO oneUserInfo)
       {
 
              doUserLogin(oneUserInfo);
              // TODO 自动生成构造函数存根
       }
       public  boolean doUserLogin(UserInfo oneUserInfo)
       {
              String userName=oneUserInfo.getUserName();
              String userUserPassWord=oneUserInfo.getUserPassWord();
             
//下面的代码实际应该改变为对数据库的访问        
              okOrNot=userName.equals("yang")&&userUserPassWord.equals("1234");
              return okOrNot;
       }
       public boolean getUserLogin()
       {
              return okOrNot;
       }
}
10、再修改前面的接口以增加一个新的方法定义
package springj2seapp;
public interface UserInfoInterface
{
          public  boolean doUserLogin(UserInfo oneUserInfo);
          public boolean getUserLogin();
}
11、再修改前面的测试的主类
package springj2seapp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class UserManageTest     //该类模拟B/S中的控制层组件(如Servlet或者Action类)
{
       public UserManageTest()
       {
              super();
              ApplicationContext ctx=new   FileSystemXmlApplicationContext("SpringApplication.xml");     
UserInfoInterface oneUserInfoManage = (UserInfoInterface) ctx.getBean("oneUserInfoManage");      
//下面的代码模拟用户的表单        
              boolean returnResult=oneUserInfoManage.getUserLogin();
              if(returnResult)
              {
                     System.out.println("您登录成功!");
              }
              else
              {
                     System.out.println("您登录失败!");
              }           
       }    
       public static void main(String[] args)
       {            new UserManageTest();
 
       }
}
12、再执行本程序
 
12、再体验IoC带来的灵活性
1)改变属性参数为错误的参数----模拟登录失败
2)再执行本程序-------但不需要重新编译,将出现下面的结果
13、将JDK中的标准的Java 类注入到我们的业务组件中
<bean id="oneUserInfoManage" class="springj2seapp.UserInfoManage">
  <constructor-arg>
      <ref bean="oneUserInfo"/>
  </constructor-arg>
  <constructor-arg>
       <ref bean="dateBean"/>
  </constructor-arg>
</bean>
<bean id="oneUserInfo" class="springj2seapp.UserInfo">
    <property name="userName">
              <value>yang</value>
       </property>
       <property name="userPassWord">
              <value>1234</value>
       </property>
</bean>
  <bean id="dateBean" class="java.util.Date"/>   
14、修改业务组件类的代码以使用该Date
package springj2seapp;
import java.util.Date;
public class UserInfoManage implements UserInfoInterface
{
       boolean okOrNot;
       private Date dateBean=null;     
       public UserInfoManage()
       {
              super();
              // TODO 自动生成构造函数存根
       }
       public UserInfoManage(UserInfoVO oneUserInfo,Date dateBean)
       {
              this.dateBean=dateBean;          
              doUserLogin(oneUserInfo);
              // TODO 自动生成构造函数存根
       }
       public  boolean doUserLogin(UserInfo oneUserInfo)
       {
              String userName=oneUserInfo.getUserName();
              String userUserPassWord=oneUserInfo.getUserPassWord();
             
//下面的代码实际应该改变为对数据库的访问        
              okOrNot=userName.equals("yang")&&userUserPassWord.equals("1234");
              String loginTime=dateBean.toLocaleString();
              System.out.println("您的登录时间是:"+loginTime);
              return okOrNot;
       }
       public boolean getUserLogin()
       {
              return okOrNot;
       }
      
}
15、再执行该程序










本文转自 weijie@java 51CTO博客,原文链接:http://blog.51cto.com/weijie/66490,如需转载请自行联系原作者

版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章