二、基于注解管理 bean
(一)Autowired 主动装配
(1)属性上主动装配
- package com.fourth.anno;
- import org.springframework.stereotype.Component;
- @Component
- public class Dao {
- public void runDao(){
- System.out.println("hello world");
- }
- }
复制代码- package com.fourth.anno;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- @Component
- public class Service {
- @Autowired
- private Dao dao;
- public void runService(){
- dao.runDao();
- }
- }
复制代码- package com.fourth.anno;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("bean-anno.xml");
- Service service = context.getBean(Service.class);
- service.runService();
- }
- }
复制代码- <?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/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
- <context:component-scan base-package="com.fourth.anno"></context:component-scan>
- </beans>
复制代码 (2)set 方法上主动装配
- package com.fourth.anno;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- @Component
- public class Service {
- private Dao dao;
- @Autowired
- public void setDao(Dao dao){
- this.dao=dao;
- }
- public void runService(){
- dao.runDao();
- }
- }
复制代码 (3)构造方法上主动装配
- package com.fourth.anno;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- @Component
- public class Service {
- private Dao dao;
- @Autowired
- public Service(Dao dao) {
- this.dao = dao;
- }
- public void runService(){
- dao.runDao();
- }
- }
复制代码 (4)构造方法的形参上主动装配
- package com.fourth.anno;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- @Component
- public class Service {
- private Dao dao;
- public Service(@Autowired Dao dao) {
- this.dao = dao;
- }
- public void runService(){
- dao.runDao();
- }
- }
复制代码 (二)Resource 主动装配
(1)属性上主动装配
- package com.fourth.anno;
- import jakarta.annotation.Resource;
- import org.springframework.stereotype.Component;
- @Component
- public class Service {
- @Resource
- private Dao dao;
- public void runService(){
- dao.runDao();
- }
- }
复制代码 (2)set 方法上主动装配
- package com.fourth.anno;
- import jakarta.annotation.Resource;
- import org.springframework.stereotype.Component;
- @Component
- public class Service {
- private Dao dao;
- @Resource
- public void setDao(Dao dao) {
- this.dao = dao;
- }
- public void runService(){
- dao.runDao();
- }
- }
复制代码 Autowired 与 Resource 比较:
Autowired 可以应用在属性、set 方法、构造方法、构造方法的形参上;其默认通过范例进行装配。
Resource 可以应用在属性、set 方法上;其默认通过名称进行装配。
Autowired 基于 spring 框架;Resource 基于 java 扩展包,假如 JDK 版本高于11或低于8,应引入以下依赖:
- <strong><dependency>
- <groupId>jakarta.annotation</groupId>
- <artifactId>jakarta.annotation-api</artifactId>
- <version>2.1.1</version>
- </dependency></strong>
复制代码 (三)全注解开发
全注解开发,纵然用配置类代替 xml 配置文件:
- package com.fourth.anno;
- import org.springframework.stereotype.Component;
- @Component
- public class Dao {
- public void runDao(){
- System.out.println("hello world");
- }
- }
复制代码- package com.fourth.anno;
- import jakarta.annotation.Resource;
- import org.springframework.stereotype.Component;
- @Component
- public class Service {
- private Dao dao;
- @Resource
- public void setDao(Dao dao) {
- this.dao = dao;
- }
- public void runService(){
- dao.runDao();
- }
- }
复制代码- package com.fourth.anno;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- @ComponentScan(value = "com.fourth.anno")
- public class SpringConfig {
- }
复制代码- package com.fourth.anno;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Test {
- public static void main(String[] args) {
- ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
- Service service = context.getBean(Service.class);
- service.runService();
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |