美文网首页
基于Idea的SpringDemo

基于Idea的SpringDemo

作者: Retree | 来源:发表于2019-01-24 16:46 被阅读0次

    快速创建一个spring项目

    新建一个工程,选择spring,勾选“Create empty spring-config.xml”,Libraries选择Download会自动下载所需包。

    image.png
    image.png

    创建HelloSpring

    package com.Re;
    
    public class HelloSpring {
        private String name;
    
        public void setName(String name){
            this.name=name;
        }
    
        public void printHello(){
            System.out.println("Spring say Hello"+name);
        }
    }
    

    编辑spring-config.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--指定文件路径-->
        <bean id="helloBean" class="com.Re.HelloSpring">
            <!--通过property给参数赋值 -->
            <property name="name" value="Re"/>
        </bean>
    </beans>
    

    创建一个测试类

    package com.Re;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class HelloSpringTest {
        public static void main(String[] args) {
            //以classpath路径下的"spring-config.xml"创建ApplicationContext实例
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            //在bean工厂获取id为"helloBean"的类
            HelloSpring helloSpring = (HelloSpring) context.getBean("helloBean");
            //调用方法,bean工厂会将参数赋值给方法
            helloSpring.printHello();
        }
    }
    

    项目结构及结果

    image.png

    自动装配Bean

    创建Person类

    package com.Re.autowiredDemo;
    
    public class Person {
        private String hei;
    
        public String getHei() {
            return hei;
        }
    
        public void setHei(String hei) {
            this.hei = hei;
        }
        
        @Override
        public String toString() {
            return "Person{" +
                    "hei='" + hei + '\'' +
                    '}';
        }
    }
    
    

    创建Customer类

    package com.Re.autowiredDemo;
    
    public class Customer {
    
        private Person person;
        //无参构造
        public Customer() {
        }
        
        //带参构造
        public Customer(Person person) {
            this.person = person;
        }
    
        public void setPerson(Person person) {
            this.person = person;
        }
    
    
    }
    
    

    创建测试类Test.java

    package com.Re.autowiredDemo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            Customer customer = (Customer) context.getBean("customer");
            System.out.println(customer);
        }
    }
    

    第一种:默认的ref

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--指定文件路径-->
        <bean id="helloBean" class="com.Re.beanDemo.HelloSpring">
            <!--通过property给参数赋值 -->
            <property name="name" value="Re"/>
        </bean>
    
        <!--默认方式装配,有两个bean:customer和person
            property name="person"指向Customer.setPerson方法
            ref="person"指向"<bean id="person" class="com.Re.autowiredDemo.Person"/>"-->
        <bean id="customer" class="com.Re.autowiredDemo.Customer">
            <property name="person" ref="person"/>
        </bean>
    
        <bean id="person" class="com.Re.autowiredDemo.Person">
            <!--将“hello”赋值给参数“hei”-->
            <property name="hei" value="hello"/>
        </bean>
    </beans>
    

    运行Test.java,验证自动装配成功


    image.png

    流程猜想
    执行: ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    此时根据路径找到配置文件,创建了一个上下文对象
    执行: Customer customer = (Customer) context.getBean("customer");

    • 此时通过对象获取配置文件里【id=customer】的配置,
    • 根据配置自动生成Person对象(property name="person"的作用)
    • 通过 ref="person"连接到【id=person】的配置,
    • 通过【property name="hei" value="hello"】给Person类里的hei变量赋值“hello”
    • 然后通过Person.java里重写的toString获得返回值“Person{hei='hello'}”
    • 然后回传给Customer里的person变量
    • 然后通过Customer.java里重写的toString获得返回值“Customer{person=Person{hei='hello'}}”

    第二种:byName

    如果一个bean的名称与其他bean属性的名称是一样的,那么将自动装配它。例子:Customer.java 增加一个“address”属性,那么Spring会在配置文件里寻找【id=address】的bean,如果找到了就自动装配,如果没找到,就什么都不做

    修改Customer

    package com.Re.autowiredDemo;
    
    public class Customer {
    
        //新增一个adress参数
        private Address address;
    
        private Person person;
    
    
        //无参构造
        public Customer() {
        }
    
        //带参构造
        public Customer(Person person) {
            this.person = person;
        }
    
        public Customer(Address address) {
            this.address = address;
        }
    
        public void setPerson(Person person) {
            this.person = person;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "Customer{" +
                    "address=" + address +
                    ", person=" + person +
                    '}';
        }
    }
    
    
    

    新建Address.java

    package com.Re.autowiredDemo;
    
    public class Address {
        private String myaddress;
    
        public void setMyaddress(String myaddress) {
            this.myaddress = myaddress;
        }
    
        @Override
        public String toString() {
            return "Address{" +
                    "myaddress='" + myaddress + '\'' +
                    '}';
        }
    }
    
    

    修改配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--指定文件路径-->
        <bean id="helloBean" class="com.Re.beanDemo.HelloSpring">
            <!--通过property给参数赋值 -->
            <property name="name" value="Re"/>
        </bean>
    
        <!--&lt;!&ndash;默认方式装配,有两个bean:customer和person-->
            <!--property name="person"指向Customer.setPerson方法-->
            <!--ref="person"指向"<bean id="person" class="com.Re.autowiredDemo.Person"/>"&ndash;&gt;-->
        <!--<bean id="customer" class="com.Re.autowiredDemo.Customer">-->
            <!--<property name="person" ref="person"/>-->
        <!--</bean>-->
    
        <!--<bean id="person" class="com.Re.autowiredDemo.Person">-->
            <!--&lt;!&ndash;将“hello”赋值给参数“hei”&ndash;&gt;-->
            <!--<property name="hei" value="hello"/>-->
        <!--</bean>-->
    
        <!--byName方式自动装配-->
        <bean id="customer" class="com.Re.autowiredDemo.Customer" autowire="byName"/>
    
        <bean id="address" class="com.Re.autowiredDemo.Address">
            <property name="myaddress" value="杭州西湖"/>
        </bean>
    
    
    </beans>
    
    

    运行测试Test.java


    image.png

    修改配置文件,将Address的id改一下,造成与Customer属性不一致的情况,测试验证找不到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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--指定文件路径-->
        <bean id="helloBean" class="com.Re.beanDemo.HelloSpring">
            <!--通过property给参数赋值 -->
            <property name="name" value="Re"/>
        </bean>
    
        <!--&lt;!&ndash;默认方式装配,有两个bean:customer和person-->
            <!--property name="person"指向Customer.setPerson方法-->
            <!--ref="person"指向"<bean id="person" class="com.Re.autowiredDemo.Person"/>"&ndash;&gt;-->
        <!--<bean id="customer" class="com.Re.autowiredDemo.Customer">-->
            <!--<property name="person" ref="person"/>-->
        <!--</bean>-->
    
        <!--<bean id="person" class="com.Re.autowiredDemo.Person">-->
            <!--&lt;!&ndash;将“hello”赋值给参数“hei”&ndash;&gt;-->
            <!--<property name="hei" value="hello"/>-->
        <!--</bean>-->
    
        <!--byName方式自动装配-->
        <bean id="customer" class="com.Re.autowiredDemo.Customer" autowire="byName"/>
    
        <bean id="address2" class="com.Re.autowiredDemo.Address">
            <property name="myaddress" value="杭州西湖"/>
        </bean>
    
    
    </beans>
    

    运行测试Test.java,address=null


    image.png

    第三种:byType

    类型自动装配,通过判断一个bean属性的数据类型与其他bean的数据类型相同,自动装配
    拿上面的例子说,Customer类里的属性address的类型是“Address”,与“Address” bean中的数据类型是一样的,所以Spring会通过setter方法自动装配 setAddress(Address address)

    修改配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--指定文件路径-->
        <bean id="helloBean" class="com.Re.beanDemo.HelloSpring">
            <!--通过property给参数赋值 -->
            <property name="name" value="Re"/>
        </bean>
    
        <!--&lt;!&ndash;默认方式装配,有两个bean:customer和person-->
            <!--property name="person"指向Customer.setPerson方法-->
            <!--ref="person"指向"<bean id="person" class="com.Re.autowiredDemo.Person"/>"&ndash;&gt;-->
        <!--<bean id="customer" class="com.Re.autowiredDemo.Customer">-->
            <!--<property name="person" ref="person"/>-->
        <!--</bean>-->
    
        <!--<bean id="person" class="com.Re.autowiredDemo.Person">-->
            <!--&lt;!&ndash;将“hello”赋值给参数“hei”&ndash;&gt;-->
            <!--<property name="hei" value="hello"/>-->
        <!--</bean>-->
    
        <!--&lt;!&ndash;byName方式自动装配&ndash;&gt;-->
        <!--<bean id="customer" class="com.Re.autowiredDemo.Customer" autowire="byName"/>-->
    
        <!--<bean id="address2" class="com.Re.autowiredDemo.Address">-->
            <!--<property name="myaddress" value="杭州西湖"/>-->
        <!--</bean>-->
    
    
        <!--通过类型自动装配-->
        <bean id="customer" class="com.Re.autowiredDemo.Customer" autowire="byType"/>
        <bean id="address2" class="com.Re.autowiredDemo.Address">
            <property name="myaddress" value="杭州余杭"/>
        </bean>
    
    
    
    </beans>
    

    运行测试Test.java


    image.png

    如果配置文件里创建了两个一样类型的bean,就会装配失败,程序不知道要选择哪个。
    所以,如果使用按类型自动装配,那么要确保bean只有唯一一个数据类型声明。


    image.png

    参考https://www.yiibai.com/spring/spring-auto-wiring-beans-in-xml.html

    相关文章

      网友评论

          本文标题:基于Idea的SpringDemo

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