【Spring】使用注解(Annotation)进行开发

一、使用注解和xml组合开发

创建并配置applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描指定的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.sdpei"/>
    <context:annotation-config />

</beans>

1.使用注解取代原本的中的实体类的注入

在实体类的上面添加@Component :作用相当于< bean id ="…" class="…"></ bean>
在这里插入图片描述

2.属性的注入

在非实体类的 “属性上” 或 “对应的set方法上” 面加上@Value("…")
在这里插入图片描述

3.引用类型属性的注入

使用xml文件实现自动装配;使用注解@Autowired和@Resource实现自动装配【点击查看】中已经讲过这一块内容。

在引用类型的属性的上面加上@Autowired@Resource

4.衍生注解

在javawe中,我们会遵循MVC三层架构,一般会存在dao\ pojo\ sercice\ controller层,不同层的类的注入方式是不同的,但是其作用都是相同的------交给spring容器托管。不需要再在XXX.xml文件中进行bean 的托管了。

在dao层我们使用@Repository: 其是在@Component上派生的,所有它有@Component的特性,实现将该类注入到spring容器中。也有自己独有的特点(如:类中抛出的数据访问异常封装为 Spring 的数据访问异常类型)
在这里插入图片描述


在pojo层我们使用@Component: 实现将该类注入到spring容器中。
在这里插入图片描述


在service层我们使用@Service: 其也是实现自动注入的,也是@Component的一个衍生注解,实现将该类注入到spring容器中。
在这里插入图片描述


在controller层我们使用@Controller: 其也是实现自动注入的,也是@Component的一个衍生注解,实现将该类注入到spring容器中。
在这里插入图片描述

5.自动装配

使用xml文件实现自动装配;使用注解@Autowired和@Resource实现自动装配【点击查看】中已经讲过这一块内容。

@Autowired:通过byType进行装配.

@Autowired(required = false):表示被注解标记的对象可以为null.

@Autowired+@Qualilier(value="xxx "):value为bean的id.

@Resource:先通过byName进行装配,再通过byType进行装配.

@Resource(value=" xxx"):value为bean的id.

6.作用域

前面我们使用过< bean id="" class="" scope=“singleton/prototype”>实现的作用域
我们可以可直接在对象的类的上面使用@Scope(“singleton”)/@Scope(“prototype”)来实现作用域的设置

在这里插入图片描述

注意

在xml文件中一定要加上下面的配置

    <context:component-scan base-package="com.sdpei"/>
    <context:annotation-config />

上面的两个标签的目的是使com.sdper下的类中的相关的注解生效


测试

    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserPojo userPojo = context.getBean("userPojo", UserPojo.class);
        UserDao userDao = context.getBean("userDao", UserDao.class);
        UserService userService = context.getBean("userService", UserService.class);
        UserController userController = context.getBean("userController", UserController.class);

        System.out.println(userPojo.toString());
        userDao.getDao();
        userService.getService();
        userController.getController();
    }

在这里插入图片描述

二、完全使用注解开发

从(一)中我们可以发现,我们还是使用到了xml文件。下面我们介绍一下如何摆脱xml文件。

1. 使用JavaConfig实现配置

在spring中,使用java取代xml文件的功能。我需要使用到两个注解@Configuration@Bean

@Configuration
class public HJ(){
	@Bean
	public User getUser(){
		return new User();
	}
}

@Bean的作用相等于< bean id=“getUser” class="… …/User" />


例如:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述


2. 将多个“@Configuration类”融合在一起

我们知道,在spring中是可以有多个xml文件的,如果我们使用注解取代xml,怎么将多个@Configuration类融合在一起呢?
一个@Configuration类中使用@import(类名.class)多个@Configuration类进入一个@Configuration类中。

这样做的好处是,我们通过一个@Configuration类就可以获取到任意一个bean对象

例如:
在这里插入图片描述


测试

    @Test
    public void test2(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        UserDao userDao = context.getBean("getUserDao", UserDao.class);
        UserPojo userPojo = context.getBean("getUserPojo", UserPojo.class);
        UserController userController = context.getBean("getUserController", UserController.class);
        UserService userService = context.getBean("getUserService", UserService.class);
        userController.getController();
        userDao.getDao();
        System.out.println(userPojo.toString());
        userService.getService();


    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值