spring注解入门之IOC,DI

清疚 2022-06-05 05:50 331阅读 0赞

注解开发

1. IOC注解入门

1.导入jar包(还需要导入核心jar包及日志jar包)
spring-aop-xxx.jar

2.导入xml约束,导入下面的约束即可

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
  3. </beans>

如果需要使用IOC跟DI的注解,那么必须在xml里声明使用注解,然后spring就会在你提供的路径里面查找注解

  1. 打开注解,告诉spring cn.itcast下查找注解,
  2. <context:component-scan base-package="cn.itcast"/>

3.在类上面添加注解

  1. @Component("p")//相当于在xml里面写了一个bean,bean的id是"p"
  2. public class Product {
  3. public void allProduct(){
  4. System.out.println("假装显示了所有商品");
  5. }
  6. }

4.找工厂索取对象

  1. public class Demo {
  2. @Test
  3. public void test_01(){
  4. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
  5. Product p = (Product)context.getBean("p");
  6. p.allProduct();
  7. }
  8. }

运行结果如下

这里写图片描述

代码分析

1.扫描开关,如果需要扫描多个包,那么只需要加上包的前缀即可,

  1. <context:component-scan base-package="cn.itcast.annotation_ioc"/>

2.需要在类上加上@Component的标签,
3.工厂默认是单列的,如果想做成多列的,需要在类上加上@Scope注解
4.如果想指定初始化方法和销毁方法,可以使用@PostConstruct & @PreDestroy

  1. @PostConstruct
  2. public void init(){
  3. System.out.println("调用init~!~~!");
  4. }
  5. //销毁对象的时候调用
  6. @PreDestroy
  7. public void destroy(){
  8. System.out.println("调用destroy~!~~!");
  9. }

5.spring针对三层结构的写法, 也细化了每一层的注解。 @Component是通用的注解,分别为WEB层的@Controller,服务层的@Service和持久层的@Repository,建议大家不用使用@Component注解,因为以后有可能会被取消掉,

DI注解

依赖注入使用注解来实现 , DI的注解一般使用两个 @Resource | @Autowired

DI注入需要的jar包及xml约束都跟IOC一样,所以使用IOC的配置就行了

1.在xml里打开扫描开关,

  1. <!--如果IOC开过了,那么就不需要再开了-->
  2. <context:component-scan base-package="com.itcast"/>

2.创建一个商品信息类

  1. @Component("info")
  2. public class ProductInfo {
  3. //其实value注解不常用,因为我们这样写,还不如直接 String pname = "海鲜";
  4. @Value(value="海鲜")
  5. private String pname;
  6. @Value(value="15")
  7. private Double money;
  8. @Override //为了方便演示,加上toString方法
  9. public String toString() {
  10. return "ProductInfo{" +
  11. "pname='" + pname + '\'' +
  12. ", money=" + money +
  13. '}';
  14. }
  15. }

创建一个Demo类

  1. public class Demo {
  2. @Test
  3. public void test_01(){
  4. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
  5. ProductInfo p = (ProductInfo)context.getBean("info");
  6. System.out.println("info="+p);
  7. }
  8. }

运行结果如下
这里写图片描述

看运行结果可以发现,我们赋值String类型的字符串成功了,那么我们赋值对象类型的呢?
运行结果如下
这里写图片描述

由此可见,value不能赋值对象类型的属性

对象类型的属性需要@Resource | @Autowire
1.创建一个信息类

  1. @Component("info")
  2. public class ProductInfo {
  3. @Value(value="海鲜")
  4. private String pname;
  5. @Value(value="15")
  6. private Double money;
  7. @Override
  8. public String toString() {
  9. return "ProductInfo{" +
  10. "pname='" + pname + '\'' +
  11. ", money=" + money +
  12. '}';
  13. }
  14. }

2.创建一个Product类

  1. @Component("p")//相当与在xml里面写了一个bean,bean的id是"p"
  2. public class Product {
  3. @Resource(name="info")
  4. private ProductInfo p;
  5. public void allProduct(){
  6. System.out.println("假装显示了所有商品");
  7. }
  8. @Override //为了方便演示,也提供一个toString
  9. public String toString() {
  10. return "Product{" +
  11. "p=" + p +
  12. '}';
  13. }
  14. }

创建一个测试类

  1. public class Demo {
  2. @Test
  3. public void test_01(){
  4. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
  5. Product p = (Product)context.getBean("p");
  6. p.allProduct();
  7. System.out.println("Product="+p);
  8. }
  9. }

运行结果如下
这里写图片描述
赋值成功,

然后我们再使用@Autowire来注入值

@Autowired不用写参数,

  1. @Component("p")//相当与在xml里面写了一个bean,bean的id是"p"
  2. public class Product {
  3. @Autowired
  4. private ProductInfo p;
  5. public void allProduct(){
  6. System.out.println("假装显示了所有商品");
  7. }
  8. @Override
  9. public String toString() {
  10. return "Product{" +
  11. "p=" + p +
  12. '}';
  13. }
  14. }

测试类运行结果如下
这里写图片描述

@Autowired也赋值成功了,那么肯定也就会有疑问,它们的区别是什么?

我先来将它们的运作原理把
@Resource 注解会拿着提供的name值去xml里找id = name值的Bean,如果没找到就会去找注解,找到了就创建实例,赋值
@Autowired 是拿着对象的类型去找,如果找到了就创建类型,也就是说,Autowired一遇到接口就不能使用了,因为它拿着接口的类型去找,并不能找到你所遇到的实例,

@Autowired 是自动注入,会找到注入接口类型的实现类,然后创建对象注入进来,如果存在多个实现类,那么会抛出异常
@Resource(name=”ud”) : 这个是直接根据给定的标识符,找到具体类,然后创建对象, 注入进来

本章就讲到这里,写代码的时候千万别忘记了打开开关

  1. <context:component-scan base-package="com.itcast"/>

代码测试有效,有什么问题欢迎留言,

发表评论

表情:
评论列表 (有 0 条评论,331人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring_AOP注解入门

    AOP简介 AOP面向接口编程,可以说AOP是面向对象编程没错,但是如果需要更准确的答案,AOP是在(OOP)面向对象编程的基础扩展和优化的, 使用步骤如下 1.导