Spring常用的注解
1、声明Bean的注解
1)@Component
该注解是一个泛化的概念,仅仅表示一个组件对象(Bean),可以作用在任何层次上,没有明确的角色。
2)@Repository
该注解用于将数据访问层(Dao)的类标识为Bean,即注解数据访问层Bean,其功能与@Component()相同。
3)@Service
该注解用于标注一个业务逻辑组件类(Service层),其功能与@Component()相同。
4)@Controller
该注解用于标注一个控制组件类(Spring MVC的Controller),其功能与@Component()相同。
2、注入Bean的注解
1)@Autowired
该注解可以对类成员变量、方法以及构造方法进行标注,完成自动装配的工作。通过@Autowired的使用来消除setter和getter方法。默认按照Bean的类型进行装配。
2)@Resource
该注解与@Autowired功能一样。区别在于,该注解默认是按照名称来装配注入的,只有当找不到与名称相匹配的Bean时才会按照类型来装配注入;而@Autowired默认按照Bean的类型进行装配,如果想按照名称来装配注入,则需要结合@Qualifier注解一起使用。
@Resource注解由两个属性:name和type。name属性指定Bean实例名称,即按照名称来装配注入;type属性指定Bean类型,即按照Bean的类型进行装配。
3)@Qualifier
该注解与@Autowired注解配合使用。当@Autowired注解需要按照名称来装配注入时,则需要结合该注解一起使用,Bean的实例名称由@Qualifier注解的参数指定。
基于注解的以来注入的使用过程如下:
1、创建Spring工程
2、创建dao层
在该包下,创建TestDao接口和TestDaoImpl实现类,并将实现类使用@Repository注解标注为数据访问层。
TestDao的代码入下:
package annotation.dao;
/**
* create with IntelliJ IDEA
* create By Qing
* Date: 2020/11/19
* Time: 17:17
*/
public interface TestDao {
public void save();
}
TestDaoImpl的代码如下:
package annotation.dao;
import org.springframework.stereotype.Repository;
/**
* create with IntelliJ IDEA
* create By Qing
* Date: 2020/11/19
* Time: 17:18
*/
@Repository("testDao")
/*
相当于@Repository,但如果在service层使用@Resource(name="testDao")注入Bean,testDao不能省略。
*/
public class TestDaoImpl implements TestDao {
@Override
public void save(){
System.out.println("tsetDao save");
}
}
3、创建service层
在该包下创建TestService接口和TestServiceImpl实现类,并将实现类TestServiceImpl使用@Service注解标注为业务逻辑层。
TestService的代码如下:
package annotation.service;
/**
* create with IntelliJ IDEA
* create By Qing
* Date: 2020/11/19
* Time: 17:22
*/
public interface TestService {
public void save();
}
TestServiceImpl的代码如下:
package annotation.service;
import annotation.dao.TestDao;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* create with IntelliJ IDEA
* create By Qing
* Date: 2020/11/19
* Time: 17:23
*/
@Service("testService")
public class TestServiceImpl implements TestService {
@Resource(name="testDao")
/*相当于@Autowired,@Autowired默认按照Bean的类型注入 */
private TestDao testDao;
@Override
public void save(){
testDao.save();
System.out.println("testService save");
}
}
4、创建Controller层
在该包下创建TestController类,并将TestController类使用@Controller注解标注为控制器层。
TestController的代码如下
package annotation.controller;
import annotation.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* create with IntelliJ IDEA
* create By Qing
* Date: 2020/11/19
* Time: 17:27
*/
@Controller
public class TestController {
@Autowired
private TestService testService;
public void save(){
testService.save();
System.out.println("testController save");
}
}
5、创建配置类
不使用Spring的XML配置文件,而使用注解和Java配置。因此,在此需要使用@Configuration创建一个Java配置类(相当于一个Spring的XML配置文件),并通过@ComponentScan扫描使用注解的包(相当于在Spring的XML配置文件中使用<context:component-scan base-package=“Bean所在的包路径”/>语句)。在annotation包下创建名为ConfigAnnotation的配置类。
ConfigAnnotation的代码如下:
package annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* create with IntelliJ IDEA
* create By Qing
* Date: 2020/11/19
* Time: 17:31
*/
@Configuration//声明当前类时一个配置类,相当于一个Spring的XML配置文件
@ComponentScan("annotation")//自动扫描annotation包下使用的注解,并注册为Bean
/*
相当于在Spring的XML配置文件中使用<context:component-scan base-package="Bean所在的包路径"/>语句
*/
public class ConfigAnnotation {
}
6、创建测试类
在annotation包下,创建测试类TestAnnotation,具体代码如下:
package annotation;
import annotation.controller.TestController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* create with IntelliJ IDEA
* create By Qing
* Date: 2020/11/19
* Time: 17:35
*/
public class TestAnnotation {
public static void main(String[] args) {
//初始化Spring容器ApplicationContext
AnnotationConfigApplicationContext appCon=new AnnotationConfigApplicationContext(ConfigAnnotation.class);
TestController tc=appCon.getBean(TestController.class);
tc.save();
appCon.close();
}