Spring完全注解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring完全注解

宋宋_浩浩_Java工程师   2022-09-29 我要评论

方式一:@Component + @ComponentScan + @Value + @Autowired

首先还是pom文件,maven项目依赖必不可少。

 <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.20</version>
    </dependency>
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

然后,写两个Java Bean,一个是Student学生类、另一个是School学校类。

由于不写xml配置,所以在两个类上方都要加上 @Component 注解,通过注解的方式将其交给Spring IOC容器管理,@Value注解则用于给8种基本数据类型以及String类型做依赖注入,@Autowired是针对引用类型的,这里不再多说了。

package com.szh.bean;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
 *
 */
@Data
@Component
public class Student {
    @Value("张起灵")
    private String name;
    @Value("20")
    private Integer age;
    @Autowired
    private School school;
}
package com.szh.bean;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
 *
 */
@Data
@Component
public class School {
    @Value("北京大学")
    private String name;
    @Value("北京市海淀区")
    private String address;
}

下面要写一个配置类,功能就是添加包扫描机制,确保上面那两个@Component 注解修饰的Java Bean可以被Spring扫描并添加至容器中。

package com.szh.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
 *
 */
@Configuration
@ComponentScan(basePackages = "com.szh.bean")
public class SpringConfig {
}

最后是我们的测试类了。

package com.szh;
import com.szh.bean.Student;
import com.szh.config.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 *
 */
public class MyTest {
    @Test
    public void testIocAnnotation() {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student = (Student) ioc.getBean("student");
        System.out.println(student);
    }
}

方式二:@Configuration + @Bean

pom文件和方式一是一样的。

下面是不一样的Java Bean。

package com.szh.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Goods {
    private String name;
    private String info;
}
package com.szh.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
/**
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
    private Integer id;
    private BigDecimal totalFee;
    private Goods goods;
}

然后是该方式对应的配置类,采用@Bean实现。

package com.szh.config;
import com.szh.entity.Goods;
import com.szh.entity.Order;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.math.BigDecimal;
/**
 *
 */
@Configuration
public class SpringConfig2 {
    @Bean
    public Goods goods() {
        return new Goods("联想-拯救者", "一款不错的游戏笔记本");
    }
    @Bean
    public Order order(Goods goods) {
        return new Order(1, new BigDecimal(9999), goods);
    }
}

最后是这种方式的测试类代码。

    @Test
    public void testIocAnnotation2() {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig2.class);
        Order order = (Order) ioc.getBean("order");
        System.out.println(order);
        System.out.println("IOC容器中存在的bean如下:");
        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
        for (String bean : beanDefinitionNames) {
            System.out.println(bean);
        }
    }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们