10-使用注解开发

使用注解开发

依然使用xml配置文件来管理

项目地址

spring4之后,要使用注解开发,必须保证aop的包导入了

image-20210801135559755

使用注解开发,要导入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
        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:annotation-config/>
<!--    指定要扫描的包,即在该包下的注解生效-->
    <context:component-scan base-package="com.ajream.pojo"/>
    
</beans>
  1. bean

    @component//组件,在创建的类前面加上这个注解,说明这个类已经被spring管理了,相当于:
        //<bean id="person" class="com.ajream.pojo.Person">
        //没有参数时,只能以getBean("类名的小写字母") 来获取bean
    @component(value="xxx")
        //有参数时,可以用getBean("xxx") 来获取bean
    

    Person类:

    package com.ajream.pojo;
    
    import org.springframework.stereotype.Component;
    
    @Component				//没有参数时,只能以getBean("person") 来获取bean
    public class Person {
        public String name = "张三";
    }
    
    
  2. 属性注入

  3. @Value("李四")//相当于<property name="name" value="李四" />
    
    @Component
    public class Person {
        
        @Value("李四")		//注入属性值
        public String name;
    }
    

    也可以在setter方法前使用:

    @Component
    public class Person {
        public String name;
    
        @Value("李四")
        public void setName(String name) {
            this.name = name;
        }
    }
    
  4. 衍生注解

    @Component有几个衍生注解,在web中一般按照mvc三层架构划分:

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

    这4个注解功能一样的

  5. 作用域scope

    单例

    @Component
    @Scope(value = "singleton")    
    public class Person {
        public String name;
    
        @Value("李四")
        public void setName(String name) {
            this.name = name;
        }
    }
    

    原型

    @Scope(value = "prototype")  
    
  6. 小结:

    xml与注解

    • xml:万能

    • 注解:不是自己的类用不了,维护相对复杂

    xml与注解最佳实践:

    • xml用来管理bean

    • 注解只负责属性注入

    • 注意:要让注解生效,必须开启注解的支持

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

使用Java的方式配置spring

项目地址

在此前一直都使用beans.xml配置文件来配置spring如属性注入,现在可以不使用xml了,将spring配置全权交由Java来配置

用MyConfig类来代替beans.xml:

package com.ajream.config;

import com.ajream.pojo.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

    @Bean
    //相当于<bean id="getPerson1" class="com.ajream.pojo.Person">
    public Person getPerson1(){         //方法名就是bean的id
        return new Person();
    }
}

Person类:

package com.ajream.pojo;

import org.springframework.beans.factory.annotation.Value;

public class Person {
    public String name;

    @Value("李四")
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

MyTest类

image-20210801151413775

这里不使用xml配置文件了,使用Java配置类 MyConfig

import com.ajream.config.MyConfig;
import com.ajream.pojo.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class MyTest {

    @Test
    public void test1() {
        //加载配置类
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);

        Person p = context.getBean("getPerson1", Person.class);

        System.out.println(p.getName());

    }
}

由于MyConfig类也是配置类,所以它具有beans.xml的功能,比如:

image-20210801155244594

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[小G]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值