一、仅通过context标签来构建对象
1.一来就遇到Bug:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- <bean id="Book" class="com.Book">--> <!-- <!– collaborators and configuration for this bean go here –>--> <!-- <property name="name" value="突飞猛进"></property>--> <!-- <property name="author" value="飞天遁地"></property>--> <!-- <property name="id" value="5"></property>--> <!-- <property name="price" value="111" ></property>--> <!-- </bean>--> <!-- <bean id="Girl" class="com.Girl">--> <!-- <property name="name" value="小兔"></property>--> <!-- <property name="age" value="18"></property>--> <!-- </bean>--> <!-- <bean id="Boy" class="com.Boy">--> <!-- <property name="age" value="30"></property>--> <!-- <property name="name" value="小狗"></property>--> <!-- <property name="girl" ref="Girl"></property>--> <!-- </bean>--> <context:component-scan base-package="com.Boy" </beans> 上述代码中死活识别不到Boy这个类,原因在没有在Boy类中添加@Component,这是注解和属性注入的区别,在属性注入中不需要添加@Component。 2.再将Boy添加了@Component,搞笑的又来了,还是没办法识别Boy这个类,原因是使用注解只需要识别到类的根目录,也就是识别到com就行了,注解会自动书别下述的@Component类。(规则真多) 目前理解注解的目的就是简化属性注入的繁杂手续(后续再加深学习,可能有误)
二、创建对象对应的属性
很容易发现,上述内容我们只是创建了对象,但是如何对上述对象的相关属性进行传递?
在Spring配置文件使用了context标签后,同时我们使用@Component来标记了相关对象,此时就Spring就已经将相关对象丢到了容器中,Spring框架的容器就是用来存储一堆一堆的对象并且进行管理的。
那么我们现在想要提取容器中对象的相关属性该怎么做?
显然,我们没有在容器中传入对象的属性,所以要么我们直接在测试类将容器中的对象提取出来再对对象进行属性设置(显然这样有种脱裤子放屁的感觉),要么我们在对应的类中通过标签@Value来构建对象的属性,注意@Value标签对应的就是对象的属性,并且是在自行创建的类中进行标记的。(这与我们在配置文件中使用<property>标签的结果相同,只不过是换了一个方法,这样使得我们可以直接在我们自行定义的类中进行标记了,而不用在配置文件中写一长串<property>)
如果我们在类中的属性为另一个自建类,,比如我们创建了类A和类B,类A中的属性包含B,那么此时需要在类A中用@Autowired来标记B,这就和@Value类似,相当于@Value只能识别Java中自带的一些属性第一,而我们自创的一些属性(也就是自创的类)用@Value是识别不了的,所以需要用@Autowired来进行识别。识别的过程就是将被@Autowired的属性丢到容器中。
对应代码如下:
包含Boy类,Girl类,配置文件以及测试类
package com; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Boy { public int getAge() { return age; } public String getName() { return name; } public Girl getGirl() { return girl; } public Boy(int age, String name, Girl girl) { this.age = age; this.name = name; this.girl = girl; } @Value("30") int age; @Value("小狗") String name; @Autowired Girl girl; public Boy() { } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public void setGirl(Girl girl) { this.girl = girl; } }
package com; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Girl { @Value("18") int age; @Value("小兔") String name; public Girl(int age, String name) { this.age = age; this.name = name; } public Girl() { } public String getName() { return name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- <bean id="Book" class="com.Book">--> <!-- <!– collaborators and configuration for this bean go here –>--> <!-- <property name="name" value="突飞猛进"></property>--> <!-- <property name="author" value="飞天遁地"></property>--> <!-- <property name="id" value="5"></property>--> <!-- <property name="price" value="111" ></property>--> <!-- </bean>--> <!-- <bean id="Girl" class="com.Girl">--> <!-- <property name="name" value="小兔"></property>--> <!-- <property name="age" value="18"></property>--> <!-- </bean>--> <!-- <bean id="Boy" class="com.Boy">--> <!-- <property name="age" value="30"></property>--> <!-- <property name="name" value="小狗"></property>--> <!-- <property name="girl" ref="Girl"></property>--> <!-- </bean>--> <context:component-scan base-package="com"></context:component-scan> </beans>
import com.Boy; import com.Girl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Tets02 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); // Girl girl= context.getBean(Girl.class); // System.out.println(girl.getAge()+girl.getName()); Boy boy = context.getBean(Boy.class); System.out.println(boy.getAge()); System.out.println(boy.getName()); Girl girl = boy.getGirl(); System.out.println(girl.getName()); System.out.println(girl.getAge()); } } 显然就目前的学习认知而言,我并不感觉这样有更加简便的,害得学