SPRING框架簡(jiǎn)介和構(gòu)造器注入作業(yè)和個(gè)人答案,在Spring容器中調(diào)用構(gòu)造方法【詩(shī)書畫唱】
目錄:
個(gè)人筆記
講義
SPRING框架簡(jiǎn)介和構(gòu)造器注入的作業(yè)和個(gè)人給出的代碼答案
1、創(chuàng)建一個(gè)Dog類,包含一個(gè)叫的方法,將一個(gè)dog對(duì)象放到spring容器中,在main方法中不使用new關(guān)鍵字調(diào)用叫的方法。
2、創(chuàng)建一個(gè)Product類,包含一個(gè)顯示商品信息的方法,將一個(gè)product對(duì)象放到spring容器中,在servlet中不使用new關(guān)鍵字調(diào)用顯示商品信息的方法。
3、創(chuàng)建一個(gè)Person類和斧頭類Axe,Person類中有一個(gè)使用工具的方法,請(qǐng)你使用斧頭砍樹,斧頭類中有一個(gè)砍樹的方法,請(qǐng)你在使用工具的方法中調(diào)用斧頭的砍樹的方法。必須使用Spring容器,并且有一個(gè)構(gòu)造方法,這個(gè)構(gòu)造方法包含有一個(gè)斧頭參數(shù)。
public class Person{
? ? private String name;
? ? private Axe axe;
? ? //在Spring容器中調(diào)用這個(gè)構(gòu)造方法
? ? public Person(String name,Axe axe) {
? ? }
}
4、創(chuàng)建一個(gè)Car類和輪子類Wheel,Car類中有一個(gè)run方法,輪子有一個(gè)滾roll的方法,創(chuàng)建一個(gè)包含有輪子參數(shù)的構(gòu)造方法,在Spring容器中調(diào)用這個(gè)構(gòu)造方法。

個(gè)人筆記 START


applicationContext.xml中寫bean等的配置,在實(shí)體類中聲明了幾個(gè)private等的屬性,在bean中包裹的constructor-arg都要對(duì)應(yīng)的有幾對(duì)。

個(gè)人筆記 END
講義? START

2. ? ? ?Spring ORM ?Hibernate|mybatis|JDO
3. ? ? ?Spring Core ?提供bean工廠 IOC
4. ? ? ?Spring Dao ?JDBC支持
5. ? ? ?Spring Context ?提供了關(guān)于UI支持,郵件支持等
6. ? ? ?Spring Web 提供了web的一些工具類的支持
7. ? ? ?Spring MVC ?提供了web mvc , webviews , jsp ,pdf ,export
"STRUTS2框架:升級(jí)servlet為action,javaweb框架,getParameter和getAttribute和
setAttribute方法都被框架隱藏起來了。"
"MYBATIS框架:升級(jí)DAO,通用框架,以后在javaweb項(xiàng)目中,不需要寫Dao類了,直接通過映射文件
實(shí)現(xiàn)SQL語句。"
SPRING框架:通用框架,粘合其他的兩個(gè)框架的。
SPRINGBOOT,SPRINGClOUD,JBPM工作流
SPRING的兩大核心:
AOP:面向切面
OOP:面向?qū)ο?br>IOC:控制反轉(zhuǎn)(DI:依賴注入)
SPRINGCORE:框架
EJB:企業(yè)級(jí)的JavaBean,非常笨重
項(xiàng)目會(huì)針對(duì)接口編程:dao,service類都會(huì)有一個(gè)接口
"解耦:在編寫程序時(shí),要讓類和類之間具備一定的關(guān)系方便傳遞消息,也要讓類和類之間具備有一
定的獨(dú)立性,這樣能保證當(dāng)你修改一個(gè)類中的BUG,不會(huì)導(dǎo)致其他類出現(xiàn)新的BUG"
AOP:比較難理解
在項(xiàng)目中使用spring框架:
main方法運(yùn)行的項(xiàng)目:
1、導(dǎo)包
2、導(dǎo)入配置文件
3、編程測(cè)試
spring框架的核心就是spring容器,所以在寫代碼的時(shí)候要想辦法獲取spring容器
"控制反轉(zhuǎn)的意思就是當(dāng)我們需要調(diào)用一個(gè)類中的方法時(shí),盡量不要自己去new出這個(gè)類,而是直接問
spring容器要這個(gè)類就可以了。"
DI:構(gòu)造器注入和setter注入

講義?END
SPRING框架簡(jiǎn)介和構(gòu)造器注入的作業(yè)和個(gè)人給出的代碼答案 START
1、創(chuàng)建一個(gè)Dog類,包含一個(gè)叫的方法,將一個(gè)dog對(duì)象放到spring容器中,在main方法中不使用new關(guān)鍵字調(diào)用叫的方法。
2、創(chuàng)建一個(gè)Product類,包含一個(gè)顯示商品信息的方法,將一個(gè)product對(duì)象放到spring容器中,在servlet中不使用new關(guān)鍵字調(diào)用顯示商品信息的方法。
3、創(chuàng)建一個(gè)Person類和斧頭類Axe,Person類中有一個(gè)使用工具的方法,請(qǐng)你使用斧頭砍樹,斧頭類中有一個(gè)砍樹的方法,請(qǐng)你在使用工具的方法中調(diào)用斧頭的砍樹的方法。必須使用Spring容器,并且有一個(gè)構(gòu)造方法,這個(gè)構(gòu)造方法包含有一個(gè)斧頭參數(shù)。
public class Person{
? ? private String name;
? ? private Axe axe;
? ? //在Spring容器中調(diào)用這個(gè)構(gòu)造方法
? ? public Person(String name,Axe axe) {
? ? }
}
4、創(chuàng)建一個(gè)Car類和輪子類Wheel,Car類中有一個(gè)run方法,輪子有一個(gè)滾roll的方法,創(chuàng)建一個(gè)包含有輪子參數(shù)的構(gòu)造方法,在Spring容器中調(diào)用這個(gè)構(gòu)造方法。

package com.SSHC.bean;
//axe:斧頭
public class Axe {
? ? public void cut(String sth){
? ? System.out.println("看到這句話說明調(diào)用了"
? ? + "Axe類中的cut(String sth):" + sth);
? ? }
}


package com.SSHC.bean;
public class Car {
//private String name;
private Wheel Wheel;
public Car(Wheel Wheel) {
//Wheel.roll();
this.Wheel=Wheel;
}
public void run(){
Wheel.roll();
}
}


package com.SSHC.bean;
public class Dog {
? ? public void call(){
? ? System.out.println("看到這句話說明已經(jīng)成功調(diào)用"
? ? + "了Dog類中的call方法");
? ? }
}


package com.SSHC.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Person {
private Axe axe;
public Person(Axe axe){
this.axe = axe;
//axe.cut("砍樹【Person(Axe m)方法中】");/*這句可以注釋掉*/
}
/*這里傳參的構(gòu)造方法public Person(Axe axe)必須寫,
* 不然,下面的axe.cut("使用斧頭砍樹");
* 是調(diào)用不了的。*/
/*axe: 斧頭
perform:執(zhí)行 */
? ? public void perform(){
? ? //創(chuàng)建斧頭的權(quán)利被剝奪了,由spring容器來創(chuàng)建
? ? //Axe axe = new Axe();
? ? axe.cut("使用斧頭砍樹");
? ? }
}


package com.SSHC.bean;
public class Product {
private Integer id;
? ? private String name;
? ? private Double price;
public Product(Integer id,String name,Double price){
? ? this.id = id;
? ? this.name = name;
? ? this.price =price;
? ? }
? ??
? ? @Override
? ? public String toString() {
?
? ? return "商品編號(hào)是:" + this.id +?
? ? ",商品名稱是:" + this.name +?
? ? ",商品價(jià)格是:" + this.price;
? ? }
}


package com.SSHC.bean;
public class Student {
? ? private String name;
? ? private Integer age;
//? ? private Axe axe;
? ??
//? ? public Student(){
//? ?
//? ? }
//? ? public Student(String name){
//? ? this.name = name;
//? ? }
? ? public Student(String name,Integer age){
? ? this.name = name;
? ? this.age = age;
? ??
? ? }
? ??
? ? @Override
? ? public String toString() {
? ? // TODO Auto-generated method stub
//? ? if(this.axe != null) {
//? ? ? ? this.axe.cut("劈柴");
//? ? }
? ? return "姓名是:" + this.name +?
? ? ",年齡是:" + this.age;
? ? }
}


package com.SSHC.bean;
public class Wheel {
public void roll(){
System.out.println("看到這句話說明調(diào)用了"
+ "Wheel類的roll方法");
}
}


package com.SSHC.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.SSHC.bean.Car;
public class CarTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new?
ClassPathXmlApplicationContext("applicationContext.xml");
//
Car o = (Car) ctx.getBean("CarBeanId");
o.run();
System.out.println(o);
// Car p = (Car)ctx.getBean("p");
// p.perform();
}
}


package com.SSHC.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.SSHC.bean.Dog;
public class DogTest {
public static void main(String[] args) {
ApplicationContext ctx = new?
ClassPathXmlApplicationContext("applicationContext.xml");
Dog a = (Dog) ctx.getBean("DogBeanId");
a.call();
}
}


package com.SSHC.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.SSHC.bean.Person;
public class PersonTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new?
ClassPathXmlApplicationContext("applicationContext.xml");
//
Person o = (Person) ctx.getBean("PersonBeanId");
o.perform();
System.out.println(o);
// Person p = (Person)ctx.getBean("p");
// p.perform();
}
}


package com.SSHC.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.SSHC.bean.Product;
public class ProductTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new?
ClassPathXmlApplicationContext("applicationContext.xml");
Product o = (Product) ctx.getBean("ProductBeanId");
System.out.println(o);
}
}


package com.SSHC.Test;
import java.util.Scanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.SSHC.bean.Student;
public class StudentTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Student s3 = (Student) ctx.getBean("s3");
System.out.println(s3);
}
}


<?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:context="http://www.springframework.org/schema/context"
? ? xmlns:mvc="http://www.springframework.org/schema/mvc"
? ? xmlns:tx="http://www.springframework.org/schema/tx"
? ? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
? ? ? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
? ? ? ? http://www.springframework.org/schema/tx?
? ? ? ? http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
? ? <bean id="DogBeanId" class="com.SSHC.bean.Dog"></bean>
? ??
? ? <bean id="ProductBeanId" class="com.SSHC.bean.Product">
? ? ?<constructor-arg value="6"></constructor-arg>
? ? ? ? <constructor-arg value="詩(shī)書畫唱的商品"></constructor-arg>
? ? ? ? <constructor-arg value="666.666"></constructor-arg>
? ? ? ?<!--? ?<constructor-arg ref="axe"></constructor-arg>-->
? ? </bean>
? ??
?
? ?<!--? <bean id="axe" class="com.SSHC.bean.Axe"></bean>
? ? <bean id="p" class="com.SSHC.bean.Person">
? ? ? ? <constructor-arg ref="axe"></constructor-arg>
? ? </bean> -->??
?<bean id="AxeBeanId" class="com.SSHC.bean.Axe"></bean>
? ? <bean id="PersonBeanId" class="com.SSHC.bean.Person">
? ? ? ? <constructor-arg ref="AxeBeanId"></constructor-arg>
? ? </bean>
? <bean id="WheelBeanId" class="com.SSHC.bean.Wheel"></bean>
? ? <bean id="CarBeanId" class="com.SSHC.bean.Car">
? ? ? ? <constructor-arg ref="WheelBeanId"></constructor-arg>
?</bean>
? ? <!--?
? ? <constructor-arg ref="AxeBeanId"></constructor-arg>
? ? ?表示引用Axe類中的構(gòu)造方法? ?-->
? ?
?<!--? ?<bean id="s1" class="com.SSHC.bean.Student"></bean>
? ? <bean id="s2" class="com.SSHC.bean.Student">
? ? ? ? <constructor-arg value="小明"></constructor-arg>
? ? </bean>-->
? ? <bean id="s3" class="com.SSHC.bean.Student">
? ? ? ? <constructor-arg value="詩(shī)書畫唱"></constructor-arg>
? ? ? ? <constructor-arg value="22"></constructor-arg>
? ? ?<!--? <constructor-arg ref="axe"></constructor-arg> -->??
? ? </bean>
</beans>



單詞注釋:ApplicationContext就是應(yīng)用上下文的意思
applicationContext.xml中寫bean等的配置
運(yùn)行效果:




