美文网首页
Spring小记(1):构建一个Maven的Spring项目

Spring小记(1):构建一个Maven的Spring项目

作者: 夜玉龙 | 来源:发表于2020-03-06 14:15 被阅读0次

    通过maven构建项目是一件很简单的事情,下面我们使用Maven来构建一个Spring的项目
    1、首先创建一个maven的简单project,如下

    设置组和ArtifactId 设置工程名

    2、添加maven的依赖,具体的做法是在pom.xml文件中写入下面的依赖信息

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>Group1</groupId>
        <artifactId>artifacId</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <!-- Spring Core-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
    
            <!--Spring Context-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            <!--Spring Jdbc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>
        </dependencies>
    </project>
    

    3、创建一个接口,定义一个未实现的方法,如下

    public interface HelloWorld {
        public void sayHello();
    }
    

    4、对这个接口进行两个实现,方法如下
    (1)SpringHelloWorld

    public class SpringHelloWorld implements HelloWorld{
        public void sayHello() {
            System.out.println("Spring Say Hello!");
        }
    }
    

    (2)StructsHelloWorld

    public class StructsHelloWorld implements HelloWorld{
        public void sayHello() {
            System.out.println("Structs Say Hello!");
        }
    }
    

    5、编写代理方法

    public class HelloWorldService {
        private HelloWorld helloWorld;
    
        public HelloWorldService(){
    
        }
    
        public HelloWorld getHelloWorld() {
            return helloWorld;
        }
    
        public void setHelloWorld(HelloWorld helloWorld) {
            this.helloWorld = helloWorld;
        }
    }
    

    6、编写依赖注入的xml文件beans.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="springHelloWorld"
              class="com.fan.spring.hello.impl.SpringHelloWorld"/>
    
        <bean id="structsHelloWorld" class="com.fan.spring.hello.impl.StructsHelloWorld"/>
    
        <bean id="helloWorldService" class="com.fan.spring.hello.HelloWorldService">
            <property name="helloWorld" ref="structsHelloWorld"></property>
        </bean>
    </beans>
    

    7、编写测试方法

    public class HelloProgram {
    
        public static void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");
            HelloWorld hw = service.getHelloWorld();
    
            hw.sayHello();
        }
    }
    

    8、测试并查看运行的结果,如下

    测试结果

    这样,一个简单的spring工程就构建完毕了

    相关文章

      网友评论

          本文标题:Spring小记(1):构建一个Maven的Spring项目

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