Spring MVC學習入門筆記
阿新 • • 發佈:2018-01-15
分享圖片 快捷 png epo 為什麽 學習 -c ava 報錯
使用Spring步驟
- 添加依賴
- 創建xml文件
- 配置(需要被管理的類)bean
- 實例化上下文類
- GetBean對象
使用idea新建一個maven項目,在pom.xml中添加依賴:
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.13.RELEASE</version> </dependency> </dependencies>
創建一個實體,Student.java:
package entity; public class Student { public void hello(){ System.out.println("hello SPring MVC!"); } }
選中項目右鍵--創建一個Spring Config:
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="stu" class="entity.Student"></bean> </beans>
在實體包下寫一個Main.java類用於測試:
package entity; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //超類ApplicationContext(是一個接口)--所以需要new子類 //ClassPath類路徑==和代碼放在一起的,同在main文件夾下面 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springMVC.xml"); //Ctrl+H (快捷鍵)-->可看類型的層次 Student student = (Student) applicationContext.getBean("stu"); //xml的id名 student.hello(); }
項目目錄結構:
問題:
答:分隔符之間可以混用,分別可以使用,;(空格)三種符號作為分隔符,而:則會報錯,測試代碼如下:
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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="stu" name="student s,st" class="entity.Student"></bean> <!--可以混用--> <!--<bean id="stu" name="student,s" class="entity.Student"></bean> <bean id="stu" name="student;s" class="entity.Student"></bean>--> </beans>
Main.java測試的代碼:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springMVC.xml"); //Ctrl+H -->類型的層次 xsd /*Student student = (Student) applicationContext.getBean("stu"); student.hello(); Student student1 = (Student) applicationContext.getBean("student"); student1.hello(); Student student2 = (Student) applicationContext.getBean("s"); student2.hello(); Student student3 = (Student) applicationContext.getBean("st"); student3.hello();*/
<!--創建對象有三種形式:1.直接new;2.間接使用工廠類new-為什麽用:;3.實現接口-->
第一種:即上邊提到的一種
第二種:
實體代碼:
package entity; public class Student { public static Student hello(){ System.out.println("hello SPring MVC!"); return new Student(); } }
xml代碼:
<bean id="xx" class="entity.Student" factory-method="hello"></bean>
Main測試代碼:
Student student4 = (Student) applicationContext.getBean("xx");
第三種:
實體代碼:
public Student hello (){ System.out.println("hello SPring MVC!"); return new Student(); }
xml代碼(方法一):
<bean id="factory" class="entity.Student"></bean> <bean id="yy" class="entity.Student" factory-bean="factory" factory-method="hello"></bean>
xml代碼(方法二):
<!--xml中能否寫自己:不可以?別名呢?可以-->
<bean id="factory" class="entity.Student" name="student s"></bean> <bean id="yy" class="entity.Student" factory-bean="s" factory-method="hello"></bean>
Main測試代碼:
Student student5 = (Student) applicationContext.getBean("yy");
<!--xml配置中能否去id,怎麽寫-->
實體代碼:
public static Student hello(){ System.out.println("hello SPring MVC!"); return new Student(); }
xml代碼:
<bean class="entity.Student"></bean>
Main測試代碼:
Student student6 = applicationContext.getBean(Student.class); student6.hello();
Spring MVC學習入門筆記