import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
public @Bean TDao TDao(){
return new TDao();
}
public @Bean TService TService(){
return new TService(TDao());
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={AppConfig.class})
public class ConfigTest {
@Autowired
TService tService;
/*public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
TService myBean = ctx.getBean(TService.class);
myBean.select();
}*/
@Test
public void testselect(){
tService.select();
}
}
public class TService {
TDao tDao;
public TService(TDao tDao) {
super();
this.tDao = tDao;
}
public void select(){
tDao.select();
}
}
public class TDao {
public void select(){
System.out.println("select");
}
}