IntelliJ IDEA 2017版 Spring5最基本的bean例子创建

时间:2023-12-31 22:13:56

一、简述

        SpringBoot是基于spring框架之上的快速开发的框架.Spring4核心就是容器,容器提供了对bean的装配和管理.
      spring依赖加载:
       搜索:spring-context:如图:
              IntelliJ IDEA 2017版 Spring5最基本的bean例子创建

IntelliJ IDEA 2017版 Spring5最基本的bean例子创建粘贴到pom文档中

POM文档:

 <?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.study.spring</groupId>
<artifactId>spring4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <name>spring4</name>
<description>Demo project for Spring Boot</description>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!--修改编译级别为1.8-->
<maven.compiler.source>1.8</maven.compiler.source>
<mavne.compiler.target>1.8</mavne.compiler.target>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>junit</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

1、编写配置文件Conf

 package com.spring4.conf;

 import com.spring4.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; /**
* Created by liuya
*
* @author : liuya
* Date: 2018/5/2
* Time: 23:50
* projectName:spring4
* 配置类
*/ @Configuration
public class SpringConf { /**
* 配置bean 写一个方法,返回bean
* <p>
* 指定名字,默认是方法名字
*/
@Bean(name = "getUser")
/**
* 修改为非单例模式,创建的对象就是不一样的了
*
*/
@Scope(value = "prototype")
public User getUser() {
User user = new User();
user.setAge(29);
user.setUserName("王圆圆");
user.setPassWord("1110000"); return user;
} }

2、编写bean

 package com.spring4.pojo;

 /**
* Created by liuya
*
* @author : liuya
* Date: 2018/5/2
* Time: 23:52
* projectName:spring4
*/
public class User { private String userName; private String passWord; private Integer age; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWord() {
return passWord;
} public void setPassWord(String passWord) {
this.passWord = passWord;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", age=" + age +
'}';
}
}

3、Spring4Application

 package com.study.spring;

 import com.spring4.conf.SpringConf;
import com.spring4.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.io.IOException;
import java.util.List; /**
* @author liuyang
*/
@SpringBootApplication
public class Spring4Application { public static void main(String[] args) {
// 通过Java配置来实例化Spring容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConf.class); // 在Spring容器中获取Bean对象
User user = context.getBean(User.class); //根据名字获取(默认是根据方法名字获取)
Object user2 = context.getBean("getUser"); System.out.println(user.getUserName() + ", " + user.getAge() + ", " + user.getPassWord());
System.out.println(user2.toString() + ", ");
String list = user2.toString();
String[] ont = list.split(",");
for (int i = 0; i < ont.length; i++) {
System.out.println(ont[i]);
}
// 销毁该容器
context.destroy();
}
}