其他分享
首页 > 其他分享> > spring IOC 使用注解方式配置

spring IOC 使用注解方式配置

作者:互联网

spring·基于注解的方式IOC操作bean管理

1 配置maven依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.9.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>
</dependencies>

2 xml文件配置

6 完全注解开发

1 创建一个配置类

2 在类上面写注解

@Configuration //作为配置类,替代 xml 配置文件
@ComponentScan(basePackages = {"com.atguigu"})
public class SpringConfig {
}

3 创建测试类

将ClassPathXMLConfigApplication改写程AnnotationConfigApplication

@Test
public void testService2() {
 //加载配置类
 ApplicationContext context
 = new AnnotationConfigApplicationContext(SpringConfig.class);
    
    
}

 

标签:xml,Qualifier,spring,配置,扫描,注解,IOC,属性
来源: https://www.cnblogs.com/tunan-weixin/p/15582461.html