Spring3.x框架的使用

作者: huhu502 | 来源:发表于2016-07-13 20:09 被阅读269次

Spring的优缺点

优点:
1:开发人员可以只关注整个结构中的其中某一层;
2:可以很容易的用新的实现来替换原有层次的实现;
3:可以降低层与层之间的依赖;
4:有利于标准化;
5:利于各层逻辑的复用。
6:结构更加的明确
7:在后期维护的时候,极大地降低了维护成本和维护时间
缺点:
1:降低了系统的性能。这是不言而喻的。如果不采用分层式结构,很多业务可以直接造访数据库,以此获取相应的数据,如今却必须通过中间层来完成。
2:有时会导致级联的修改。这种修改尤其体现在自上而下的方向。如果在表示层中需要增加一个功能,为保证其设计符合分层式结构,可能需要在相应的业务逻辑层和数据访问层中都增加相应的代码。

在项目下导入jar包和schema文件夹

schema文件夹
spring-beans-3.2.xsd
spring-context-3.2.xsd

在src下面导入bean.xml

bean.xml的名字并没有固定,只要是xml文件就可以了。

配置bean.xml

要写Student实体类,这个类里要包括无参的构造函数。

    <bean name="stu" class="com.huihui.ioctest.Student" scope="prototype"> 
    <property name="name"> <value>tom</value> </property> </bean>

测试配置是否正确,只需要写test类里面有main方法就可以了。

    ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
    Student stu=(Student) ac.getBean("stu");
    System.out.println(stu);

例子1

1,构建包com.briup.bean在包下构建Address类,属性如下:

   private String city;
   private String street;
   private String country;

在包下构建Person类,属性如下:

 private int sNo;
 private String name;
 private boolean gender;
 private int age;
 private Address address;

提供get和set方法,重写toString方法,输出当前对象字符串表达形式

2,使用Set方式完成一下要求:
完成基本类型以及对象类型的装配操作
1)构建包com.briup.ioc.set
2)在包下构建set.xml
配置文件使用set方式注入完成Person类中Address属性和其他基本属性的注入
3)完成之后,使用bean设置Student的别名并修改相关文件测试

set.xml

 <bean name="Address" class="com.huihui.bean.Address">
    <property name="city"> <value>city</value> </property>
    <property name="street"> <value>street</value> </property>
    <property name="country"> <value>country</value> </property>
</bean>  
<alias name="Address" alias="a" />     
<bean name="Person" class="com.huihui.bean.Person">
    <property name="address" ref="a"></property>
    <property name="sNo"> <value>111111</value> </property>
    <property name="name"> <value>sNoname</value> </property>
    <property name="gender"> <value>true</value> </property>
    <property name="age"> <value>12</value> </property>
</bean>

Test

String path = "com/huihui/ioc/set/set.xml";
    ApplicationContext  ac =new ClassPathXmlApplicationContext(path);
    Person sb = (Person) ac.getBean("Person");
    System.out.println(sb);
    System.out.println(sb.getAddress());

例子2

集合的装配操作
1)在com.briup.bean中构建结合类CollBean,属性如下,并提供get/set方法

   private List<String> lists;
   private String[] arrays;
   private Set<String> sets;
   private Map<Integer, String> maps;
   private Properties pros;

2)构建包com.briup.ioc.coll
3)在包下构建coll.xml配置文件完成各种集合属性的注入

coll.xml

    <bean name="CollBean" class="com.huihui.bean.CollBean">
    <property name="lists">
         <list>
            <value>list1</value>
            <value>list2</value>
            <value>list3</value>
         </list>
    </property>
    <property name="sets">
         <set>
            <value>set1</value>
            <value>set1</value>
            <value>set3</value>
         </set>
    </property>
    <property name="maps">
         <map>
            <entry key="1">
              <value>value1</value>
            </entry>
            <entry key="2">
              <value>value2</value>
            </entry>
         </map>
    </property>
    <property name="pros">
         <props>
           <prop key="key1">prop1</prop>
          <prop key="key2">prop2</prop>
          <prop key="key3">prop3</prop>
         </props>
    </property>
</bean>  

Test

   String path = "com/huihui/ioc/coll/coll.xml";
    ApplicationContext  ac =new ClassPathXmlApplicationContext(path);
    CollBean sb = (CollBean) ac.getBean("CollBean");
    sb.printInfo();

CollBean

   public void printInfo(){
    System.out.println("sets");
    System.out.println(sets);
    Set proSet=pros.entrySet();
    Iterator it=proSet.iterator();
    while(it.hasNext()){
        Map.Entry entry1=(Entry) it.next();
        System.out.println("pros key"+entry1.getKey());
        System.out.println("pros value"+entry1.getValue());
    }
}

构造器注入

<bean name="DateBean" class="com.huihui.bean.DateBean">
    <constructor-arg type="int" value="25"></constructor-arg>
    <constructor-arg type="int" value="2"></constructor-arg>
    <constructor-arg type="int" value="100"></constructor-arg>
</bean>

第二种方式

   <constructor-arg index="2">
        <value>30</value>
     </constructor-arg>
     <constructor-arg index="0">
        <value>200</value>
     </constructor-arg>
     <constructor-arg index="1">
        <value>lily</value>
     </constructor-arg>

相关文章

  • Spring3.x框架的使用

    Spring的优缺点 优点:1:开发人员可以只关注整个结构中的其中某一层;2:可以很容易的用新的实现来替换原有层次...

  • 2018-09-27

    SpringMVC简介 表示层框架 SpringMVC本身是Spring3.x表示层的一部分, SpringMVC...

  • 2018-09-25

    SpringMVC简介 表示层框架SpringMVC本身是Spring3.x表示层的一部分,SpringMVC不强...

  • Spring 整体架构

    先来看spring3.x和spring4.x的整体架构图: 首先我们看到Spring框架被分成7部分:Core C...

  • Spring mvc 报错java.lang.ClassNotF

    这个问题一般出现在使用Spring 4时,因为: spring3.x是org.springframework.ht...

  • MyBatis3.x和Spring3.x的整合

    title: MyBatis3.x和Spring3.x的整合tags: MyBatiscategories: My...

  • 使用UI框架和不使用UI框架情况的区别有哪些

    使用UI框架和不使用UI框架的区别?很多朋友想这个问题很简单,使用UI框架就能够快速完成项目开发,不使用UI框架就...

  • WebView优化协议

    使用框架 使用大鬼头的JsBridge框架使用之前需要仔细阅读框架的规则 使用的data参数规则如下 例如 返回结...

  • Android Aidl 的使用

    Android Aidl 的使用 Binder框架 -- android AIDL 的使用 Binder框架 – ...

  • CoreLocation框架的使用

    CoreLocation框架的使用 CoreLocation框架使用前提导入框架(Xcode5.0之后可以省略)C...

网友评论

    本文标题:Spring3.x框架的使用

    本文链接:https://www.haomeiwen.com/subject/ogxejttx.html