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

Spring框架:构造器注入,ref,servlet或Java项目中使用,面向切面,装配【诗书画唱】

2021-03-17 15:11 作者:诗书画唱  | 我要投稿

本期的内容:

使用Spring框架来调用Student的属性和构造方法,构造器注入等等的例子


要点概况(讲义)


什么是面向切面


解耦的概念


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


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

SPRINgcORE框架简介


Spring框架的最终目的就是简化java开发。


在servlet中获取Spring容器的方法


推荐阅读


1、SPRINgcORE框架_简介.ppt 


2、SPRINgcORE框架_装配Bean.ppt 


创建一个spring框架的项目的方法 


个人对容器的理解 

个人理解:容器就是把数据放到里面,可以通过"."获取要拿出来的数据


教你理解spring框架中的配置文件中的bean标签的id等内容


在servlet中获取spring容器的方法


在main方法中获取spring容器的代码方法


一般出现了类的全路径,比如class=“XXX”的内容的话,一般就是使用了反射的技术。


教你理解loC(控制反转)


Spring中的依赖注入(DI)的方式:构造器注入,setter注入


构造器注入(也就是值注入,其中创建的类中会创建构造方法,传值)


这里的spring配置的设置,让调用构造方法等传的axe类,调用方法时,axe不会为空


ApplicationContext就是应用上下文的意思


在Java项目中使用Spring框架打印“Hello world” 

在Java Web项目中使用Spring框架打印“Hello world”


单词解释:application(应用)Context(上下文).xml:应用上下文的xml文件。

个人注释:其实”容器“思想贯彻几乎所有的编程思想中,总可以找到身为”容器“身份的对象,因为很好用。

个人感悟:其实Spring框架等最关建的就是知道如何根据需求设置配置文件等等。



使用Spring框架来调用Student的属性和构造方法,构造器注入等等的例子 START


package com.SSHC.bean;

//axe:斧头

public class Axe {

    public void cut(String sth){

    System.out.println("Axe类中的cut方法正在" + sth);

    }

}

package com.SSHC.bean;


public class HelloBean {

    public void sayHello(){

    System.out.println("Hello world");

    }

}

package com.SSHC.bean;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Person {

private Axe axe;

public Person(Axe m){

this.axe = m;

axe.cut("砍树【Person(Axe m)方法中】");/*这句可以注释掉*/

}

/*axe: 斧头

perform:执行 */

    public void perform(){

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

    //Axe axe = new Axe();

    axe.cut("砍树【perform()方法中】");

    }

}

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,Axe m){

    this.name = name;

    this.age = age;

    this.axe = m;

    }

    

    @Override

    public String toString() {

    // TODO Auto-generated method stub

    if(this.axe != null) {

        this.axe.cut("劈柴");

    }

    return "姓名是:" + this.name + ",年龄是:" + this.age;

    }

}

/**CTRL+F:

 * 使用ApplicationContext获取自己命名为ctx的Spring容器

 * */



package com.SSHC.Text;

import java.util.Scanner;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.SSHC.bean.Axe;

import com.SSHC.bean.HelloBean;

import com.SSHC.bean.Person;

import com.SSHC.bean.Student;



public class Test {


public static void main(String[] args) {

// TODO Auto-generated method stub



/*使用ApplicationContext获取自己命名为ctx的Spring容器 START

 

 *每次调用Spring容器的时候就会调用Axe类中声明的cut方法

 *(Axe类中的cut方法)。

 */



ApplicationContext ctx = new 

ClassPathXmlApplicationContext("applicationContext.xml");

/*使用ApplicationContext获取自己命名为ctx的Spring容器 END

*/

while(true){


System.out.print("请选择操作:"

+ "\n 1.不使用Spring就调用"

+ "\n HelloBean中的sayHello方法(以后不用)"

+ ",\n 2.使用Spring框架来调用HelloBean中的sayHello方法,"

+ "\n 3.原始地运用new等关键字来调用构造方法"

+ "\n (个人的理解:将被Spring容器"

+ "\n 的获取数据的方法取代,因为有“耦合”的缺点),"

+ "\n 4.构造器注入(引用注入)可以设置构造方法传的类不为空,"

+ "\n 同时调用含“传的类.传的声明的方法”的方法。"

+ "\n 5.使用Spring框架来调用"

+ "\n Student的属性和构造方法等等(没有使用value和ref)"

+ "\n 6.使用Spring框架来调用"

+ "\n Student的属性和构造方法等等(使用了value,没使用ref)"

+ "\n 7.使用Spring框架来调用"

+ "\n Student的属性和构造方法等等(使用了value和ref)"

+ "");

   Scanner input = new Scanner(System.in);

   int num = input.nextInt();

   System.out.println(num);

 

   if(num==1){

HelloBean hb = new HelloBean();

hb.sayHello();

}

if(num==2){


HelloBean a = (HelloBean) ctx.getBean("hb");

a.sayHello();

}

if(num==3){

/*在person中创建Axe属性,同时创建传

* Axe的构造方法,下面本来是调用其构造方法的方式,

* 但是使用spring框架后就不用像下面的方式来写代码了。

* 下面就是调用其构造方法*/



Person p = new Person(new Axe());


/*Person p = new Person(new Axe());

* 就是调用 Person(Axe类)的构造方法。用

* p.perform();调用不了其

* perform方法*/

//System.out.println(p);

}

if(num==4){

Person p = (Person)ctx.getBean("p");

p.perform();

/*因为在Spring框架配置文件中设置了

*  <bean id="axe" class="com.SSHC.bean.Axe"></bean>

    <bean id="p" class="com.SSHC.bean.Person">

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

    </bean>

    ,所以p.perform();的调用才不会报错。

    要知道perform方法中其实是“声明调用了传过来的

    Axe类中声明的方法”的一个方法。

    

    

    

    */


}

if(num==5){

/* 下面是配置文件中的设置:

 * 

 * <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="Kite"></constructor-arg>

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

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

    </bean>

 

 s1是没有给Student类用value设置构造方法中要打印的

 属性的值,也没有用ref调用只传axe类的构造方法,

 所以打印的语句的输出的结果

 的话就是空。

 

  而s2是只用value设置构造方法中要打印的属性的值,

  但没有用ref调用只传axe类的构造方法。

  

s3是用value设置构造方法中要打印的属性的值,

  也用了ref来调用只传axe类的构造方法。

 * */



Student s1 = (Student) ctx.getBean("s1");

System.out.println(s1);

}

if(num==6){

Student s2 = (Student) ctx.getBean("s2");

System.out.println(s2);

}

if(num==7){ 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="hb" class="com.SSHC.bean.HelloBean"></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="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就是应用上下文的意思



使用Spring框架来调用Student的属性和构造方法,构造器注入等等的例子 END




Spring中的依赖注入(DI)的方式:构造器注入,setter注入








构造器注入(也就是值注入,其中创建的类中会创建构造方法,传值)

总之的话,使用spring框架就是要尽量减少new,import等关键字的使用,要把这些关键字的使用配置文件的设置来替代。


这里的spring配置的设置,让调用构造方法等传的axe类,调用方法时,axe不会为空

总之,其实这些代码的话,都是有逻辑的,思考后就可以知道其规律等等。


这里的spring配置的设置,让调用构造方法等传的axe类,调用方法时,axe不会为空






控制反转的例子








创建一个spring框架的项目的方法 START


首先,创建lib的文件夹,之后把spring的复制粘贴到lib的文件夹中,这样的话就会更方便地找。之后就是把applicationContext.xml(虽然批量统一路径等改也可以运行,但最好不要改名字,为了让别人知道我使用了Spring框架)



首先,创建lib的文件夹,之后把spring的复制粘贴到lib的文件夹中,这样的话就会更方便地找。之后就是把applicationContext.xml(虽然批量统一路径等改也可以运行,但最好不要改名字,为了让别人知道我使用了Spring框架)


上面的红色部分是不使用Spring框架时在main方法中调用bean等中方法的方式,被Spring取代了,很多值内容统一地写在了xml配置文件中,方便查看,修改,且别的内容很少要修改。

先设置好配置文件,之后在获取配置文件内容,调用内容等等。

在main方法中获取spring容器的代码方法:

上面的红色部分是不使用Spring框架时在main方法中调用bean等中方法的方式,被Spring取代了,很多值内容统一地写在了xml配置文件中,方便查看,修改,且别的内容很少要修改。先设置好配置文件,之后在获取配置文件内容,调用内容等等。



一般出现了类的全路径,比如class=“XXX”的内容的话,一般就是使用了反射的技术。


一般出现了类的全路径,比如class=“XXX”的内容的话,一般就是使用了反射的技术。




创建一个spring框架的项目的方法 END


在servlet中获取spring容器的方法 START

在servlet中获取spring容器的方法 END

教你理解loC(控制反转)START

个人理解:

loC,控制反转就是本来可以在一个类中new出别的类(比如有main方法的text类中,new实例化了person类),单在依赖注入的模式下,这种可以new出别的类的“控制权”,被剥夺,

就是“控制权”被“反转”了,被“造反”了。


教你理解loC(控制反转)END



个人对容器的理解 START


个人理解:容器就是把数据放到里面,可以通过"."获取要拿出来的数据

个人理解:容器就是把数据放到里面,可以通过"."获取要拿出来的数据




个人对容器的理解 END

教你理解spring框架中的配置文件中的bean标签的id等内容 START


其实在applicationContext.xml文件中声明以上的bean标签中的内容的话,就是相当于在main方法中new实例化了一个类,同时id的值就像是特殊的独一无二的“饭票”,用独一无二的“饭票”,通过“."获取独一无二的对应的内容,比如图中的MyHello类中的内容。


其实在applicationContext.xml文件中声明以上的bean标签中的内容的话,就是相当于在main方法中new实例化了一个类,同时id的值就像是特殊的独一无二的“饭票”,用独一无二的“饭票”,通过“."获取独一无二的对应的内容,比如图中的MyHello类中的内容。






教你理解spring框架中的配置文件中的bean标签的id等内容 END



视频教程笔记 START


个人的理解:

开源指的是github上有源码。

spring的轻量级就是相对于EJB这个有非常笨重这个企业级的Java Beans而言的。

EJB是的Enterprise Java Beans技术的简称, 又被称为企业Java Beans。这种技术最早是由美国计算公司研发出来的。

“服务于其他的框架”的意思是可以整合其他的框架,让其他的框架可以一起结合起来使用,相当于”媒婆“。


Spring的低侵入式设计(比如依赖注入(DI),解耦就是体现了这种设计)就是指增强了各个部分的互不影响性,独立性,一个部分出BUG,修改后,别的部分出BUG等的影响会很小。因为项目会针对接口编程,dao,service类都会有一个接口,之前的MVC开发模式中使用了dao,service,结合之前自己使用接口的感觉后对其好处优点深有体会和认同。


Spring框架可以减少import,new等关键字的使用,减少修改时出的BUG等“一传十,十传百”的情况。Spring是接口编程,一些要new等调用的方法,就是被容器等代替了。spring

可以减少除了配置文件外的内容的批量地改变等等。



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


过滤器就是用了面向切面(AOP)的思想。

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。


aspect 英[ˈæspekt]美[ˈæspekt]

n.方面;


oriented,英语单词,主要用作形容词、动词,作形容词时译为“以……为方向的;重视……的”

oriented英[ˈɔːrientɪd]美[ˈɔːrientɪd]

v.朝向; 



programming英[ˈprəʊɡræmɪŋ]美[ˈproʊɡræmɪŋ]

n.编程; 






视频教程笔记 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



什么是面向切面 START

Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。


把和主业务无关的事情,放到代码外面去做。


某一行代码经常在你的Controller里出现,比如方法入口日志打印,那就要考虑使用AOP来精简你的代码了。



推荐阅读:https://www.zhihu.com/question/24863332



https://www.baeldung.com/spring-aop-vs-aspectj


什么是面向切面 END


1、SPRINgcORE框架_简介.ppt START

SPRINgcORE框架简介


SPRINgcORE框架简介


Spring框架介绍:

开源

轻量级

服务于其他框架


优点:

1、低侵入式设计。 2、依赖注入(DI)解耦 3、面向切面(AOP)提高代码的复用度 4、高度开放性,可自由选择功能引入。

Spring框架的核心思想

控制反转(IoC),通过依赖注入(ID)实现

面向切面编程(AOP)

Spring框架的最终目的就是简化java开发。


Spring容器

在Spring框架参与的应用中,你所创建的类可以交由Spring的容器来进行管理。

Spring容器将掌控这些类从创建到销毁的整个生命周期。

换个方式理解,就是在使用Spring框架以后,几乎所有需要创建的类都可以直接从Spring容器中获取。所以,项目中很少会用到关键字new来创建对象。我们把容器中的对象称为bean。

Spring容器中bean的生命周期

获取Spring容器

Spring容器是Spring的核心,一切Spring bean都存储在Spring容器内,并由其通过IoC技术管理。Spring容器也就是一个bean工厂(BeanFactory)。应用中bean的实例化,获取,销毁等都是由这个bean工厂管理的。

加载并获取Spring容器的类:

   1、FileSystemXmlApplicationContext(从文件系统加载,也就是写绝对路径)

   2、ClassPathXmlApplicationContext(从类路径加载,在src目录下)

   3、WebApplicationContext(在web工程中加载,在servlet上下文中直接获取)

创建第一个Spring程序

要使用Spring框架,需要在工程中添加相关的JAR包。从官方网站下载spring的最新版本。

跟其他的框架一样,Spring框架也有它自己的配置文件。Spring容器的加载就是依照这些配置文件进行的。我们可以在文件系统中直接加载Spring容器,在工程的类路径中加载Spring容器也可以在servlet上下文获取Spring容器。不管用那种方式加载,配置文件的内容不变。

创建第一个Spring程序

在工程的src目录下新增配置文件applicationContext.xml。

创建一个HelloBean类,该类中有一个sayHi方法。

创建第一个Spring程序

在applicationContext.xml文件中添加如下的代码:

<bean id="helloBean" class="springcore.HelloBean"></bean>

创建第一个Spring程序

创建一个测试类,在这个类中我们不要自己new出helloBean对象,而是从Spring容器中拿到这个对象,再调用syHi方法,把Hello world打印出来。

在servlet中获取Spring容器的方法

前面我们演示的是在main方法中获取Spring容器的代码,那么在servlet中如何获取Spring容器呢?

修改web.xml文件,添加一个listener标签。

在servlet中获取Spring容器的方法

将applicationContext.xml文件移到WEB-INF目录下。

创建一个servlet,在这个servlet中获取Spring容器的方法如下:


1、SPRINgcORE框架_简介.ppt END



2、SPRINgcORE框架_装配Bean.ppt START



2、SPRINgcORE框架_装配Bean.ppt END




在Java项目中使用Spring框架打印“Hello world” START


项目需要的文件的概览


package com.SSHC.bean;


public class MyHello {

    public void sayHello(){

    System.out.println("Hello world");

    }

}



package JavaText;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.SSHC.bean.MyHello;



public class Demo {


public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext ctx = new 

        ClassPathXmlApplicationContext("applicationContext.xml");

MyHello b = (MyHello) ctx.getBean("mh");

b.sayHello();

}


}

<?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="mh" class="com.SSHC.bean.MyHello"></bean>

</beans>






在Java项目中使用Spring框架打印“Hello world” END


在Java Web项目中使用Spring框架打印“Hello world” START

该项目所需文件概览

package com.SSHC.servlet;


import java.io.IOException;


import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;


import com.SSHC.bean.MyHello;


/**

 * Servlet implementation class TestServlet

 */

@WebServlet("/ts")

public class TestServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public TestServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

this.doPost(request, response);

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//获取javaweb的上下文

ServletContext sc = this.getServletContext();

//获取spring容器

WebApplicationContext ctx = WebApplicationContextUtils

.getWebApplicationContext(sc);

MyHello b = (MyHello) ctx.getBean("mh");

b.sayHello();

}


}



package com.SSHC.bean;


public class MyHello {

    public void sayHello(){

    System.out.println("Hello world");

    }

}


package com.SSHC.servlet;


import java.io.IOException;


import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;


import com.SSHC.bean.MyHello;


/**

 * Servlet implementation class TestServlet

 */

@WebServlet("/ts")

public class TestServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public TestServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

this.doPost(request, response);

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//获取javaweb的上下文

ServletContext sc = this.getServletContext();

//获取spring容器

WebApplicationContext ctx = WebApplicationContextUtils

.getWebApplicationContext(sc);

MyHello b = (MyHello) ctx.getBean("mh");

b.sayHello();

}


}


<?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="mh" class="com.SSHC.bean.MyHello"></bean>

</beans>




<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>Spring2</display-name>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>



在Java Web项目中使用Spring框架打印“Hello world” END


Spring框架:构造器注入,ref,servlet或Java项目中使用,面向切面,装配【诗书画唱】的评论 (共 条)

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