zoukankan      html  css  js  c++  java
  • 一、spring入门案例

    一、什么是spring呢?话不多说,来看看这个入门案例

    1. 建立java项目myspring1

    2. 在包com.beans包下建立简单javabean名叫User

    package com.beans;
    
    public class User {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        public void sayHello(){
            System.out.println("Hello "+name);
        }
    }

    3.在com.test包下建立Test.java测试

    package com.test;
    
    import com.beans.User;
    
    public class Test {
        
        public static void main(String[] args) {
            
            User user=new User();
            user.setName("蔡文姬");
            user.sayHello();
            
        }
    
    }


    4.运行结果当然是打出"Hello 蔡文姬",但是如果叫你不许用User user=new User();这样的代码建立新对象,那你打得出"Hello 蔡文姬"吗?

    5.这就要用到spring了,在引入spring开发包之后,在src目录下新建applicationContext.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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="user" class="com.beans.User">
            <property name="name" value="蔡文姬"/>
        </bean>
    </beans>

    6.此时在Test.java中,不必new一个User对象,只需要

    package com.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.beans.User;
    
    public class Test {
        
        public static void main(String[] args) {
            
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            User user=(User) ac.getBean("user");
            user.sayHello();
            
        }
    
    }


    7.显然, 该代码首先获得了spring的applicationContext.xml(容器对象),然后从里面取出了一个id叫做user的bean对象,该bean已经在xml中配置了name信息,故调用sayHello函数也能出现相同的输出结果。

    二、当然,不是所有的属性都有一个确定的值,比如下面这个案例

    1. 创建项目myspring2并导入spring开发包

    2. 新建类Dog:

    package com.service;
    
    public class Dog {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    3.新建类User:

     
    
    package com.service;
    
    
    public class User{
        private String name;
        private Dog dog;
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        public void show(){
            System.out.println(name+"有一只狗"+dog.getName());
            
        }
    }

    4.配置applicationContext.xml,此时Dog类好配置,注意User类的配置

    <?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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        
        <bean id="user" class="com.service.User">
            <property name="name" value="小明"/>
            <property name="dog" ref="dog"/>
        </bean>
        <bean id="dog" class="com.service.Dog">
            <property name="name">
                <value>旺财</value>
            </property>
        </bean>
        
    
    
    </beans>


    5.ref="dog"表示,该属性值参考于另一个bean,名叫dog

    6.此时编辑运行Test.java:

    package com.test;
    
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    
    import com.service.User;
    
    
    public class Test {
        
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            User user=(User) ac.getBean("user");
            user.show();
        }
        
    }


    7.运行结果:控制台打出 小明有一只狗旺财

    三、通过以上两个案例,我们还看不出到底spring有什么好处,Spring开发提倡接口编程,配合ioc(或者叫di),可以达到解耦的目的,举例说明:

    1. 新建项目myspring3,导入spring开发包

    2. 新建接口ChangeLetter

    package com.myz.inter;
    
    public interface ChangeLetter {
        public String change();
    }


    3. 新建类UpperLetter,实现ChangeLetter接口,将字符串全部转为大写

    package com.myz.inter;
    
    public class UpperLetter implements ChangeLetter{
        private String str;
        
        public String getStr() {
            return str;
        }
    
        public void setStr(String str) {
            this.str = str;
        }
    
        @Override
        public String change() {
            // TODO Auto-generated method stub
            return str.toUpperCase();
        }
    
    }


    4. 同理新建类LowwerLetter,将字符串全部转为小写

    package com.myz.inter;
    
    public class LowwerLetter implements ChangeLetter {
        private String str;
        
        public String getStr() {
            return str;
        }
    
        public void setStr(String str) {
            this.str = str;
        }
    
        @Override
        public String change() {
            // TODO Auto-generated method stub
            return str.toLowerCase();
        }
    }


    5.配置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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="changeLetter" class="com.myz.inter.UpperLetter">
            <property name="str" value="aBcD"/>
        </bean>
    </beans>

    6.main函数测试

    package com.myz.test;
    
    import org.springframework.context.ApplicationContext;
    
    import com.myz.inter.ChangeLetter;
    import com.util.ApplicationContextUtil;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            ChangeLetter str = (ChangeLetter) ac.getBean("changeLetter");
            System.out.println(str.change());
     }


    7.控制台输出:ABCD

    8.而我们如果我们突然想要字符串的小写了,而不是大写,仅仅需要修改一点点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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="changeLetter" class="com.myz.inter.LowwerLetter">
            <property name="str" value="aBcD"/>
        </bean>
    </beans>

    项目源码:链接:https://pan.baidu.com/s/1kVy3t7T 密码:byad

  • 相关阅读:
    A Node Influence Based Label Propagation Algorithm for Community detection in networks 文章算法实现的疑问
    Fast Newman-FN算法以及模块度定义介绍
    Label Propagation Algorithm LPA 标签传播算法解析及matlab代码实现
    设计一个smartnic
    Intel GEN11 GPU
    Intel GEN9 GPU
    Shared Virtual Memory (SVM) Functions
    connect via ssh to virtualbox guest vm without knowing ip address
    smartnic
    技术精品翻译
  • 原文地址:https://www.cnblogs.com/myz666/p/8098201.html
Copyright © 2011-2022 走看看