IntelliJ IDEA 2017版 Spring5最基本的bean例子创建
一、简述
SpringBoot是基于spring框架之上的快速开发的框架.Spring4核心就是容器,容器提供了对bean的装配和管理.
spring依赖加载:
搜索:spring-context:如图:
![](https://images2018.cnblogs.com/blog/994129/201805/994129-20180503002131210-1912981760.png)
![](https://images2018.cnblogs.com/blog/994129/201805/994129-20180503002131210-1912981760.png)
粘贴到pom文档中
POM文档:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.study.spring</groupId> 7 <artifactId>spring4</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>war</packaging> 10 11 <name>spring4</name> 12 <description>Demo project for Spring Boot</description> 13 <url>http://maven.apache.org</url> 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.1.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 25 <!--修改编译级别为1.8--> 26 <maven.compiler.source>1.8</maven.compiler.source> 27 <mavne.compiler.target>1.8</mavne.compiler.target> 28 <java.version>1.8</java.version> 29 </properties> 30 31 <dependencies> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-context</artifactId> 35 <version>5.0.5.RELEASE</version> 36 </dependency> 37 <dependency> 38 <groupId>org.robolectric</groupId> 39 <artifactId>junit</artifactId> 40 <version>3.8</version> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-web</artifactId> 45 </dependency> 46 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-tomcat</artifactId> 50 <scope>provided</scope> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-starter-test</artifactId> 55 <scope>test</scope> 56 </dependency> 57 </dependencies> 58 59 <build> 60 <plugins> 61 <plugin> 62 <groupId>org.springframework.boot</groupId> 63 <artifactId>spring-boot-maven-plugin</artifactId> 64 </plugin> 65 </plugins> 66 </build> 67 68 69 </project>
1、编写配置文件Conf
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.spring4.conf; 2 3 import com.spring4.pojo.User; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.context.annotation.Scope; 7 8 /** 9 * Created by liuya 10 * 11 * @author : liuya 12 * Date: 2018/5/2 13 * Time: 23:50 14 * projectName:spring4 15 * 配置类 16 */ 17 18 @Configuration 19 public class SpringConf { 20 21 22 /** 23 * 配置bean 写一个方法,返回bean 24 * <p> 25 * 指定名字,默认是方法名字 26 */ 27 @Bean(name = "getUser") 28 /** 29 * 修改为非单例模式,创建的对象就是不一样的了 30 * 31 */ 32 @Scope(value = "prototype") 33 public User getUser() { 34 User user = new User(); 35 user.setAge(29); 36 user.setUserName("王圆圆"); 37 user.setPassWord("1110000"); 38 39 return user; 40 } 41 42 }
2、编写bean
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.spring4.pojo; 2 3 /** 4 * Created by liuya 5 * 6 * @author : liuya 7 * Date: 2018/5/2 8 * Time: 23:52 9 * projectName:spring4 10 */ 11 public class User { 12 13 private String userName; 14 15 private String passWord; 16 17 private Integer age; 18 19 public String getUserName() { 20 return userName; 21 } 22 23 public void setUserName(String userName) { 24 this.userName = userName; 25 } 26 27 public String getPassWord() { 28 return passWord; 29 } 30 31 public void setPassWord(String passWord) { 32 this.passWord = passWord; 33 } 34 35 public Integer getAge() { 36 return age; 37 } 38 39 public void setAge(Integer age) { 40 this.age = age; 41 } 42 43 @Override 44 public String toString() { 45 return "User{" + 46 "userName='" + userName + '\'' + 47 ", passWord='" + passWord + '\'' + 48 ", age=" + age + 49 '}'; 50 } 51 }
3、Spring4Application
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.study.spring; 2 3 import com.spring4.conf.SpringConf; 4 import com.spring4.pojo.User; 5 import org.springframework.boot.SpringApplication; 6 import org.springframework.boot.autoconfigure.SpringBootApplication; 7 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 8 9 import java.io.IOException; 10 import java.util.List; 11 12 /** 13 * @author liuyang 14 */ 15 @SpringBootApplication 16 public class Spring4Application { 17 18 public static void main(String[] args) { 19 // 通过Java配置来实例化Spring容器 20 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConf.class); 21 22 // 在Spring容器中获取Bean对象 23 User user = context.getBean(User.class); 24 25 //根据名字获取(默认是根据方法名字获取) 26 Object user2 = context.getBean("getUser"); 27 28 System.out.println(user.getUserName() + ", " + user.getAge() + ", " + user.getPassWord()); 29 System.out.println(user2.toString() + ", "); 30 String list = user2.toString(); 31 String[] ont = list.split(","); 32 for (int i = 0; i < ont.length; i++) { 33 System.out.println(ont[i]); 34 } 35 // 销毁该容器 36 context.destroy(); 37 } 38 }
十年磨一剑,一剑破万法