IDEA Spring环境搭建+简单入门(图文教程)

IDEA Spring环境搭建+简单入门(图文教程)

首先利用IDEA创建Maven工程项目

1.选择新建项目

2.选中Maven骨架 3.填写项目名称和项目位置 4.Finsh之后默认打开的是pom.xml文件 5.在pom.xml文件下填写Spring的相关依赖(其中有一些拓宽工具依赖) 5.1完整的pom.xml代码(可直接复制)

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

4.0.0

org.example

spring_demo

1.0-SNAPSHOT

junit

junit

4.13

org.springframework

spring-context

5.0.2.RELEASE

log4j

log4j

1.2.9

org.springframework

spring-test

5.0.2.RELEASE

org.projectlombok

lombok

1.18.12

provided

6.完善简单项目结构和编写测试类文件 6.1简单项目结构编写 6.2在resources包下创建Spring配置文件,整合日志配置文件 6.2.1 applicationContext.xml 是Spring配置文件 applicationContext是约定俗成的叫法

xmlns:p="http://www.springframework.org/schema/p"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

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

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd">

6.2.2 log4j2.xml 日志配置文件

6.3编写简单User类,并加上注解(可以直接复制) **说明:这里的User类用到了 Lombok工具,具体可参照官网文档 Lombok官网:Lombok @Data 替我们生成 getter,setter,toString等方法 @AllArgs/NoArgsConstructor分别为全参构造和无参构造

package com.xxx.demo.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import org.springframework.stereotype.Component;

@Data

@AllArgsConstructor

@NoArgsConstructor

@Component

public class User {

private String userName;

private String password;

public void add(){

System.out.println("User add.........");

}

}

6.4在config包下编写AppConfig类,并加上注解(可直接复制代码) @ComponentScan: 全局扫描组件 其中的参数 basePackages 扫描组件的包 @Configuration: 声明当前类为JavaConfig类 @Bean: 自动装配

package com.xxx.demo.config;

import com.xxx.demo.pojo.User;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@ComponentScan(basePackages = "com.xxx.demo")

@Configuration

public class AppConfig {

@Bean

public User user(){

return new User();

}

}

7.至此Spring简单配置完成,接下来测试 7.1简单完善test包,包名和java包下保持一直一致如图 7.2在test的java包下的pojo包下创建 TestUser 测试类

package com.xxx.demo.pojo;

import com.xxx.demo.config.AppConfig;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = AppConfig.class)

public class TestUser {

@Autowired

private User user;

@Test

public void test(){

user.add();

}

}

7.3运行结果

8.到此简单Spring基础环境搭建好,接下去就是对Spring部分基础入门学习

8.1 bean的作用域 1.单例(Singleton) 在整个应用程序中,只创建bean的一个shili 2.原型(prototype) 每次注入或通过Spring上下文获取的时候,都会创建一个新的bean实例 3.会话(Session) 在Web应用中,为每个会话创建一个bean实例 4.请求(request) 在Web应用中,为每个请求创建一个bean实例

8.2 Spring的框架运行有三种配置方式 1.xml配置 2.自动装配(注解的方式) 3.JavaConfig配置(如前面的AppConfig类+注解配置)

简单总结

Spring的环境搭建使用Maven更加方便Spring的注解需要多学习,实际应用远不止这些基础注解Spring的IOC控制反转和AOP切面编织思想需要认真理解博客的编写不够熟练,整体排版简陋

若本博客内容描述有错误请各位及时留言告知