Live2D

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

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

     我的博客地址:https://www.cnblogs.com/themysteryofhackers/p/12006785.html

     更新时间:2019-12-08

 

一、创建Maven项目

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

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

1575796209(1)

1575796502(1)

1575796608(1)

    

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

 

1575796656(1)

 

1575796755(1)

     然后就导入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>

1575797457(1)

 

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

 

1575797707(1)

1575797858(1)

 

      创建好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的项目就完成了。

 

1575801770(1)

 

结尾

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

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

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

posted @ 2019-12-08 19:20  黑客之谜  阅读(2223)  评论(0编辑  收藏  举报