欢迎光临散文网 会员登陆 & 注册

SPRING框架简介和构造器注入作业和个人答案,在Spring容器中调用构造方法【诗书画唱】

2021-05-07 15:20 作者:诗书画唱  | 我要投稿


目录:

个人笔记

讲义

SPRING框架简介和构造器注入的作业和个人给出的代码答案

1、创建一个Dog类,包含一个叫的方法,将一个dog对象放到spring容器中,在main方法中不使用new关键字调用叫的方法。

2、创建一个Product类,包含一个显示商品信息的方法,将一个product对象放到spring容器中,在servlet中不使用new关键字调用显示商品信息的方法。

3、创建一个Person类和斧头类Axe,Person类中有一个使用工具的方法,请你使用斧头砍树,斧头类中有一个砍树的方法,请你在使用工具的方法中调用斧头的砍树的方法。必须使用Spring容器,并且有一个构造方法,这个构造方法包含有一个斧头参数。

public class Person{

    private String name;

    private Axe axe;

    //在Spring容器中调用这个构造方法

    public Person(String name,Axe axe) {

    }

}

4、创建一个Car类和轮子类Wheel,Car类中有一个run方法,轮子有一个滚roll的方法,创建一个包含有轮子参数的构造方法,在Spring容器中调用这个构造方法。



个人笔记 START

个人尝试的经验:在实体类中声明了多少个构造方法和别的类作为属性,就在配置文件要都对应的配置,少了一个配置都会报错

applicationContext.xml中写bean等的配置,在实体类中声明了几个private等的属性,在bean中包裹的constructor-arg都要对应的有几对。

applicationContext.xml中写bean等的配置,在实体类中声明了几个private等的属性,在bean中包裹的constructor-arg都要对应的有几对。

个人笔记 END

讲义  START

1.      Spring AOP  面向切面编程思想
2.      Spring ORM  Hibernate|mybatis|JDO
3.      Spring Core  提供bean工厂 IOC
4.      Spring Dao  JDBC支持
5.      Spring Context  提供了关于UI支持,邮件支持等
6.      Spring Web 提供了web的一些工具类的支持
7.      Spring MVC  提供了web mvc , webviews , jsp ,pdf ,export

"STRUTS2框架:升级servlet为action,javaweb框架,getParameter和getAttribute和
setAttribute方法都被框架隐藏起来了。"
"MYBATIS框架:升级DAO,通用框架,以后在javaweb项目中,不需要写Dao类了,直接通过映射文件
实现SQL语句。"
SPRING框架:通用框架,粘合其他的两个框架的。
SPRINGBOOT,SPRINGClOUD,JBPM工作流
SPRING的两大核心:
AOP:面向切面
OOP:面向对象
IOC:控制反转(DI:依赖注入)

SPRINGCORE:框架
EJB:企业级的JavaBean,非常笨重

项目会针对接口编程:dao,service类都会有一个接口

"解耦:在编写程序时,要让类和类之间具备一定的关系方便传递消息,也要让类和类之间具备有一
定的独立性,这样能保证当你修改一个类中的BUG,不会导致其他类出现新的BUG"

AOP:比较难理解

在项目中使用spring框架:
main方法运行的项目:
1、导包
2、导入配置文件
3、编程测试

spring框架的核心就是spring容器,所以在写代码的时候要想办法获取spring容器

"控制反转的意思就是当我们需要调用一个类中的方法时,尽量不要自己去new出这个类,而是直接问
spring容器要这个类就可以了。"

DI:构造器注入和setter注入


讲义 END




SPRING框架简介和构造器注入的作业和个人给出的代码答案 START


1、创建一个Dog类,包含一个叫的方法,将一个dog对象放到spring容器中,在main方法中不使用new关键字调用叫的方法。

2、创建一个Product类,包含一个显示商品信息的方法,将一个product对象放到spring容器中,在servlet中不使用new关键字调用显示商品信息的方法。

3、创建一个Person类和斧头类Axe,Person类中有一个使用工具的方法,请你使用斧头砍树,斧头类中有一个砍树的方法,请你在使用工具的方法中调用斧头的砍树的方法。必须使用Spring容器,并且有一个构造方法,这个构造方法包含有一个斧头参数。

public class Person{

    private String name;

    private Axe axe;

    //在Spring容器中调用这个构造方法

    public Person(String name,Axe axe) {

    }

}

4、创建一个Car类和轮子类Wheel,Car类中有一个run方法,轮子有一个滚roll的方法,创建一个包含有轮子参数的构造方法,在Spring容器中调用这个构造方法。



package com.SSHC.bean;

//axe:斧头

public class Axe {

    public void cut(String sth){

    System.out.println("看到这句话说明调用了"

    + "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("看到这句话说明已经成功调用"

    + "了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)方法中】");/*这句可以注释掉*/

}

/*这里传参的构造方法public Person(Axe axe)必须写,

* 不然,下面的axe.cut("使用斧头砍树");

* 是调用不了的。*/

/*axe: 斧头

perform:执行 */

    public void perform(){

    //创建斧头的权利被剥夺了,由spring容器来创建

    //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 "商品编号是:" + this.id + 

    ",商品名称是:" + this.name + 

    ",商品价格是:" + 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("看到这句话说明调用了"

+ "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="诗书画唱的商品"></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类中的构造方法   -->

   

 <!--   <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="诗书画唱"></constructor-arg>

        <constructor-arg value="22"></constructor-arg>

     <!--  <constructor-arg ref="axe"></constructor-arg> -->  

    </bean>

</beans>




单词注释:ApplicationContext就是应用上下文的意思

applicationContext.xml中写bean等的配置


运行效果:







SPRING框架简介和构造器注入的作业和个人给出的代码答案 END





SPRING框架简介和构造器注入作业和个人答案,在Spring容器中调用构造方法【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律