关注各种黑科技
B站UP主,编程教学,游戏玩家,挂B,金牛座

Java入坑day06 纯手工建立spring开发环境

自己也配了一下spring maven项目,从最简单的开始,
用idea建一个java hello项目,再激活maven

从Spring 3.0开始 springframework分离为多个jar包
spring-core不应用也行 已经在spring-context里了,不过最好还是写全

spring-beans也有用, 不过目前还没搞明白,先能run再说

package com.devecho;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "SpringBeans.xml");

        HelloWorld obj = (HelloWorld) context.getBean("helloBean");
        obj.printHello();
    }
}

package com.devecho;

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void printHello() {
        System.out.println("Spring 3 : Hello ! " + name);
    }
}
<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-3.0.xsd">

    <bean id="helloBean" class="com.devecho.HelloWorld">
        <property name="name" value="Devecho" />
    </bean>

</beans>
<?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>com.devecho</groupId>
    <artifactId>hello</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>5.1.2.RELEASE</spring.version>
    </properties>
    <dependencies>
        <!--<dependency>-->
            <!--<groupId>org.springframework</groupId>-->
            <!--<artifactId>spring-core</artifactId>-->
            <!--<version>${spring.version}</version>-->
        <!--</dependency>-->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</project>

.iml 是idea的配置,用了maven auto import会自动更新

没关联的话,可以右键pom.xml reimport即可,iml会自动增加

eclipse的项目依赖是.classpath 和 idea的iml类似,另外eclipse一般叫更新maven,不是import

熟悉以后还是用springboot这种脚手架快

转载请注明出处
分享到: 更多 (0)

评论 0