写一个Demo,总结一下我在SpringBoot2.X中使用IOC的方式。
一、开发基础,首先这个demo是在springboot web基础框架上建立的,搭建请参照SpringBoot2.X之旅,开篇 hello world(Web Project),在这个基础上引入lombok包,pom.xml配置如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cobra</groupId>
<artifactId>webdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webdemo</name>
<description>Demo project for Spring Boot</description>
<!--默认是jar包,编译成war包需要重新定义-->
<!--<packaging>war</packaging>-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--@Slf4j、@Data的使用的依赖包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、Bean 的定义和注入使用方式之一:
1、在application.yml配置文件中添加信息:
user:
username: 周伯通
nickname: 老顽童
age: 3
emailAddress: [email protected]
2、新建config包,新建UserConfig类,@ConfigurationProperties(prefix = "user")获取user信息,@Component定义并注入bean
package com.cobra.webdemo.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/7 11:20
*/
@Data
@Component
@ConfigurationProperties(prefix = "user")
public class UserConfig {
private String username;
private String nickname;
private Integer age;
private String emailAddress;
}
3、bean的使用,在controller下新建UserController,@Autowired引入UserConfig的bean,定义获取user配置信息的get请求方法
package com.cobra.webdemo.controller;
import com.cobra.webdemo.config.UserConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/6 23:27
*/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private UserConfig userConfig;
/**
* 获取user的配置参数
* @return
*/
@GetMapping("/info")
public UserConfig getUserConfig() {
log.info(userConfig.toString());
return userConfig;
}
}
4、启动,浏览器测试,:
三、Bean 的定义和注入使用方式之二:
1、在项目目录下新建vo包,新建类OrderVO
package com.cobra.webdemo.vo;
import lombok.Data;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/7 11:42
*/
@Data
public class OrderVO {
private String orderId;
private String username;
private String email;
}
2、在config包下,新建OrderConfig类,@Autowired引入获取配置文件user信息,这里可以用另一个注解@Configuration定义并注入bean,@Bean注解返回OrderVO的方法:
package com.cobra.webdemo.config;
import com.cobra.webdemo.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/7 11:53
*/
@Configuration
public class OrderConfig {
@Autowired
private UserConfig userConfig;
@Bean
public OrderVO getOderVO(){
OrderVO orderVO = new OrderVO();
orderVO.setOrderId("123456789");
orderVO.setUsername(userConfig.getUsername());
orderVO.setEmail(userConfig.getEmailAddress());
return orderVO;
}
}
3、用@Autowired在UserController引入OrderVObean,定义获取orderVO的get方法:
package com.cobra.webdemo.controller;
import com.cobra.webdemo.config.UserConfig;
import com.cobra.webdemo.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/6 23:27
*/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private UserConfig userConfig;
@Autowired
private OrderVO orderVO;
/**
* 获取user的配置参数
* @return
*/
@GetMapping("/info")
public UserConfig getUserConfig() {
log.info(userConfig.toString());
return userConfig;
}
/**
* 获取OrderVO
* @return
*/
@GetMapping("/order")
public OrderVO getOrderVO(){
log.info(orderVO.toString());
return orderVO;
}
}
4、启动程序,测试:
四、个人常用的IOC注解
1、定义注入bean:@Component、@Configuration、@RestController、@Controller、@Service、@Repository
2、引入bean:@Autowired、@Resource
@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用;
@Resource(这个注解属于J2EE的),默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。