Spring:(五) 注解

一、前言

  1. 在spring4之后,想要使用注解形式,必须得要引入aop的包

  2. 在配置文件当中,还得要引入一个context约束

    <?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">
    
    </beans>
    

二、Bean的实现

  1. 配置扫描哪些包下的注解

    <!--指定注解扫描包-->
    <context:component-scan base-package="com.ry.pojo"/>
    
  2. 在指定包下编写类,增加注解

    @Component("user")
    // 相当于配置文件中 <bean id="user" class="当前注解的类"/>
    public class User {
        public String name = "路飞";
    }
    
  3. 测试

    @Test
    public void test(){
        ApplicationContext applicationContext =
            new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) applicationContext.getBean("user");
        System.out.println(user.name);
    }
    

三、属性注入

  1. 可以不用提供set方法,直接在直接名上添加@value("值")

    @Component("user")
    // 相当于配置文件中 <bean id="user" class="当前注解的类"/>
    public class User {
        @Value("路飞")
        // 相当于配置文件中 <property name="name" value="路飞"/>
        public String name;
    }
    
  2. 如果提供了set方法,在set方法上添加@value("值");

    @Component("user")
    public class User {
        public String name;
        @Value("秦疆")
        public void setName(String name) {
            this.name = name;
        }
    }
    

四、衍生注解

为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。

  • @Controller:web层
  • @Service:service层
  • @Repository:dao层

写上这些注解,就相当于将这个类交给Spring管理装配了!

五、作用域 @scope

  • singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
  • prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收

六、总结

  • XML可以适用任何场景 ,结构清晰,维护方便
  • xml与注解整合开发:xml管理Bean,注解完成属性注入,使用过程中, 可以不用扫描,扫描是为了类上的注解

七、基于Java类进行配置

  1. 实体类

    @Component  //将这个类标注为Spring的一个组件,放到容器中!
    public class Dog {
        public String name = "dog";
    }
    
  2. 编写Config配置类

    @Configuration  //代表这是一个配置类
    public class MyConfig {
    
        @Bean //通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id!
        public Dog dog(){
            return new Dog();
        }
    
    }
    
  3. 测试

    @Test
    public void test2(){
        ApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(MyConfig.class);
        Dog dog = (Dog) applicationContext.getBean("dog");
        System.out.println(dog.name);
    }
    

    导入其他配置

    @Configuration  //代表这是一个配置类
    public class MyConfig2 {
    }
    
    @Configuration
    @Import(MyConfig2.class)  //导入合并其他配置类,类似于配置文件中的 inculde 标签
    public class MyConfig {
    
        @Bean
        public Dog dog(){
            return new Dog();
        }
    
    }
    
原文地址:https://www.cnblogs.com/dreamzone/p/12402816.html