在maven基础下配置spring

作者: giraffecode9668 | 来源:发表于2018-12-28 20:29 被阅读2次

1.使用maven新建项目

archetype列表中选择maven-archetype-webapp新建javaweb项目。

2.添加jar包

maven在pom.xml文件中已经帮我们写好了一些东西,我们看到<dependencies>这个依赖包,代码如下:

 <dependencies>

   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.11</version>
     <scope>test</scope>
   </dependency>

 </dependencies>

注:maven默认帮我们配置了一个junit框架,这个框架是测试代码用的,大家可以查查它是怎么使用的

<dependencies>标签中添加spring-corespring-context两个依赖包,添加后IDEA会提示importimport一下。代码如下:


<dependencies>

    <!-- junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- Spring Core -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.1.4.RELEASE</version>
    </dependency>

    <!-- Spring Context -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.4.RELEASE</version>
    </dependency>

  </dependencies>

3.beans.xml管理

beans.xml位置:main/resources/beans.xml
main包下如果没有resources包,创建resources,在resources包创建beans.xml文件
beans.xml文件代码如下:


<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">


</beans>

4.大功告成

什么?没了?真的就没了!就这么简单。
那我们用个例子来体验spring框架吧!
创建下面的项目文件

helloworld例子

HelloWorld.java声明sayHello()方法

public interface HelloWorld {
    public void sayHello();
}

SpringHelloWorld.java继承HelloWorld.java并实现sayHello()方法,我们让它说“spring say helloworld!”

public class SpringHelloWorld implements HelloWorld {
    @Override
    public void sayHello() {
        System.out.println("spring say helloworld!");
    }
}

StrutsHelloWorld.java继承HelloWorld.java并实现sayHello()方法,我们让它说“struts say helloworld!”

public class StrutsHelloWorld implements HelloWorld {
    @Override
    public void sayHello() {
        System.out.println("struts say helloworld!");
    }
}

我们顺便把HelloWorldService完善一下吧

public class HelloWorldService {
    private HelloWorld helloWorld;
    
    public HelloWorldService(){
        
    }

    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

接下来的两步是重点,做笔记做笔记

第一步:写入beans.xml

beans.xml写入我们的文件名和路径,具体代码如下:

<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">

    <bean id="springHelloWorld" class = "com.test.spring.helloworld.impl.SpringHelloWorld"/>
    <bean id="strutsHelloWorld" class="com.test.spring.helloworld.impl.StrutsHelloWorld"/>

    <bean id="helloWorldService" class="com.test.spring.helloworld.HelloWorldService">
        <property name="helloWorld" ref="strutsHelloWorld"/>
    </bean>
</beans>
 <bean id="使用时候用的id" class = "路径"/>
    <bean id="helloWorldService" class="helloworld.HelloWorldService">
        <property name="helloWorld" ref="strutsHelloWorld"/>
    </bean>

    <bean id="使用时候用的id" class="路径">
        <property name="参数" ref="调用哪一个id生成的实参"/>
    </bean>

这个时候IDEA会提示“Application context not configured for this file”,点击Configure application context咯
那就配好第一步啦!别急,下一步就是使用了

第二步:使用beans.xml

HelloProgram使用beans.xml

public class HelloProgram {
    public static void main(String[] args) {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");

        HelloWorldService service =
                (HelloWorldService) context.getBean("helloWorldService");

        HelloWorld hw= service.getHelloWorld();

        hw.sayHello();
    }
}

然后点击main函数左边的一个三角号运行

struts say helloworld
可以看到这里的代码虽然没有给service实例StrutsHelloWorld类,但是结果它已经实例了StrutsHelloWorld这个类,我想你也已经猜到了这里的写法了
        HelloWorldService service =
                (HelloWorldService) context.getBean("helloWorldService");

这里context.getBean("helloWorldService")就是使用了beans.xml中的<bean id="helloWorldService">,因为前面我们说了,已经把strutsHelloWorld给了helloWorldService了,所以这里就是调用strutsHelloWorld的方法了。

如果我们把beans.xml代码中的helloWorldService赋的参数改为springHelloWorld

    <bean id="helloWorldService" class="helloworld.HelloWorldService">
 <!-- <property name="helloWorld" ref="strutsHelloWorld"/>-->
        <property name="helloWorld" ref="springHelloWorld"/>
    </bean>

继续运行HelloProgram.java,结果变了


spring say helloworld

好了这就真的大功告成了~~

spring 就是利用beans.xml来实现对实例类之间的解耦

参考以下文档:https://www.yiibai.com/spring/spring-tutorial-for-beginners.html

相关文章

网友评论

    本文标题:在maven基础下配置spring

    本文链接:https://www.haomeiwen.com/subject/hzwdlqtx.html