首页 > 编程语言 > 详细

如何创建Maven项目和Spring IOC例子

时间:2019-12-08 20:26:12      阅读:127      评论:0      收藏:0      [点我收藏+]

     把如何创建Maven项目和创建Spring IOC的例子分享给大家,希望能对大家有帮助!

     我的博客地址:

     更新时间:2019-12-08


一、创建Maven项目

     我用的是Intellij IDEA开发工具创建Maven项目的,打开该软件后,直接点击file --->project,如下图所示,

然后就直接跟着我的图片的步骤往下走。

技术分享图片

技术分享图片

技术分享图片

    

     到了这一个就创建好了Maven项目了,然后开发工具会在右下角提示下图的信息,直接点击自动导入就好。


技术分享图片


技术分享图片

     然后就导入Spring IOC的项目依赖,可以去这个网站查找Maven依赖查找。然后在pom.xml文件先导入下面的依赖。

 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

技术分享图片


     导入依赖后就创建包,创建包是为了更好的去管理Java类,创建好包之后就直接创建类,创建包和类的命名遵从Java命名规范即可。


技术分享图片

技术分享图片


      创建好Student类后,然后在resources文件夹里面直接创建applicationContext.xml文件,最后在test下的java下创建一个包,在创建一个测试类,具体代码如下:

Student.java

package com.zzx.entity;

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Integer sex;
    private String address;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Integer getSex() {
        return sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", sex=" + sex +
                ", address=‘" + address + ‘\‘‘ +
                ‘}‘;
    }
}


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd ">
    <!-- 把一个对象放进Spring容器 -->
    <bean name="s1" class="com.zzx.entity.Student">
        <property name="id" value="1"></property>
        <property name="name" value="小红"></property>
        <property name="age" value="18"></property>
        <property name="sex" value="2"></property>
        <property name="address" value="中国"></property>
    </bean>
    <!-- 把另一个对象也放到spring容器中,对象的名字不能重复,否则运行会报错 -->
    <!-- 用property设置对象的属性,那该对象要有setter方法,还要有一个无参数的构造方法 -->
    <bean name="s2" class="com.zzx.entity.Student">
        <property name="id" value="2"></property>
        <property name="name" value="小白"></property>
        <property name="age" value="16"></property>
        <property name="sex" value="1"></property>
        <property name="address" value="中国"></property>
    </bean>
</beans>


Test01.java

package com.zzx.ioc;

import com.zzx.entity.Student;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {
    @Test
    public void studentTest(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student s1 = applicationContext.getBean("s1", Student.class);
        Student s2 = applicationContext.getBean("s2", Student.class);
        System.out.println(s1);
        System.out.println(s2);
    }

}


     最后,直接运行程序,这样一个简单的Spring IOC结合Maven的项目就完成了。


技术分享图片


结尾

     我是一个Java程序员,一个向往技术的小白,以后我会陆续将自己学习到的Java或者其他的知识会以博客的形式分享出来,希望能对大家有帮助。

     喜欢小编的就给我一个关注吧!

     如果有哪些问题、有哪些不妥或者侵犯到您的权益的地方,可以联系我,我马上修改。

如何创建Maven项目和Spring IOC例子

原文:https://www.cnblogs.com/themysteryofhackers/p/12006785.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!