Spring注解的方式来创建对象

本文介绍Spring框架中注解的基本用法,包括注解的概念、使用步骤及如何通过注解来替代XML配置文件创建和管理Bean。同时,还探讨了不同类型的注解如@Component、@Controller等,并解释了它们在不同层的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring的bean管理(注解)

1. 什么是注解

  1. 代码里面的一些特殊的标记。我们使用注解也可以直接来完成一些相关的功能。

  2. 注解的写法是@注解名称(属性名称=属性值)

  3. 注解可以用在类、方法、属性。

2. Spring注解开发的基本的准备工作

注解可以替代配置文件,但是不能完全脱离配置文件,只是能够减少配置。

  1. 导入jar包,就是导入spring最基本的几个jar包。

  2. 导入aop的jar包,为了使用注解。

  3. 创建一个类,在类当中,创建一个方法

  4. 创建spring的配置文件,在配置文件当中引入约束

<?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"> <!-- bean definitions here -->

</beans>
  1. 开启注解扫描
<?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"> 
        <!-- bean definitions here -->
    <!-- 开启注解的扫描 -->
    <context:component-scan base-package="cn.itcast.anno,cn.itcast.xmlanno"></context:component-scan>
    <context:component-scan base-package="cn.itcast"></context:component-scan>
    <context:component-scan base-package="cn"></context:component-scan>
</beans>
  1. 仅仅开启注解扫描当中的属性的注解扫描
<?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"> 
        <!-- bean definitions here -->
    <!-- 
        开启注解的扫描,扫描类,方法,属性上面的注解
    -->
    <context:component-scan base-package="cn.itcast.anno,cn.itcast.xmlanno"></context:component-scan>
    <context:component-scan base-package="cn.itcast"></context:component-scan>
    <context:component-scan base-package="cn"></context:component-scan>

    <!-- 
        开启属性上面的扫描
     -->
    <context:annotation-config></context:annotation-config>
</beans>
2.1 注解创建对象
  1. 在创建对象的类上面使用注解就可以实现。

  2. 创建对象有四个注解。

@Conmponent
@Controller web层
@Service 业务层
@Repository 持久层

目前来说,这四个注解的功能都是一样的,为什么四个注解,就是为了后续的版本当中进行功能扩展的。

3.创建对象是单实例还是双实例

类的代码

package cn.itcast.anno;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.*;

@Service(value="user")  //<bean id="user" class="cn.itcast.anno.User">
@Scope(value="prototype") 
public class User {
    public void add(){
        System.out.println("add..............");
    }
}

配置文件

<?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"> 
        <!-- bean definitions here -->
    <!-- 
        开启注解的扫描,扫描类,方法,属性上面的注解
    -->
    <context:component-scan base-package="cn"></context:component-scan>
</beans>

测试代码

package cn.itcast.anno;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnno {

    @Test
    public void testUser(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        User user=(User)context.getBean("user");
        System.out.println(user);
        user.add();
    }
}

测试结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值