{"id":1327,"date":"2025-03-26T12:04:10","date_gmt":"2025-03-26T03:04:10","guid":{"rendered":"https:\/\/route-zero.com\/recruit\/route\/1327\/"},"modified":"2025-06-26T12:08:56","modified_gmt":"2025-06-26T03:08:56","slug":"1327","status":"publish","type":"route","link":"https:\/\/route-zero.com\/recruit\/route\/1327\/","title":{"rendered":"Spring Boot \u306e@Autowired \u306f\u306a\u305c\u52d5\u304f\uff1fNullPointerException \u5bfe\u7b56\u304b\u3089\u5b66\u3076 DI\/IoC \u306e\u57fa\u672c\u3068\u300c\u6b63\u3057\u3044\u300d\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u7ba1\u7406\u8853"},"content":{"rendered":"<div>\n<h2>NullPointerException \u306b\u60a9\u3080\u3042\u306a\u305f\u3078\uff5c@Autowired \u306e\u6b63\u4f53\u3068 DI \u306e\u610f\u5473<\/h2>\n<p>\u3042\u306a\u305f\u3082\u3053\u3093\u306a\u7d4c\u9a13\u3042\u308a\u307e\u305b\u3093\u304b\uff1f<\/p>\n<p>\u300c@Autowired \u3092\u4f7f\u3063\u3066\u308b\u306e\u306b\u3001\u306a\u305c\u304b NullPointerException \u304c\u51fa\u308b\u2026\u300d\u300c@Component \u3068 @Service \u306e\u9055\u3044\u304c\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u2026\u300d<\/p>\n<p>Spring Boot \u3092\u4f7f\u3063\u305f\u958b\u767a\u306b\u6163\u308c\u3066\u304d\u305f\u9803\u3001\u3053\u3046\u3057\u305f\u201c\u5730\u5473\u306b\u30cf\u30de\u308b\u201d\u30c8\u30e9\u30d6\u30eb\u306b\u76f4\u9762\u3059\u308b\u3053\u3068\u304c\u5c11\u306a\u304f\u3042\u308a\u307e\u305b\u3093\u3002\u7279\u306b @Autowired \u306b\u95a2\u9023\u3059\u308b\u4f9d\u5b58\u6027\u6ce8\u5165\uff08Dependency Injection: DI\uff09\u306e\u3057\u304f\u307f\u306f\u3001\u8868\u9762\u7684\u306a\u7406\u89e3\u3067\u4f7f\u3044\u7d9a\u3051\u308b\u3068\u3001\u601d\u308f\u306c\u30a8\u30e9\u30fc\u306e\u539f\u56e0\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u52d9\u3067\u3088\u304f\u906d\u9047\u3059\u308b NullPointerException \u306e\u591a\u304f\u306f\u3001<strong>\u4f9d\u5b58\u95a2\u4fc2\u306e\u6ce8\u5165\u304c\u9069\u5207\u306b\u884c\u308f\u308c\u3066\u3044\u306a\u3044<\/strong>\u3053\u3068\u304c\u539f\u56e0\u3067\u3059\u3002<\/p>\n<p><strong>DI\uff08Dependency Injection\uff09\u3068\u306f\uff1f<\/strong> <br \/>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u751f\u6210\u3084\u4f9d\u5b58\u95a2\u4fc2\u306e\u6ce8\u5165\u3092\u958b\u767a\u8005\u304c\u76f4\u63a5\u8a18\u8ff0\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u5074\u306b\u4efb\u305b\u308b\u3053\u3068\u3067\u3001\u4fdd\u5b88\u6027\u30fb\u518d\u5229\u7528\u6027\u3092\u9ad8\u3081\u308b\u4ed5\u7d44\u307f\u3067\u3059\u3002Spring Boot \u3067\u306f IoC \u30b3\u30f3\u30c6\u30ca\u304c\u3053\u308c\u3092\u62c5\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001<\/p>\n<ul>\n<li>\n<p>@Autowired \u304c\u306a\u305c\u6a5f\u80fd\u3059\u308b\u306e\u304b\uff1f<\/p>\n<\/li>\n<li>\n<p>@Component, @Service, @Repository \u306e\u9055\u3044\u3068\u6b63\u3057\u3044\u4f7f\u3044\u5206\u3051\u65b9<\/p>\n<\/li>\n<li>\n<p>\u3088\u304f\u3042\u308b\u9593\u9055\u3044\u3068\u305d\u306e\u5bfe\u51e6\u6cd5\uff08\u5faa\u74b0\u53c2\u7167\u3084\u672a\u767b\u9332\u306e\u30af\u30e9\u30b9\uff09<\/p>\n<\/li>\n<\/ul>\n<p>\u3068\u3044\u3063\u305f\u7591\u554f\u3084\u8ab2\u984c\u306b\u5bc4\u308a\u6dfb\u3044\u306a\u304c\u3089\u3001Spring Boot \u306e DI \u3068 IoC\uff08Inversion of Control\uff09 \u306e\u57fa\u672c\u3092\u3084\u3055\u3057\u304f\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u300c\u4ed5\u7d44\u307f\u304c\u308f\u304b\u308b\u3068\u3001\u30a8\u30e9\u30fc\u304c\u6016\u304f\u306a\u304f\u306a\u308b\u300d\u2014\u2014\u305d\u3093\u306a\u5b89\u5fc3\u611f\u3092\u76ee\u6307\u3057\u3066\u3001\u3053\u308c\u304b\u3089\u4e00\u7dd2\u306b\u5b66\u3093\u3067\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<hr>\n<h2>@Autowired \u304c\u52d5\u304f\u4ed5\u7d44\u307f\u3068 DI\/IoC \u306e\u57fa\u790e\u7406\u89e3<\/h2>\n<p>Spring Boot \u3067\u306f\u3001\u958b\u767a\u8005\u304c\u660e\u793a\u7684\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u3001<strong>IoC\uff08Inversion of Control\uff09\u30b3\u30f3\u30c6\u30ca<\/strong>\u304c\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u7ba1\u7406\u3092\u62c5\u3044\u307e\u3059\u3002@Component \u3084 @Service \u3068\u3044\u3063\u305f\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u30af\u30e9\u30b9\u3092\u767b\u9332\u3059\u308b\u3068\u3001Spring \u306e\u30b3\u30f3\u30c6\u30ca\u304c\u305d\u306e\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u4ed6\u306e\u30af\u30e9\u30b9\u306b\u6ce8\u5165\u3057\u3066\u304f\u308c\u307e\u3059\u3002\u3053\u306e\u300c\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u6ce8\u5165\u3059\u308b\u300d\u4ed5\u7d44\u307f\u304c <strong>DI\uff08Dependency Injection\uff09<\/strong> \u3067\u3059\u3002<\/p>\n<h3>DI\u30b3\u30f3\u30c6\u30ca\u304c\u52d5\u4f5c\u3059\u308b\u57fa\u672c\u30b9\u30c6\u30c3\u30d7<\/h3>\n<ol>\n<li>\n<p>@Component\uff08\u307e\u305f\u306f @Service \u306a\u3069\uff09\u3092\u4ed8\u3051\u305f\u30af\u30e9\u30b9\u3092 Spring \u304c\u30b9\u30ad\u30e3\u30f3<\/p>\n<\/li>\n<li>\n<p>\u30b3\u30f3\u30c6\u30ca\u304c\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u30fb\u4fdd\u6301<\/p>\n<\/li>\n<li>\n<p>@Autowired \u304c\u4ed8\u3044\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u3084\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306b\u3001\u9069\u5207\u306a\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u81ea\u52d5\u3067\u6ce8\u5165<\/p>\n<\/li>\n<\/ol>\n<pre><code><span>@Service<\/span>\r\n<span>public<\/span> <span><span>class<\/span> <span>UserService<\/span> <\/span>{\r\n    <span>private<\/span> <span>final<\/span> UserRepository userRepository;\r\n\r\n    <span>@Autowired<\/span>\r\n    <span><span>public<\/span> <span>UserService<\/span><span>(UserRepository userRepository)<\/span> <\/span>{\r\n        <span>this<\/span>.userRepository = userRepository;\r\n    }\r\n}\r\n<span>\/\/ UserRepository \u304c\u767b\u9332\u3055\u308c\u3066\u3044\u308c\u3070\u3001\u81ea\u52d5\u3067\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u3055\u308c\u308b<\/span><\/code><\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u3001Spring \u306f UserRepository \u306b @Repository\uff08\u307e\u305f\u306f @Component\uff09\u304c\u4ed8\u3044\u3066\u3044\u308c\u3070\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3057\u3001UserService \u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306b\u6e21\u3057\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001\u73fe\u5728\u306f<strong>\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u304c\u63a8\u5968<\/strong>\u3055\u308c\u3066\u304a\u308a\u3001\u3053\u308c\u306f\u30c6\u30b9\u30c8\u6642\u306b\u30e2\u30c3\u30af\u306e\u5dee\u3057\u66ff\u3048\u304c\u3057\u3084\u3059\u304f\u3001\u518d\u5229\u7528\u6027\u304c\u9ad8\u307e\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n<hr>\n<h2>DI\u306e\u5931\u6557\u539f\u56e0\u3068\u89e3\u6c7a\u6cd5\uff5c\u3064\u307e\u305a\u304d\u3084\u3059\u30443\u3064\u306e\u30b1\u30fc\u30b9<\/h2>\n<p>\u30b1\u30fc\u30b9\u2460\uff1aDI \u5bfe\u8c61\u306e\u30af\u30e9\u30b9\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u306a\u3044<\/p>\n<pre><code><span>public<\/span> <span><span>class<\/span> <span>NotificationService<\/span> <\/span>{\r\n    <span><span>public<\/span> <span>void<\/span> <span>send<\/span><span>(String message)<\/span> <\/span>{\r\n        <span>\/\/ \u901a\u77e5\u51e6\u7406<\/span>\r\n    }\r\n}\r\n\r\n<span>@Service<\/span>\r\n<span>public<\/span> <span><span>class<\/span> <span>UserService<\/span> <\/span>{\r\n    <span>@Autowired<\/span>\r\n    <span>private<\/span> NotificationService notificationService;\r\n}<\/code><\/pre>\n<pre><code>java.lang.NullPointerException: Cannot invoke <span>\"NotificationService.send()\"<\/span> because <span>\"notificationService\"<\/span> <span>is<\/span> <span>null<\/span>\r\n<span>\/\/ NotificationService \u304c Spring \u306b\u767b\u9332\u3055\u308c\u3066\u3044\u306a\u3044\u30b1\u30fc\u30b9<\/span><\/code><\/pre>\n<p><strong>\u5bfe\u5fdc\u7b56<\/strong>\uff1aNotificationService \u306b @Component \u3092\u8ffd\u52a0\u3001\u307e\u305f\u306f @Bean \u3067\u660e\u793a\u767b\u9332<\/p>\n<hr>\n<p>\u30b1\u30fc\u30b9\u2461\uff1a\u30d5\u30a3\u30fc\u30eb\u30c9\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u306b\u3088\u308b\u898b\u3048\u306b\u304f\u3044\u4f9d\u5b58<\/p>\n<pre><code><span>@Service<\/span>\r\n<span>public<\/span> <span><span>class<\/span> <span>OrderService<\/span> <\/span>{\r\n    <span>@Autowired<\/span>\r\n    <span>private<\/span> PaymentGateway paymentGateway; <span>\/\/ \u4f9d\u5b58\u304c\u898b\u3048\u306b\u304f\u3044<\/span>\r\n}<\/code><\/pre>\n<p><strong>\u5bfe\u5fdc\u7b56<\/strong>\uff1a<strong>\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b<\/strong><\/p>\n<pre><code><span>@Service<\/span>\r\n<span>public<\/span> <span><span>class<\/span> <span>OrderService<\/span> <\/span>{\r\n    <span>private<\/span> <span>final<\/span> PaymentGateway paymentGateway;\r\n\r\n    <span><span>public<\/span> <span>OrderService<\/span><span>(PaymentGateway paymentGateway)<\/span> <\/span>{\r\n        <span>this<\/span>.paymentGateway = paymentGateway;\r\n    }\r\n}\r\n<span>\/\/ \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u4f9d\u5b58\u95a2\u4fc2\u3092\u660e\u793a\u3057\u3001\u30c6\u30b9\u30c8\u3067\u3082\u5dee\u3057\u66ff\u3048\u304c\u7c21\u5358\u306b<\/span><\/code><\/pre>\n<hr>\n<p>\u30b1\u30fc\u30b9\u2462\uff1a\u5faa\u74b0\u53c2\u7167\u306b\u3088\u308b\u521d\u671f\u5316\u30a8\u30e9\u30fc<\/p>\n<pre><code><span>@Service<\/span>\r\n<span>public<\/span> <span><span>class<\/span> <span>A<\/span> <\/span>{\r\n    <span>@Autowired<\/span>\r\n    <span>private<\/span> B b;\r\n}\r\n\r\n<span>@Service<\/span>\r\n<span>public<\/span> <span><span>class<\/span> <span>B<\/span> <\/span>{\r\n    <span>@Autowired<\/span>\r\n    <span>private<\/span> A a;\r\n}<\/code><\/pre>\n<p><strong>\u5bfe\u5fdc\u7b56<\/strong>\uff1a\u4f9d\u5b58\u306e\u5411\u304d\u3092\u4e00\u65b9\u5411\u306b\u8a2d\u8a08\u3057\u76f4\u3059 or @Lazy \u3067\u9045\u5ef6\u89e3\u6c7a<\/p>\n<hr>\n<h2>\u307e\u3068\u3081\u3068\u6b21\u306b\u8aad\u307f\u305f\u3044\u95a2\u9023\u30c8\u30d4\u30c3\u30af<\/h2>\n<h3>\u672c\u8a18\u4e8b\u306e\u307e\u3068\u3081<\/h3>\n<ul>\n<li>\n<p>@Autowired \u306f Spring \u306e DI \u6a5f\u80fd\u306b\u3088\u308a\u3001\u81ea\u52d5\u7684\u306b\u4f9d\u5b58\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6ce8\u5165\u3059\u308b<\/p>\n<\/li>\n<li>\n<p>\u30af\u30e9\u30b9\u306f @Component \u7cfb\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u767b\u9332\u5fc5\u9808\uff08\u672a\u767b\u9332\uff1dnull\uff09<\/p>\n<\/li>\n<li>\n<p><strong>\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3<\/strong> \u63a8\u5968\uff1a\u4f9d\u5b58\u95a2\u4fc2\u304c\u660e\u793a\u7684\u306b\u306a\u308a\u3001\u30c6\u30b9\u30c8\u3082\u3057\u3084\u3059\u3044<\/p>\n<\/li>\n<li>\n<p>\u3088\u304f\u3042\u308b\u30a8\u30e9\u30fc\uff08NullPointerException\u3001\u5faa\u74b0\u53c2\u7167\uff09\u306f\u8a2d\u8a08\u3068\u767b\u9332\u30df\u30b9\u304c\u4e3b\u56e0<\/p>\n<\/li>\n<\/ul>\n<p>\u300c@Autowired \u304c\u201c\u306a\u305c\u52d5\u304f\u304b\u201d\u304c\u308f\u304b\u3063\u305f\u3053\u3068\u3067\u3001\u81ea\u4fe1\u3092\u6301\u3063\u3066 Spring Boot \u306b\u5411\u304d\u5408\u3048\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u300d\u2014\u2014\u3082\u3057\u305d\u3046\u611f\u3058\u3066\u3082\u3089\u3048\u305f\u306a\u3089\u5b09\u3057\u3044\u9650\u308a\u3067\u3059\u3002<br \/>\u4eca\u5f8c\u3082\u300c\u5b9f\u52d9\u3067\u5f79\u7acb\u3064\u57fa\u790e\u300d\u3092\u4e00\u7dd2\u306b\u6df1\u3081\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u3010\u5916\u90e8\u30ea\u30f3\u30af\u3011<\/h3>\n<p><a href=\"https:\/\/spring.io\/projects\/spring-boot\">Spring Boot\u516c\u5f0f\u30b5\u30a4\u30c8<\/a><\/p>\n<p><a href=\"https:\/\/spring.io\/guides\/gs\/serving-web-content\/\">Spring Framework\u516c\u5f0f\u30ac\u30a4\u30c9 \u2013 Spring MVC<\/a><\/p>\n<h3>\u3010\u5185\u90e8\u30ea\u30f3\u30af\u3011<\/h3>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1100\/\">\u3010Spring Boot\u5b9f\u8df5\u30ac\u30a4\u30c9\u3011List\u578b\u30fbMap\u578b\u306e\u9055\u3044\u3068Bean\u30af\u30e9\u30b9\u306e\u57fa\u790e\uff06\u5fdc\u7528<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1104\/\">Spring Boot\u306e\u3088\u304f\u4f7f\u308f\u308c\u308b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3068\u306f\uff1f\u521d\u5fc3\u8005\u5fc5\u898b\u306e\u89e3\u8aac\u3068\u6d3b\u7528\u6cd5<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1302\/\">Spring Boot\u3067\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3055\u308c\u306a\u3044\u539f\u56e0\u3068\u306f\uff1f@Transactional\u5b8c\u5168\u30ac\u30a4\u30c9<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1299\/\">Spring Boot\u3067\u7406\u89e3\u3059\u308bGET\u3068POST\u306e\u9055\u3044\uff5cThymeleaf\u5b9f\u88c5\u4f8b\u4ed8\u304d\u3067\u521d\u5fc3\u8005\u306e\u753b\u9762\u9077\u79fb\u30fb\u30a8\u30e9\u30fc\u5bfe\u5fdc\u307e\u3067\u4e01\u5be7\u89e3\u8aac<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1269\/\">MVC\u69cb\u6210\u3092\u7406\u89e3\u3059\u308c\u3070\u73fe\u5834\u3067\u8a70\u307e\u3089\u306a\u3044 | Spring Boot\u5b9f\u88c5\u30ac\u30a4\u30c9<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1138\/\">Java\u306efor\u6587\u3092Stream\u306b\u66f8\u304d\u63db\u3048\u308b\u306b\u306f\uff1fSpring Boot\u3067\u306e\u5b9f\u8df5\u4f8b\u4ed8\u304d\u3067\u308f\u304b\u308b\uff01<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1088\/\">Spring Boot\u30a2\u30d7\u30ea\u958b\u767a\u3067\u983b\u767a\u3059\u308bWhitelabel Error Page\uff5cThymeleaf\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30a8\u30e9\u30fc\u3068\u5bfe\u7b56\u307e\u3068\u3081<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1115\/\">Spring Boot \u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u5165\u9580\uff5cMVC\u7d71\u5408\u3068\u30ab\u30b9\u30bf\u30e0\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u5b9f\u8df5\u4f8b<\/a><\/p>\n<\/div>\n","protected":false},"featured_media":1328,"template":"","_links":{"self":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route\/1327"}],"collection":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route"}],"about":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/types\/route"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media\/1328"}],"wp:attachment":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media?parent=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}