基于注解的方式实现Bean管理和注入属性

目录

1.什么是注解

2.Spring针对Bean管理中创建对象提供的注解(注解后面都需要value值)

3.用注解的方式创建对象

4.用注解的方实现属性注入(值注入)

 5.IOC纯注解的方式(全注解<去掉.xml文件>)


1.什么是注解

①:注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值...)

②:使用注解,注解作用在类上面,方法上面,属性上边

③:使用注解的目的:简化XML配置

2.Spring针对Bean管理中创建对象提供的注解(注解后面都需要value值)

@Component 普通的类

@Controller 表现层

@Service 业务层

@Repository 持久层

3.用注解的方式创建对象

3.1:编写接口和实现类

public class Car{

    public  void hello(){
        System.out.println("输出成功!!!!");
    }

}

 3.2:  在需要管理的类上添加@Component注解

@Component("car")
public class Car{

    public  void hello(){
        System.out.println("输出成功!!!!");
    }

}

3.3: 编写配置文件,重点是开启注解扫描

<?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">
   
    <!--开启注解扫描 com.qcby所有的包中的所有的类-->
    <context:component-scan base-package="com.qcby"/>
</beans>

3.4: 编写测试方法

package com.qcby.test;

import com.qcby.testanno.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo2 {
   @Test
    public void run3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Car cars  = (Car) ac.getBean("www");
        cars.hello();
    }
}

4.用注解的方实现属性注入(值注入)

@Value 用于注入普通类型(String,int,double等类型)

@Autowired 默认按类型进行自动装配(引用类型)

@Qualifier 不能单独使用必须和@Autowired一起使用,强制使用名称注入

@Resource Java提供的注解,也被支持。使用name属性,按名称注入

具体代码如下

package com.qcby.service;

import com.qcby.Demo;
import com.qcby.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Component("car")
//@Controller("www")
public class Car {
    @Value("大众")
    private String name;
    @Value(value = "123456")
    private Double money;
//    @Autowired
//    private User user;
    public  void hello(){
        System.out.println("输出成功!!!!");
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", money=" + money +
//                ", user=" + user +
                '}';
    }
}
   @Test
    public void run3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Car cars  = (Car) ac.getBean("car");
        System.out.println(cars.toString());
    }

 5.IOC纯注解的方式(全注解<去掉.xml文件>)

纯注解的方式是微服务架构开发的主要方式,所以也是非常的重要。纯注解的目的是替换掉所有的配置文件。但是需要编写配置类

常用的注解总结

@Configuration 声明是配置类

@ComponentScan 扫描具体包结构的

5.1: 编写实体类

package com.qcby.service;

import com.qcby.Demo;
import com.qcby.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Component("car")
//@Controller("www")
public class Car {
    @Value("大众")
    private String name;
    @Value(value = "123456")
    private Double money;
//    @Autowired
//    private User user;


    public  void hello(){
        System.out.println("输出成功!!!!");
    }


    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", money=" + money +
//                ", user=" + user +
                '}';
    }
}

5.2:编写配置类,替换掉applicationContext.xml配置文件

package com.qcby;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
/*定义为配置类*/
// 扫描指定的包结构
@ComponentScan(value = "com.qcby.service")
public class SpringConfig {
}

5.3: 测试方法的编写

package com.qcby.test;
import com.qcby.demo4.Order;
import com.qcby.demo4.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

    @Test
    public void run2(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
        Car car = (Car) ac.getBean("car");
        System.out.println(car.toString());
    }

注意这句话:ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);

 IOC全注解终

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叫我老伯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值