java配置方法
1.新建一个Config文件夹

2.代码
package com.shao.config;
import com.shao.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
@Configuration //这是一个配置类
@ComponentScan("com.shao") //扫描
public class ShaoConfig {
//注册一个Bean,相当于之前写的bean标签
//这个方法的名字,就相当于bean中id属性
//返回值相当于class属性
@Bean
public User getUser(){
return new User(); //返回要注入的对象
}
}
3.测试
import com.shao.config.ShaoConfig;
import com.shao.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Tset {
@Test
public void test01(){
//如果使用了完全配置方法去做,就应该用AnnotationConfigApplicationContext这个类去调配置文件
ApplicationContext context = new AnnotationConfigApplicationContext(ShaoConfig.class);
User getUser = context.getBean("getUser", User.class);
System.out.println(getUser.name);
}
}
主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈

浙公网安备 33010602011771号