本博客依据是是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; @Override
public 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/beans
http://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的更多相关文章

  1. spring原始注解开发-01

    我们使用xml-Bean标签的配置方式和注解做对比理解 1.创建UserDao接口以及UserDao的实现类UserDaoImpl(接口代码省略) public class UserDaoImpl i ...

  2. spring原始注解

    spring原始注解主要是替代Bean标签的配置 @Component:使用在类上用于实例化Bean @Controller:使用在web层类上用于实例化Bean @Service:使用在servic ...

  3. Spring原始注解开发-02

    使用@Repository.@Service.@Controller注解配置,使其更加清晰属于哪一层,因为我是模拟的web层,所有没有使用@Controller注解,后面结合web开发会使用到 1.创 ...

  4. Spring常用注解介绍【经典总结】

    Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...

  5. Spring MVC注解的一些案列

    1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...

  6. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  7. Spring IOC的描述和Spring的注解(转)

    Spring常用的注解 本文系转载:转载网址: http://www.cnblogs.com/xdp-gacl/p/3495887.html http://ljhzzyx.blog.163.com/b ...

  8. Spring常用注解简析

    1. Autowired 自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property.当然,getter看个人需求,如果私有属性需要对外提供的话,应当 ...

  9. Spring常用注解总结

    转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...

随机推荐

  1. mysql卡顿问题查找和解决方法

    mysql卡顿问题查找和解决方法 版权一.所遇问题        写在前边的废话:今天面试阿里的时候问到过类似问题,以前做调优的时候都是现查现用,缺乏总结,面试时答得也不好,今天趁此机会做一个梳理,知 ...

  2. 记一次mybatis-plus遇到的问题

    在用了 springboot 和 mybatis-plus很久之后, 有一天突然看到配置文件有点繁杂, 想将相同的配置拉到application.yml里, 就在将配置拉过去后, 问题就开始出现了. ...

  3. 七天接手react项目 —— state&事件处理&ref

    state&事件处理&ref 在 react 起步 一文中,我们学习了 react 相关知识:jsx.组件.props.本篇将继续研究 state.事件处理和ref. state St ...

  4. LGP5279题解

    这题好牛逼啊... 虽然说也是 DP 套 DP,但是感觉比 TJOI 那题高明到哪里去了( 我们先考虑如何计算期望.如果设 \(f_i\) 为拿到 \(i\) 张牌后胡的方案数,这个并不是很好做,因为 ...

  5. ASP.NET Core 6框架揭秘实例演示[24]:中间件的多种定义方式

    ASP.NET Core的请求处理管道由一个服务器和一组中间件组成,位于 "龙头" 的服务器负责请求的监听.接收.分发和最终的响应,针对请求的处理由后续的中间件来完成.中间件最终体 ...

  6. 网关中间件-Nginx(一)

    一.Nginx介绍 1.nginx是一个高性能HTTP服务器,反向代理服务器,邮件代理服务器,TCP/UDP反向代理服务器. 2.nginx处理请求是异步非阻塞的,在高并发下nginx 能保持低资源低 ...

  7. java反射获取类的成员函数,成员变量,构造函数

    package com.imooc.reflect;import javax.sound.midi.Soundbank;import java.lang.reflect.Constructor;imp ...

  8. JavaWeb Bug处理

    1.(getParameter) Java文本无法解析Cannot resolve method 'getParameter(java.lang.String)方法    解决方法:1.在projec ...

  9. web服务器-nginx负载均衡

    web服务器-nginx负载均衡 一 负载均衡的作用 负载均衡: 分摊到多个操作单元上进行执行,和它的英文名称很匹配.就是我们需要一个调度者,保证所有后端服务器都将性能充分发挥,从而保持服务器集群的整 ...

  10. Apache HBase MTTR 优化实践

    HBase介绍 HBase是Hadoop Database的简称,是建立在Hadoop文件系统之上的分布式面向列的数据库,它具有高可靠.高性能.面向列和可伸缩的特性,提供快速随机访问海量数据能力. H ...