spring —— IoC容器(二)

打印 上一主题 下一主题

主题 517|帖子 517|积分 1551

二、基于注解管理 bean

(一)Autowired 主动装配

(1)属性上主动装配

  1. package com.fourth.anno;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class Dao {
  5.     public void runDao(){
  6.         System.out.println("hello world");
  7.     }
  8. }
复制代码
  1. package com.fourth.anno;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class Service {
  6.     @Autowired
  7.     private Dao dao;
  8.     public void runService(){
  9.         dao.runDao();
  10.     }
  11. }
复制代码
  1. package com.fourth.anno;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test {
  5.     public static void main(String[] args) {
  6.         ApplicationContext context = new ClassPathXmlApplicationContext("bean-anno.xml");
  7.         Service service = context.getBean(Service.class);
  8.         service.runService();
  9.     }
  10. }
复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:context="http://www.springframework.org/schema/context"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="
  6.        http://www.springframework.org/schema/context
  7.        http://www.springframework.org/schema/context/spring-context.xsd
  8.        http://www.springframework.org/schema/beans
  9.        http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.     <context:component-scan base-package="com.fourth.anno"></context:component-scan>
  11. </beans>
复制代码
(2)set 方法上主动装配 

  1. package com.fourth.anno;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class Service {   
  6.     private Dao dao;
  7.     @Autowired
  8.     public void setDao(Dao dao){
  9.         this.dao=dao;
  10.     }
  11.     public void runService(){
  12.         dao.runDao();
  13.     }
  14. }
复制代码
(3)构造方法上主动装配 

  1. package com.fourth.anno;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class Service {
  6.     private Dao dao;
  7.     @Autowired
  8.     public Service(Dao dao) {
  9.         this.dao = dao;
  10.     }
  11.     public void runService(){
  12.         dao.runDao();
  13.     }
  14. }
复制代码
(4)构造方法的形参上主动装配 

  1. package com.fourth.anno;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class Service {
  6.     private Dao dao;
  7.     public Service(@Autowired Dao dao) {
  8.         this.dao = dao;
  9.     }
  10.     public void runService(){
  11.         dao.runDao();
  12.     }
  13. }
复制代码
 (二)Resource 主动装配

(1)属性上主动装配

  1. package com.fourth.anno;
  2. import jakarta.annotation.Resource;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class Service {
  6.     @Resource
  7.     private Dao dao;
  8.     public void runService(){
  9.         dao.runDao();
  10.     }
  11. }
复制代码
(2)set 方法上主动装配 

  1. package com.fourth.anno;
  2. import jakarta.annotation.Resource;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class Service {
  6.     private Dao dao;
  7.     @Resource
  8.     public void setDao(Dao dao) {
  9.         this.dao = dao;
  10.     }
  11.     public void runService(){
  12.         dao.runDao();
  13.     }
  14. }
复制代码
 Autowired 与 Resource 比较:

Autowired 可以应用在属性、set 方法、构造方法、构造方法的形参上;其默认通过范例进行装配。
Resource 可以应用在属性、set 方法上;其默认通过名称进行装配。
 Autowired 基于 spring 框架;Resource 基于 java 扩展包,假如 JDK 版本高于11或低于8,应引入以下依赖:
  1. <strong><dependency>
  2.     <groupId>jakarta.annotation</groupId>
  3.     <artifactId>jakarta.annotation-api</artifactId>
  4.     <version>2.1.1</version>
  5. </dependency></strong>
复制代码
(三)全注解开发

全注解开发,纵然用配置类代替 xml 配置文件:
  1. package com.fourth.anno;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class Dao {
  5.     public void runDao(){
  6.         System.out.println("hello world");
  7.     }
  8. }
复制代码
  1. package com.fourth.anno;
  2. import jakarta.annotation.Resource;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class Service {
  6.     private Dao dao;
  7.     @Resource
  8.     public void setDao(Dao dao) {
  9.         this.dao = dao;
  10.     }
  11.     public void runService(){
  12.         dao.runDao();
  13.     }
  14. }
复制代码
  1. package com.fourth.anno;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. @ComponentScan(value = "com.fourth.anno")
  6. public class SpringConfig {
  7. }
复制代码
  1. package com.fourth.anno;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. public class Test {
  5.     public static void main(String[] args) {
  6.         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
  7.         Service service = context.getBean(Service.class);
  8.         service.runService();
  9.     }
  10. }
复制代码
 
 
 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我爱普洱茶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表