Spring注解的方式来创建对象

Spring的bean管理(注解)

1. 什么是注解

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

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

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

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

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

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

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

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

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


<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"> 

beans>
  1. 开启注解扫描

<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: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. 仅仅开启注解扫描当中的属性的注解扫描

<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: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")  //
@Scope(value="prototype") 
public class User {
    public void add(){
        System.out.println("add..............");
    }
}

配置文件


<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: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();
    }
}

测试结果

Spring注解的方式来创建对象_第1张图片

你可能感兴趣的:(Java)