栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring IoC 自动装载 autowire、 基于注解的开发

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring IoC 自动装载 autowire、 基于注解的开发

Spring IoC 自动装载 autowire

自动装载是 Spring 提供的⼀种更加简便的⽅式来完成 DI,不需要手动配置 property,IoC 容器会自动选择 bean 完成注⼊。

自动装载有两种⽅式:
byName,通过属性名完成自动装载。
byType,通过属性对应的数据类型完成⾃动装载。

byName 的操作如下所示。
1、创建 Person 实体类。

@Data
public class Person {
 private Integer Id;
 private String name;
 private Car car; }

2、在 spring.xml 中配置 Car 和 Person 对应的 bean,并且通过自动装载完成依赖注入。


 
 
 
 
 

byType 的操作如下所示。


 
 
 
 
 

使⽤ byType 进⾏⾃动装载时,必须保证 IoC 中只有⼀个符合条件的 bean,否则会抛出异常

Spring IoC 基于注解的开发

Spring IoC 的作⽤是帮助开发者创建项目中所需要的 bean,同时完成 bean 之间的依赖注⼊关系,DI。
实现该功能有两种方式:
1、基于 XML 配置。
2、基于注解。
基于注解有两步操作,缺⼀不可:
1、配置自动扫包。



2、添加注解。

@Data
@Component
public class Repository {
 private DataSource dataSource; }

DI:DI 指 bean 之间的依赖注⼊,设置对象之间的级联关系

@Data
@Component
public class DataSource {
 private String user;
 private String password;
 private String url;
 private String driverName; }
@Data
@Component(value = "myrepo")
public class Repository {
 @Autowired
 private DataSource dataSource; }

@Autowired 默认是通过 byType 进行注入的,如果要改为 byName,需要配置 @Qualifier 注解来完成

@Autowired
@Qualifier(value = "ds")
private DataSource dataSource;

表示将 IoC 中 id 为 ds 的 bean 注⼊到 repository 中。
实体类中普通的成员变量(String、包装类等)可以通过 @Value 注解进⾏赋值。

@Data
@Component(value = "ds")
public class DataSource {
 @Value("root")
 private String user;
 @Value("root")
 private String password;
 @Value("jdbc:mysql://localhost:3308/library")
 private String url;
 @Value("com.mysql.cj.Driver")
 private String driverName; }

等同于 spring.xml


 
 
 
 

实际开发的使用

实际开发中我们会将程序分为三层:
Controller
Service
Repository(DAO)
关系 Controller -->Service -->Repository

@Component 注解是将标注的类加载到 IoC 容器中,实际开发中可以根据业务需求分别使用@Controller、@Service、@Repository 注解来标注控制层类、业务层类、持久层类。

Controller

package com.southwind.controller;
import com.southwind.service.MyService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Setter
@Controller
public class MyController {
 @Autowired
 private MyService myService;
 
 public String service(Double score){
 return myService.doService(score);
 }
}

Service

package com.southwind.service;
public interface MyService {
 public String doService(Double score);
}
package com.southwind.service.impl;
import com.southwind.repository.MyRepository;
import com.southwind.service.MyService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Setter
@Service
public class MyServiceImpl implements MyService {
 @Autowired
 private MyRepository myRepository;
 @Override
 public String doService(Double score) {
 return myRepository.doRepository(score);
 }
}

Repository

package com.southwind.repository;
public interface MyRepository {
 public String doRepository(Double score);
}
package com.southwind.repository.impl;
import com.southwind.repository.MyRepository;
import org.springframework.stereotype.Repository;
@Repository
public class MyRepositoryImpl implements MyRepository {
 @Override
 public String doRepository(Double score) {
 String result = "";
 if(score < 60){
 result = "不及格";
 }
 if(score >= 60 && score < 80){
 result = "合格";
 }
 if(score >= 80){
 result = "优秀";
 }
 return result;
 }
}

spring.xml


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842617.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号