Spring-05注解开发 取消Bean的存在

用注解开发

说明

在spring4之后,想要使用注解形式,必须得要引入aop的包
在这里插入图片描述在配置文件当中,还得要引入一个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>

1、 Bean的实现

我们之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解!

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

<!--指定注解扫描包-->
<context:component-scan base-package="com.xuyuan.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);
}

2、属性如何注入

package com.xuyuan.Pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//等价于    <context:component-scan base-package="com.xuyuan.Pojo"/>
//@Component 组件
@Component

public class User {
    //相当于    <property name="name" value="xuyuan"/>
    @Value("xuyuan")
    public String 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;
    }
}

3、衍生的注解

@Component 有几个衍生注解,我们在web开发中 会按照MVC三层架构的方式分层;
。Dao----【@Repository】
。Service-----【@Service】 。
。 controller—【@Controller】
这四个注解功能都是一样的,都代表将某个类注册到Spring中 装配Bean

4、自动装配

@Autowired:自动装配 通过类型,名字 。如果@Autowired不能唯一自动装配上属性,则需简要通过
@Qualifier(Value=“×××”) @Nullable 字段标记了这个注解,说明这个字段可以为null @Resource
自动装配 通过名字 类型

5、作用域
singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。

prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
@Component
@Scope("singleton")

public class User {
    //相当于    <property name="name" value="xuyuan"/>
    @Value("xuyuan")
    public String name;
}

6、小结

XML与注解比较

XML可以适用任何场景 ,结构清晰,维护方便

注解不是自己提供的类使用不了,开发简单方便

xml与注解整合开发 :推荐最佳实践

xml管理Bean

注解完成属性注入

使用过程中, 可以不用扫描,扫描是为了类上的注解
 <context:component-scan base-package="com.xuyuan"/>
<context:annotation-config/>  

作用:

进行注解驱动注册,从而使注解生效

用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册

如果不扫描包,就需要手动配置bean

如果不加注解驱动,则注入的值为null!

使用java的方式配置Spring

我们现在完全不需要Spring的 xml配置了 全权交给 java来做!
javaConfig 是Spring的一个子项目 在Spring4 之后的新功能

在这里插入图片描述测试:

1、编写一个实体类,User
package com.xuyuan.Pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//这里这个注解的意思是,就是说明这个类被Spring接管了,注册到了容器中

@Component
public class User {
    public  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、新建一个config配置包,编写一个MyConfig配置类

package com.xuyuan.Config;
import com.xuyuan.Pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
//@Configuration  这个也会被Spring托管  因为身就是本身就是一个配置
// @Component//@Configuration代表这是一个配置类,就喝我们之前看到的beansxml一样的
@Configuration
@ComponentScan("com.xuyuan.Pojo")//包的扫描
@Import(Config2.class)//相当于bean中的import引入多个Beans。xml
public class config {
    //注册一个Bean就相当于我们之前写的一个bean标签
    //这个方法的名字 就相当于bean标签中的id属性值
    //这个方法的返回值 就相当于 bean标签中的Class属性值
    @Bean
    public User getuser(){
        return new User();//就是返回要注入到Bean中的对象
    }
}

3、测试

import com.xuyuan.Config.config;
import com.xuyuan.Pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class mytest {
    //如果完全使用了配置类方式去做,我们就只能通过AnnontationConfig 上下文来获取容器,通过配置类的Class对象加载!
    public static void main(String[] args) {
        ApplicationContext context  =  new AnnotationConfigApplicationContext(config.class);
           User user= (User) context.getBean("getuser");
        System.out.println(user.getName());
    }

}

4、成功输出结果!
导入其他配置如何做呢?

1、我们再编写一个配置类!

package com.xuyuan.Config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config2 {

}

2、在之前的配置类中我们来选择导入这个配置类

@ComponentScan("com.xuyuan.Pojo")//包的扫描
@Import(Config2.class)//相当于bean中的import引入多个Beans。xml
public class config {
    //注册一个Bean就相当于我们之前写的一个bean标签
    //这个方法的名字 就相当于bean标签中的id属性值
    //这个方法的返回值 就相当于 bean标签中的Class属性值
    @Bean
    public User getuser(){
        return new User();//就是返回要注入到Bean中的对象

    }
}

在这里插入图片描述

关于这种Java类的配置方式,我们在之后的SpringBoot 和 SpringCloud中还会大量看到,我们需要知道这些注解的作用即可!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值