Spring5—Spring开发的其他两种方式

1.使用注解开发

  • 使用注解开发,就必须要导入aop的包,

在这里插入图片描述

  • 同时要导入context约束,增加注解的支持

需要添加四条到原始的配置文件:

  • xmlns:context=“http://www.springframework.org/schema/context”
  • http://www.springframework.org/schema/context
  • https://www.springframework.org/schema/context/spring-context.xsd
  • context:annotation-config/
<?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/>
    
</beans>

再通过下面的方式让注解生效:

<!--    指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="包的全类名"/>

1.代替bean:@Component

//等价于<bean id = "user" class="pojo.user"/>
//@Component组件
@Component
public class User {
    public String name ;
}

2.属性如何注入:@Value(“xxx”)

//等价于<bean id = "user" class="pojo.user"/>
//@Component组件
@Component
public class User {
    //等价于<property name="name" value="小胖"/>
    @Value("小胖")
    public String name ;
}

3.衍生的注解

  • @Component有几个衍生注解,我们web开发中,会按照mvc三层架构分层
    • dao层【@Repository】
    • service层【@Service】
    • controller层【@Controller】
  • 这四个注解功能是一样的,都是将某个类注册到Spring容器中,装配Bean

4.自动装配配置**@Autowired**

5.作用域**@Scope(“xxx”)**

//代替了<bean id="user" class="pojo.User" scope="prototype"/>
@Scope("prototype")
@Component
public class User {
    @Value("小胖")
    public String name ;
}

总结:

  • xml与注解

    • xml更加万能,适用于任何场合
    • 注解,如果不是自己的类使用不了,维护相对复杂
  • xml与注解最佳实践

    • xml用来管理bean
    • 注解只完成属性注入
    • 使用过程中必须要注意:要想注解生效,就必须开启注解的支持
    <context:annotation-config/>
    <context:component-scan base-package="pojo"/>
    

2.使用Java的方式配置Spring

完全不使用Spring的xml来配置,全部由java来做

JavaConfig是Spring的一个子项目,

1.编写实体类User

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

public class User {
    private String name;

    public String getName() {
        return name;
    }

    @Value("小胖")//属性注入值
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

2.编写配置类

//这个也会被Spring容器托管,注册到容器中,因为他本来也算是个@Component,
// @Configuration代表一个配置类,相当于是以前的bean.xml
@Configuration
//扫描包让包里的注解生效
@ComponentScan("pojo")
//这个标签可以导入另外一个配置类进行合并
@Import(Config02.class)
public class Config01 {

    //注册一个bean,就相当于我们之前写的一个bean标签
    //这个方法的名字,相当于bean的id属性
    //这个方法的返回值,相当于bean标签中class属性
    @Bean
    public User getUser(){
        return new User();//就是返回注入到bean的对象
    }
}

3.测试:

public class mytest {
    @Test
    public void test01(){
        //如果完全使用配置类方式去做,就只能通过AnnotationConfig上下文来获取容器,通过配置类的class类对象加载
        ApplicationContext context = new AnnotationConfigApplicationContext(Config01.class);
        User getuser = (User) context.getBean("getUser");

        System.out.println(getuser.getName());
    }
}

这种纯java的配置方式,在SpringBoot中随处可见

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值