spring入门案例
?幾個細節(jié):
?
* 1)、ApplicationContext(IOC容器的接口)
* 2)、給容器中注冊一個組件;我們也從容器中按照id拿到了這個組件的對象?
*???? 組件的創(chuàng)建工作,是容器完成;
*???? Person對象是什么時候創(chuàng)建好了呢?
*???? 容器中對象的創(chuàng)建在容器創(chuàng)建完成的時候就已經創(chuàng)建好了;
* 3)、同一個組件在ioc容器中是單實例的;容器啟動完成都已經創(chuàng)建準備好的;
* 4)、容器中如果沒有這個組件,獲取組件?報異常
* org.springframework.beans.factory.NoSuchBeanDefinitionException:
* No bean named 'person03' is defined
* 5)、ioc容器在創(chuàng)建這個組件對象的時候,(property)會利用setter方法為javaBean的屬性進行賦值;
* 6)、javaBean的屬性名是由什么決定的?getter/setter方法是屬性名;set去掉后面那一串首字母小寫就是屬性名;
*???? private String lastName;?
*???? 所有getter/setter都自動生成!
寫配置
? ? ?spring的配置文件中,集合了spring的ioc容器管理的所有組件(會員清單);
? ? ?創(chuàng)建一個Spring Bean Configuration File(Spring的bean配置文件);
?一個Bean標簽可以注冊一個組件(對象、類)?
????class:寫要注冊的組件的全類名
????id:這個對象的唯一標示;
Person.java
package com.atguigu.bean;public class Person {private String lastName;private Integer age;private String gender;private String email;public Person() {System.out.println("person創(chuàng)建了...");}public String getLastName() {return lastName;}public void setLastName(String lastName) {System.out.println("setLastName..."+lastName);this.lastName = lastName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String toString() {return "Person [lastName=" + lastName + ", age=" + age + ", gender="+ gender + ", email=" + email + "]";}}ioc.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"><!-- 注冊一個Person對象,Spring會自動創(chuàng)建這個Person對象 --><!--一個Bean標簽可以注冊一個組件(對象、類) class:寫要注冊的組件的全類名id:這個對象的唯一標示;--><bean id="person01" class="com.atguigu.bean.Person"><!--使用property標簽為Person對象的屬性賦值 name="lastName":指定屬性名value="張三":為這個屬性賦值--><property name="lastName" value="張三"></property><property name="age" value="18"></property><property name="email" value="zhangsan@atguigu.com"></property><property name="gender" value="男"></property></bean><bean id="person02" class="com.atguigu.bean.Person"><property name="lastName" value="小花"></property></bean></beans>IOCTest.java
package com.atguigu.test;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atguigu.bean.Person;public class IOCTest {/*** 從容器中拿到這個組件*/@Testpublic void test() {//ApplicationContext:代表ioc容器//ClassPathXmlApplicationContext:當前應用的xml配置文件在 ClassPath下//根據spring的配置文件得到ioc容器對象//ApplicationContext ioc = new ClassPathXmlApplicationContext("com/atguigu/bean/ioc.xml");ApplicationContext ioc = new ClassPathXmlApplicationContext("com/atguigu/bean/ioc.xml");//容器幫我們創(chuàng)建好對象了; // System.out.println("容器啟動完成....");Person bean = (Person) ioc.getBean("person01");System.out.println(bean);Object bean2 = ioc.getBean("person01");System.out.println(bean == bean2); //System.out.println("=================="); // Object bean3 = ioc.getBean("person03");}} /*** 存在的幾個問題;* 1)、src,源碼包開始的路徑,稱為類路徑的開始;* 所有源碼包里面的東西都會被合并放在類路徑里面;* java:/bin/* web:/WEB-INF/classes/* 2)、導包commons-logging-1.1.3.jar(依賴)* 3)、先導包再創(chuàng)建配置文件;* 4)、Spring的容器接管了標志了s的類;*/ /*** 幾個細節(jié):* 1)、ApplicationContext(IOC容器的接口)* 2)、給容器中注冊一個組件;我們也從容器中按照id拿到了這個組件的對象?* 組件的創(chuàng)建工作,是容器完成;* Person對象是什么時候創(chuàng)建好了呢?* 容器中對象的創(chuàng)建在容器創(chuàng)建完成的時候就已經創(chuàng)建好了;* 3)、同一個組件在ioc容器中是單實例的;容器啟動完成都已經創(chuàng)建準備好的;* 4)、容器中如果沒有這個組件,獲取組件?報異常* org.springframework.beans.factory.NoSuchBeanDefinitionException: * No bean named 'person03' is defined* 5)、ioc容器在創(chuàng)建這個組件對象的時候,(property)會利用setter方法為javaBean的屬性進行賦值;* 6)、javaBean的屬性名是由什么決定的?getter/setter方法是屬性名;set去掉后面那一串首字母小寫就是屬性名;* private String lastName;?* 所有getter/setter都自動生成!*/總結
以上是生活随笔為你收集整理的spring入门案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 隔行换色案例||全选和全不选||QQ表情
- 下一篇: 实验4:正确的为各种属性赋值||实验7: