Spring——Spring基于注解的IOC配置

基于注解的IOC配置

学习基于注解的IOC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。

1.创建工程

在这里插入图片描述

1.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.wt</groupId><artifactId>Spring_IOC_Annotation</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- Spring常用依赖 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.8.RELEASE</version></dependency></dependencies>
</project>

1.2 dao

/*** 持久层实现类*/
public class UserDaoImpl implements UserDao {@Overridepublic void addUser(){System.out.println("insert into tb_user......");}
}

1.3 service

/*** 业务层实现类*/
public class UserServiceImpl implements UserService {private UserDao userDao;public void addUser(){userDao.addUser();}
}

2 IOC

2.1 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd "><!-- 告知spring框架,在读取配置文件,创建容器时扫描包,依据注解创建对象,并存入容器中 --><context:component-scan base-package="com.wt"></context:component-scan>
</beans>

2.2 dao

@Repository
public class UserDaoImpl implements UserDao {... ...
}

2.3 service

@Service
public class UserServiceImpl implements UserService {... ...
}

3 DI

3.1 service

@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;public void addUser() {userDao.addUser();}
}

3.2 测试

/*** 模拟表现层*/
public class Client {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = ac.getBean("userServiceImpl",UserService.class);userService.addUser();}
}

3 常用注解

3.1 用于创建对象的

以下四个注解的作用及属性都是一模一样的,都是针对一个的衍生注解只不过是提供了更加明确的语义化。

3.1.1 @Controller
  • 作用:

    把资源交给spring来管理,相当于:<bean id="" class="">;一般用于表现层。

  • 属性:

    value:指定bean的id;如果不指定value属性,默认bean的id是当前类的类名,首字母小写;

3.1.2 @Service
  • 作用:

    把资源交给spring来管理,相当于:<bean id="" class="">;一般用于业务层。

  • 属性:

    value:指定bean的id;如果不指定value属性,默认bean的id是当前类的类名,首字母小写;

  • 案例

    //@Service("userService")声明bean,且id="userServiceImpl"
    @Service//声明bean,且id="userServiceImpl"
    public class UserServiceImpl implements UserService {...   
    }
    
3.1.3 @Repository
  • 作用:

    把资源交给spring来管理,相当于:<bean id="" class="">;一般用于持久层。

  • 属性:

    value:指定bean的id;如果不指定value属性,默认bean的id是当前类的类名,首字母小写;

  • 案例

    //@Repository("userDaoImpl")声明bean,且id="userDaoImpl"
    @Repository//声明bean,且id="userDaoImpl"
    public class UserDaoImpl implements UserDao {@Overridepublic void addUser(){System.out.println("insert into tb_user......");}
    }
    
3.1.4 @Component
  • 作用:

    把资源交给spring来管理,相当于:<bean id="" class="">;通用。

  • 属性:

    value:指定bean的id;如果不指定value属性,默认bean的id是当前类的类名,首字母小写;

3.1.5 @Scope
  • 作用:

    指定bean的作用域范围。

  • 属性:

    value:指定范围的值,singleton prototype request session。

3.2 用于属性注入的

以下四个注解的作用相当于:<property name="" ref="">

3.2.1 @Autowired
  • 作用:

    自动按照类型注入。set方法可以省略。

  • 案例:

    @Service
    public class UserServiceImpl implements UserService {@Autowired //注入类型为UserDAO的beanprivate UserDao userDao;public void addUser(){userDao.addUser();}
    }
    
3.2.2 @Resource
  • 作用:

    自动按照名字注入。set方法可以省略。

  • 属性:

​ name:指定bean的id。

  • 案例:

    @Service
    public class UserServiceImpl implements UserService {@Resource(name="userDaoImpl")//注入id=“userDaoImpl”的beanprivate UserDao userDao;public void addUser(){userDao.addUser();}
    }
    
3.2.3 @Value
  • 作用:

    注入基本数据类型和String类型数据的

  • 属性:

​ value:用于指定值

  • 案例一

    @Service
    public class UserServiceImpl implements UserService {@Resource(name="userDaoImpl") //注入id=“userDaoImpl”的beanprivate UserDao userDao;@Value("张三")//注入Stringprivate String name;@Value("18")//注入Integerprivate Integer age;public void addUser(){System.out.println(name+","+age);userDao.addUser();}
    }
    
  • 案例二

    1. 创建config.properties

      name=张三
      age=18
      
    2. 加载配置文件

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd "><!--加载config.properties--><context:property-placeholder location="config.properties"/><context:component-scan base-package="com.wt"></context:component-scan>
      </beans>
      
    3. 注入属性值

      @Service
      public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Value("${name}")//注入Stringprivate String name;@Value("${age}")//注入Integerprivate Integer age;public void addUser() {System.out.println(name+","+age);userDao.addUser();}
      }
      

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/320971.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

TP-GMM

Task-parameterized Gaussian mixture model (TP-GMM) 对于一组示教数据 ξ ∈ R D N \bm{ξ} ∈R^{DN} ξ∈RDN&#xff0c;从不同的坐标系去观测它 X t ( j ) A t , j − 1 ( ξ t − b t , j ) X^{(j)}_t\bm{A}^{-1}_{t,j}(\bm{ξ}_t-\bm{b}_{t,j}) Xt(j)​At,j−1​(ξ…

多通道病虫害分子检测仪-百科科普知识

在农业科技日新月异的今天&#xff0c;病虫害防治已经成为现代农业的重要一环。为了更精准、更快速地检测和防治病虫害&#xff0c;多通道病虫害分子检测仪应运而生&#xff0c;成为守护绿色家园的"黑科技"。 WX-XC1多通道病虫害分子检测仪是一款集成了分子生物学、…

如何使用Cloudreve+Cpolar搭建个人PHP云盘系统并发布公网可访问

文章目录 1、前言2、本地网站搭建2.1 环境使用2.2 支持组件选择2.3 网页安装2.4 测试和使用2.5 问题解决 3、本地网页发布3.1 cpolar云端设置3.2 cpolar本地设置 4、公网访问测试5、结语 1、前言 自云存储概念兴起已经有段时间了&#xff0c;各互联网大厂也纷纷加入战局&#…

10.java初始化——(浏览阅读代码时的圣经)

初始化 类的初始化 上面我们创建出来了—个 Car 这个对象&#xff0c;其实在使用 new 关键字创建—个对象的时候&#xff0c;其实是调用了 这个对象无参数的构造方法进行的初始化&#xff0c;也就是如下这段代码 这个无参数的构造函数可以隐藏&#xff0c;由 JVM 自动添加。…

DoIP学习笔记系列:(八)车厂一般关于DoIP的相关测试分析

文章目录 1. 前言2. 基本项测试2.1 协议版本默认值2.2 车辆标识请求报文格式2.3 带EID的车辆标识请求报文格式2.4 带VIN的车辆标识请求报文格式2.5 否定响应码0x002.6 否定响应码0x012.7 否定响应码0x022.8 否定响应码0x042.9 路由激活应答码0x002.10 路由激活应答码0x012.11 路…

写了个在线 SQL 转换工具,支持 Oracle、Mysql、SQLServer 语句互转。

原本用户公司要迁移 oracle 到 mysql 上&#xff0c;数据库方言上有一定的区别&#xff0c;老的 SQL 又臭又长转起来也不太方便&#xff0c;尤其是日期类的完全无法适用&#xff0c;所以才写了这个工具&#xff1a;不同类型sql互转在线工具-开发者工具 可以用于不同数据库之间的…

CSS transition详解

文章目录 属性transition-propertytransition-durationtransition-timing-functiontransition-delaytransition 简写属性 方法Element&#xff1a;transitionrun 事件Element&#xff1a;transitionstart 事件Element&#xff1a;transitionend 事件Element&#xff1a;transit…

【AI】DETR模型可视化操作

Detr作为目标检测的算法&#xff0c;不同于之前算法的就是注意力机制&#xff0c;注意力机制能够直观看出来模型对图像关注的点&#xff0c;这个直观到底怎么直观呢&#xff0c;我们只听别人说肯定是不行的&#xff0c;上手测试才是最好的方式&#xff0c;像论文中插图那样的使…

静态网页设计——校园官网(HTML+CSS+JavaScript)

前言 声明&#xff1a;该文章只是做技术分享&#xff0c;若侵权请联系我删除。&#xff01;&#xff01; 使用技术&#xff1a;HTMLCSSJS 主要内容&#xff1a;对学校官网的结构进行模仿&#xff0c;对布局进行模仿。 主要内容 1、首页 首页以多个div对页面进行分割和布局…

用贪心算法编程求解任务安排问题

题目&#xff1a;用贪心算法编程求解以下任务安排问题 一个单位时间任务是恰好需要一个单位时间完成的任务。给定一个单位时间任务的有限集S。关于S的一个时间表用于描述S中单位时间任务的执行次序。时间表中第1个任务从时间0 开始执行直至时间1 结束&#xff0c;第2 个任务从时…

Python之安装和环境配置

python的下载 1.可以去python官网下载&#xff0c;https://www.python.org/ 2.下载完成后&#xff0c;安装即可。 python的检测 1.打开开始-运行-cmd&#xff08;快捷键winR&#xff09;。 如果是mac&#xff0c;打开使用工具-终端。 2.在终端里输入python&#xff0c;以下…

如何向嵌入式设备中添加tcpdump工具

说明&#xff1a;tcpdump是一个在网络设备调试中一个非常重要的工具&#xff0c;它并不像hexdump等工具集成在busybox里面&#xff0c;也不像其他的软件一样只需要依赖linux标准的库就可以实现&#xff0c;它需要pcap相关的库和加密的相关库。 本文主要是基于realtek 83系列的…