日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring原始注解(value)-03

發布時間:2024/10/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring原始注解(value)-03 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本博客依據是是spring原始注解-02的代碼


注入普通數據類型:@Value注解的使用
1.添加driver屬性,使用value注解

@Service("userService") public class UserServiceImpl implements UserService {@Value("${jdbc.driver}")private String driver;@Autowired//@Qualifier("userDao") //要注入的id值private UserDao userDao;@Overridepublic void sava() {System.out.println(driver);userDao.save1();} }

2.applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置組件掃描--> <!-- spring會掃描hao包下的所有子包和該包下的所有類--><context:component-scan base-package="com.hao"/><!-- 加載外部的properties配置文件--><context:property-placeholder location="classpath:jdbc.properties"/> </beans>

3.測試

public class UserController {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");UserService service = (UserService) context.getBean("userService");service.sava();} }

結果:
com.mysql.cj.jdbc.Driver
save running…


這個是演示Scope注解的使用,如果不懂Scope的作用請訪問scope的作用


這里演示的是@PostConstruct注解和@PreDestory注解相當于init-method和destory-method方法,不了解可以訪問init-method和destory-method方法

注意:銷毀方法中不會被打印出來信息,因為spring容器還沒有關閉,Java代碼已經執行結束

總結

以上是生活随笔為你收集整理的spring原始注解(value)-03的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。