spring原始注解(value)-03
生活随笔
收集整理的這篇文章主要介紹了
spring原始注解(value)-03
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本博客依據是是spring原始注解-02的代碼
注入普通數據類型:@Value注解的使用
1.添加driver屬性,使用value注解
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring原始注解开发-02
- 下一篇: spring新注解