(三) Spring 注解注入

 

目录

一、@Component

二、3个@Component注解衍生注解

三、@Autowired:自动根据类型注入

四、@Scope("prototype")     多例、单例的配置(默认singleton单例)


 

首先简单说明一下什么是注解注入,回顾之前我们的案例,不管是装配对象(Bean),还是注入对象(Bean)都需要在xml配置文件中写大量的

<!--配置一个bean对象-->
<bean id="userService" class="com.xyl.service.UserServiceImpl">
    <property name="name" value="xyl"></property>
</bean>

这种配置,那么如果我们使用注解注入,xml文件中就不需要再重复配置这些bean了,直接几行代码就搞定,想想就舒服多了。注解的书写通常是 @xxxx 写在属性、方法、类上面。

下面开始介绍常用的spring注解

一、@Component

泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于action,service,dao),我们就可以使用@Component来标注这个类。

下面我们来举例具体用途。

首先创建一个演示类 Demonstration.class,里面就只有一个添加方法

package com.xyl.spring.test;

public class Demonstration {
    public void add(){
        System.out.println("这是一个添加方法");
    }
}

如果我们之前不使用注解的时候,xml配置文件就得写一个

<bean id="demonstration" class="com.xyl.spring.test.Demonstration"/>

如果我们又要获取另一个对象xxx,就得又配置一个bean,然后各种+++

现在使用@Component就相当于我们在xml配置文件中配置了

@Component   相当于:

<bean class="com.xyl.spring.test.Demonstration"/> (没有id)

@Component("demonstration")  相当于:

<bean id="demonstration" class="com.xyl.spring.test.Demonstration"/> (有id)

 下面使用注解@Component

在Demonstration类中加上注解

package com.xyl.spring.test;

@Component
public class Demonstration {
    public void add(){
        System.out.println("这是一个添加方法");
    }
}

 然后在测试代码中获取Demonstration对象,调用add方法

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans-info.xml");
        Demonstration demonstration = context.getBean(Demonstration.class);
        demonstration.add();
    }

 注意:现在注解还不起作用。最后我们需要修改一下beans-info.xml配置文件

1.添加beans属性:

xmlns:context="http://www.springframework.org/schema/context"

2.修改beans属性

xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd"

3.开启注解,设置扫描注解地址

<!--开启注解-->
<context:annotation-config/>
<!--注解的位置-->
<context:component-scan base-package="com.xyl.spring"/>

 beans-info.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
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解-->
    <context:annotation-config/>
    <!--注解的位置-->
    <context:component-scan base-package="com.xyl.spring"/>
</beans>

运行结果:

配置完后,以后我们使用对象就可以直接添加注解获取就可以了,不用再去修改xml文件

 

二、3个@Component注解衍生注解

@Repository(“名称”):dao层

@Service(“名称”):service层

@Controller(“名称”):web层

功能一样是取代<beans class=" " />,但是与Component注解不同的是,这三个衍生注解是标注在特定的分层;

举个栗子:

我们平时页面调用action方法程序的流程是   (web)action——>service——>dao

如果所有层都用Component注解,那么spring容器将不知道先创建哪一层的对象。所以需要这三个衍生注解来区分

结构如图,那么三个衍生标签的使用如下

 

 

 用不同注解注释不同层;

三、@Autowired:自动根据类型注入

这里我们申明一个对象,但是不实例化对象。然后在声明对象上面加上@Autowired注解,spring容器将自动帮我们根据类型实例化注入。

举个栗子:我们就拿action中调用service方法举例

那么,我们只要在userService上面加上@Autowired注解,spring容器就会自己去帮我们找到UserService这个对象并帮我们实例化

 

四、@Scope("prototype")     多例、单例的配置(默认singleton单例)

被@Scope("prototype")注解标注的对象会被设置成多例模式,每次调用都将 new一个新对象

默认是@Scope("singleton")单例模式,默认需要使用该注解

写的spring系列:Spring框架完全掌握 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值