JAVA-5-[Spring框架]基于注解方式的Bean管理

参考什么是spring,它能够做什么?
参考w3cschool
参考Spring框架入门教程

1 基于注解的装配

在 Spring 中,尽管可以使用XML配置文件实现Bean的装配工作,但如果应用中Bean的数量较多,会导致XML配置文件过于臃肿,从而给维护和升级带来一定的困难。

Java从JDK 5.0以后,提供了Annotation(注解)功能,Spring 2.5版本开始也提供了对Annotation技术的全面支持,我们可以使用注解来配置依赖注入。

Spring默认不使用注解装配Bean,
因此需要在配置文件中添加<context:annotation-config/>,启用注解。

Spring中常用的注解如下。
1)@Component
可以使用此注解描述Spring中的Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),
并且可以作用在任何层次。使用时只需将该注解标注在相应类上即可。

2)@Repository
用于将数据访问层(DAO层)的类标识为Spring中的Bean,其功能与@Component相同。

3)@Service
通常作用在业务层(Service层),用于将业务层的类标识为Spring中的Bean,
其功能与@Component相同。

4)@Controller
通常作用在控制层(如Struts2的Action、SpringMVC的Controller),
用于将控制层的类标识为Spring中的Bean,其功能与@Component相同。

5)@Autowired
可以应用到Bean的属性变量、属性的setter方法、非setter方法及构造函数等,
配合对应的注解处理器完成Bean的自动配置工作。
默认按照Bean的类型进行装配。

6)@Resource
作用与Autowired相同,区别在于@Autowired默认按照Bean类型装配,
而@Resource默认按照Bean实例名称进行装配。

@Resource中有两个重要属性:name和type。
Spring将name属性解析为Bean的实例名称,type属性解析为Bean的实例类型。
如果指定name属性,则按实例名称进行装配;
如果指定type属性,则按Bean类型进行装配。
如果都不指定,则先按Bean实例名称装配,如果不能匹配,则再按照Bean类型进行装配;
如果都无法匹配,则抛出NoSuchBeanDefinitionException异常。

7)@Qualifier
与@Autowired注解配合使用,会将默认的按Bean类型装配修改为按Bean的实例名称装配,
Bean的实例名称由@Qualifier注解的参数指定。

2 基于注解方式

1、什么是注解
(1)注解是代码特殊标记
格式:@注解名称(属性名称=属性值,属性名称=属性值,...)
(2)使用注解,注解作用在类上面、方法上面、属性上面。
(3)使用注解目的:简化xml配置。

2、spring针对Bean管理中创建对象提供注解
(1)@Component
(2)@Service
(3)@Controller
(4)@repository
上面四个注解功能是一样的,都可以用来创建Bean实例。

3、基于注解方式实现对象创建
第一步 引入依赖spring-aop
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
第二步 开启组件扫描
第三步 创建类,在类上面添加创建对象注解

2.1 开启组件扫描

开启组件扫描的细节配置:
(1)示例一
use-default-filters="false" 表示现在不使用默认filter,自己配置filter
context:include-filter设置扫描哪些内容。

<context:component-scan base-package="dao,service" use-default-filters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
(2)示例二
下面设置扫描包所有内容
context:exclude-filter:设置哪些内容不进行扫描。

<context:component-scan base-package="dao,service">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

例如:

<?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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--开启组件扫描
        1、如果扫描多个包,多个包使用逗号隔开
        2、扫描包上层目录
    -->
    <context:component-scan base-package="dao,service"/>

</beans>

2.2 对象创建

一、UserService.java

package service;

import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写,
//默认值是类名称的首字母小写
//UserService == userService
@Component(value = "userService")  //等价于<bean id="userService" class="..."/>
public class UserService {
    
    
    public void add(){
    
    
        System.out.println("service add ......");
    }
}

二、测试

package net.biancheng;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
public class MainApp {
    
    
    public static void main(String[] args) {
    
    
        // 1 加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        // 2 获取配置创建的对象
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

2.3 属性注入

(1)@Autowired:根据类型进行自动装配【用的最多】
第一步 把service和dao对象创建,在service和dao类添加创建对象注解
第二步 在service注入dao对象,在service类添加dao类型属性,在属性上面使用注解。

(2)@Qualifier:根据名称进行注入
这个@Qualifier注解的使用,和上面@Autowired一起使用

(3)@Resource:可以根据类型注入,可以根据名称注入
//@Resource //根据类型注入
//@Resource(name="userDaoImpl1") // 根据名称注入

(4)@Value:注入普通类型属性

2.3.1 @Autowired根据类型注入

一、UserService.java

package service;

import dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写,
//默认值是类名称的首字母小写
//UserService == userService
@Component //等价于<bean id="userService" class="..."/>
public class UserService {
    
    
    // 定义dao类型属性,不需要添加set方法
    // 添加注入属性的注解
    @Autowired
    private UserDao userDao1;

    public void add(){
    
    
        System.out.println("service add ......");
        userDao1.add();
    }
}

二、UserDao.java接口

package dao;

public interface UserDao {
    
    
    public void add();
}

三、UserDaoImpl.java接口实现类

package dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao{
    
    
    public void add() {
    
    
        System.out.println("dao add ....");
    }
}

四、MainApp.java测试

package net.biancheng;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
public class MainApp {
    
    
    public static void main(String[] args) {
    
    
        // 1 加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        // 2 获取配置创建的对象
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

2.3.2 @Qualifier根据名称注入

一、UserService.java

package service;

import dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写,
//默认值是类名称的首字母小写
//UserService == userService
@Component //等价于<bean id="userService" class="..."/>
public class UserService {
    
    
    // 定义dao类型属性,不需要添加set方法
    // 添加注入属性的注解
    @Autowired
    @Qualifier(value="userDaoImpl")
    private UserDao userDao1;

    public void add(){
    
    
        System.out.println("service add ......");
        userDao1.add();
    }
}

主要用于处理,一个接口有多个实现类,不知道该选择哪个的情况。
二、UserDao.java接口

package dao;

public interface UserDao {
    
    
    public void add();
}

三、UserDaoImpl.java接口实现类

package dao;

import org.springframework.stereotype.Repository;

@Repository(value="userDaoImpl1")
public class UserDaoImpl implements UserDao{
    
    
    public void add() {
    
    
        System.out.println("dao add ....");
    }
}

2.3.3 @Resource根据类型或名称注入

一、UserService.java

package service;

import dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//在注解里面value属性值可以省略不写,
//默认值是类名称的首字母小写
//UserService == userService
@Component //等价于<bean id="userService" class="..."/>
public class UserService {
    
    
    // 定义dao类型属性,不需要添加set方法
    // 添加注入属性的注解
    //@Resource //根据类型注入
    @Resource(name="userDaoImpl1") // 根据名称注入
    private UserDao userDao1;

    public void add(){
    
    
        System.out.println("service add ......");
        userDao1.add();
    }
}

2.3.4 @Value注入普通类型属性

一、UserService.java

package service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class UserService {
    
    
    // 定义dao类型属性,不需要添加set方法
    // 添加注入属性的注解
    @Value(value="abc")
    private String name;

    public void add(){
    
    
        System.out.println("service add ......");
        System.out.println(name);
    }
}

3 完全注解开发

在实际中是使用spring boot进行实现。

(1)创建配置类,替代xml配置文件
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {
    
    "config,dao,service"})
public class SpringConfig {
    
    
}

(2)加载配置类

3.1 SpringConfig.java配置类

package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {
    
    "config,dao,service"})
public class SpringConfig {
    
    
}

3.2 MainApp.java测试

package net.biancheng;

import config.SpringConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import service.UserService;
public class MainApp {
    
    
    public static void main(String[] args) {
    
    
        // 1 加载配置类
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        // 2 获取配置创建的对象
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_20466211/article/details/129673867