spring完全注解开发

前面创造的类都一样除了不要xml配置文件一样的就不写了:
这是代替xml的配置文件,用到了
@Configuration还有@ComponentScan(basePackages = {“cn.zsp”})
链接:https://blog.csdn.net/qq_42388853/article/details/108305350

package cn.zsp.spring5.config;

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

@Configuration
@ComponentScan(basePackages = {"cn.zsp"})
public class SpringConfig {

}

test2是我们的测试类,这里的new 变成了AnnotationConfigApplicationContext

package cn.zsp.spring5.testdemo;

import cn.zsp.spring5.config.SpringConfig;
import cn.zsp.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo1 {
    @Test
    public  void test1(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.show();

    }
    @Test
    public  void test2(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.show();

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值