@PostConstruct的用法
时间: 2023-09-25 08:15:20 浏览: 123
@PostConstruct注解用于在依赖注入完成后,需要执行的方法上。它通常用于初始化操作,例如设置属性值、打开资源连接等。当一个类被实例化后,构造方法会被自动调用,然后再调用带有@PostConstruct注解的方法。
示例代码:
```java
import javax.annotation.PostConstruct;
public class MyClass {
private String property;
@Autowired
public MyClass(String property) {
this.property = property;
}
@PostConstruct
public void init() {
System.out.println("依赖注入完成,开始初始化操作");
// 在这里执行初始化操作,例如设置属性值、打开资源连接等
}
}
```<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [@PostConstruct注解的使用](https://blog.csdn.net/luxingzhou123/article/details/107695732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [@PostConstruct使用讲解](https://blog.csdn.net/cm15835106905/article/details/124772607)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文