Spring05-(使用注解开发)

在spring4之后,想要使用注解形式,必须得要引入aop的包
在这里插入图片描述在配置文件applicationContext.xml当中,还得要引入一个context约束

<?xml version="1.0" encoding="UTF8"?>
<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>

Bean的实现

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

  1. 配置扫描哪些包下的注解(applicationContext.xml)
    <!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.codeyancy.pojo"/>
  1. 在指定包下编写类,增加注解
//等价于<bean id="user" class="com.codeyancy.pojo.User"/>
//@Component 组件
@Component
public class User {
    public String name = "张三";

}
  1. 测试
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user");

        System.out.println(user.name);
    }
}

结果

在这里插入图片描述

属性注入

使用注解注入属性

User

  1. 可以不用提供set方法,直接在属性上方添加@value(“值”)
//等价于<bean id="user" class="com.codeyancy.pojo.User"/>
//@Component 组件
@Component
public class User {
        //相当于<property name="name" value="zhangsan" />
    @Value("zhangsan")
    public String name;

}
  1. 如果提供了set方法,在set方法上添加@value(“值”);
//等价于<bean id="user" class="com.codeyancy.pojo.User"/>
//@Component 组件
@Component
public class User {

    public String name;

    //相当于<property name="name" value="zhangsan" />
    @Value("zhangsan")
    public void setName(String name) {
        this.name = name;
    }
}

和前面一个测试类相同,测试结果为:zhangsan

衍生注解

这些注解,就是替代了在配置文件当中配置步骤而已!更加的方便快捷!

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

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

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

自动装配注解

见Spring04

作用域

@Scope

@Scope("singleton")//单例模式
public class User {

    //相当于<property name="name" value="zhangsan" />
    @Value("zhangsan")
    public String name;
}
  • singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
  • prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收

小结

XML与注解:

  • xml更加万能,适用于任何场合!维护简单方便
  • 注解 不是自己类 使用不了,维护相对复杂

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

  • xml管理Bean
  • 注解完成属性注入
  • 使用过程中,必须让注解生效,就需要开启注解的支持
    <!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.codeyancy"/>
    <!--开启注解的支持-->
    <context:annotation-config/>

使用Java的方式进行配置Spring

现在完全不使用Spring的xml配置了,完全交给java来处理

JavaConfig 原来是 Spring 的一个子项目,它通过 Java 类的方式提供 Bean 的定义信息,在 Spring4 之后的版本, JavaConfig 已正式成为 Spring4 的核心功能 。

  1. 编写一个实体类(User)
package com.codeyancy.pojo;

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

//这里这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中
@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }


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

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

  1. 新建一个config配置包,编写一个MyConfig配置类
package com.codeyancy.config;

import com.codeyancy.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;

//@Configuration代表这是一个配置类,就和我们之前看的beans.xml
@Configuration
@ComponentScan("com.codeyancy.pojo")//扫描包
@Import(MyConfig2.class)//导入MyConfig2配置类
public class MyConfig {
    //注册一个bean,就相当于我们之前写的一个bean标签
    //这个方法的名字,就相当于bean标签中的id属性
    //这个方法的返回值,就相当于bean标签中的class属性
    @Bean
    public User getUser(){
        return new User();//就是返回要注入到bean的对象!
    }
}

创建MyConfig2配置类(测试@Import)

@Configuration
public class MyConfig2 {
}

  1. 测试
    public static void main(String[] args) {
        //如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载!
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User getUser = (User) context.getBean("getUser");
        System.out.println(getUser.getName());
    }
}
  1. 成功输出结果:zhangsan
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值